206 lines
8.1 KiB
JavaScript
206 lines
8.1 KiB
JavaScript
const express = require('express')
|
|
const { Op } = require("sequelize")
|
|
|
|
module.exports = function (options) {
|
|
const db = options.db;
|
|
var router = express.Router();
|
|
|
|
router.route('/:id')
|
|
.get(function(request, response, next) {
|
|
db.TranslationEntryVersion.findByPk(request.params.id, {
|
|
include: [
|
|
{
|
|
association: db.TranslationEntryVersion.EntryVersion,
|
|
required: true,
|
|
include: [
|
|
{
|
|
association: db.EntryVersion.Chapters,
|
|
required: true,
|
|
limit: 1,
|
|
// TODO: Order by chapter index
|
|
}
|
|
]
|
|
}
|
|
]
|
|
}).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
|
|
}
|
|
]
|
|
},
|
|
{
|
|
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(chapter => {
|
|
if (chapter) {
|
|
response.display("translate-chapter", {
|
|
user: request.user,
|
|
pageTitle: `Translate Chapter ${chapter.name}`,
|
|
chapter: chapter
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
|
|
router.route('/:id/chapter/:chapterId/t/:chunkIndex')
|
|
.get(function(request, response, next) {
|
|
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,
|
|
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(chunks => {
|
|
if (chunks.length > 0) {
|
|
const chunk = chunks.find(chunk => chunk.index == Number(request.params.chunkIndex))
|
|
response.display("translate-chunk", {
|
|
user: request.user,
|
|
pageTitle: `Translate ${chunk.chapter.entryVersion.entry.name}`,
|
|
chunk: chunk,
|
|
previousChunk: chunks.find(chunk => chunk.index == previousIndex),
|
|
nextChunk: chunks.find(chunk => chunk.index == nextIndex)
|
|
})
|
|
} else {
|
|
next()
|
|
}
|
|
}).catch(error => {
|
|
next(error)
|
|
})
|
|
})
|
|
.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;
|
|
}; |