Files
mantra.press/server/router/translate/index.js

206 lines
8.1 KiB
JavaScript
Raw Normal View History

const express = require('express')
2021-12-23 00:47:06 +02:00
const { Op } = require("sequelize")
module.exports = function (options) {
const db = options.db;
var router = express.Router();
router.route('/:id')
.get(function(request, response, next) {
2021-12-25 01:35:32 +02:00
db.TranslationEntryVersion.findByPk(request.params.id, {
include: [
{
2021-12-25 01:35:32 +02:00
association: db.TranslationEntryVersion.EntryVersion,
required: true,
include: [
{
2021-12-25 01:35:32 +02:00
association: db.EntryVersion.Chapters,
required: true,
limit: 1,
// TODO: Order by chapter index
}
]
}
]
2021-12-25 01:35:32 +02:00
}).then(translationEntryVersion => {
if (translationEntryVersion) {
response.redirect(`/translate/${translationEntryVersion.id}/chapter/${translationEntryVersion.entryVersion.chapters[0].id}`)
} else {
next()
}
})
})
router.route('/:id/chapter/:chapterId')
.get(function(request, response, next) {
db.Chapter.findByPk(request.params.chapterId, {
include: [
{
association: db.Chapter.Chunks,
include: [
{
association: db.Chunk.Translation
}
]
},
{
2021-12-25 01:35:32 +02:00
association: db.Chapter.EntryVersion,
required: true,
include: [
{
2021-12-25 01:35:32 +02:00
association: db.EntryVersion.TranslationEntryVersions,
required: true,
where: {
id: request.params.id
}
},
{
2021-12-25 01:35:32 +02:00
association: db.EntryVersion.Entry,
include: [
{
association: db.Entry.Dialect
}
]
}
]
}
]
}).then(chapter => {
if (chapter) {
response.display("translate-chapter", {
user: request.user,
pageTitle: `Translate Chapter ${chapter.name}`,
chapter: chapter
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
2021-12-22 22:34:58 +02:00
2021-12-23 00:47:06 +02:00
router.route('/:id/chapter/:chapterId/t/:chunkIndex')
2021-12-22 22:34:58 +02:00
.get(function(request, response, next) {
2021-12-23 00:47:06 +02:00
const previousIndex = Number(request.params.chunkIndex)-1
const nextIndex = Number(request.params.chunkIndex)+1
db.Chunk.findAll({
where: {
chapterId: request.params.chapterId,
index: {
[Op.between]: [
previousIndex,
nextIndex
],
}
},
limit: 3,
2021-12-22 22:34:58 +02:00
include: [
{
association: db.Chunk.Translation
},
{
association: db.Chunk.Chapter,
2021-12-23 00:47:06 +02:00
required: true,
2021-12-22 22:34:58 +02:00
include: [
{
2021-12-25 01:35:32 +02:00
association: db.Chapter.EntryVersion,
2021-12-22 22:34:58 +02:00
required: true,
include: [
{
2021-12-25 01:35:32 +02:00
association: db.EntryVersion.TranslationEntryVersions,
2021-12-22 22:34:58 +02:00
required: true,
where: {
id: request.params.id
}
},
{
2021-12-25 01:35:32 +02:00
association: db.EntryVersion.Entry,
include: [
{
association: db.Entry.Dialect
}
]
2021-12-22 22:34:58 +02:00
}
]
}
]
}
]
2021-12-23 00:47:06 +02:00
}).then(chunks => {
if (chunks.length > 0) {
const chunk = chunks.find(chunk => chunk.index == Number(request.params.chunkIndex))
2021-12-22 22:34:58 +02:00
response.display("translate-chunk", {
user: request.user,
2021-12-25 01:35:32 +02:00
pageTitle: `Translate ${chunk.chapter.entryVersion.entry.name}`,
2021-12-23 00:47:06 +02:00
chunk: chunk,
previousChunk: chunks.find(chunk => chunk.index == previousIndex),
nextChunk: chunks.find(chunk => chunk.index == nextIndex)
2021-12-22 22:34:58 +02:00
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
2021-12-25 02:03:56 +02:00
.post(function(request, response, next) {
db.Chunk.findOne({
where: {
chapterId: request.params.chapterId,
index: request.params.chunkIndex
},
include: [
{
association: db.Chunk.Translation
},
{
association: db.Chunk.Chapter,
required: true,
include: [
{
association: db.Chapter.EntryVersion,
required: true,
include: [
{
association: db.EntryVersion.TranslationEntryVersions,
required: true,
where: {
id: request.params.id
}
},
{
association: db.EntryVersion.Entry,
include: [
{
association: db.Entry.Dialect
}
]
}
]
}
]
}
]
}).then(async (chunk) => {
if (chunk) {
if (chunk.translation) {
chunk.translation.text = request.body.translatedText
await chunk.translation.save()
} else {
translation = await db.Translation.create({
chunkId: chunk.id,
text: request.body.translatedText,
translationEntryVersionId: request.params.id
})
}
response.redirect(`/translate/${request.params.id}/chapter/${request.params.chapterId}/t/${request.params.chunkIndex}`)
} else {
next()
}
}).catch(error => {
next(error)
})
})
return router;
};