const express = require('express'); const req = require('express/lib/request'); module.exports = function (options) { const db = options.db; var router = express.Router(); router.route('/:id') .get(function(request, response, next) { db.EntryVersion.findByPk(request.params.id, { include: [ { association: db.EntryVersion.Entry, include: [ { association: db.Entry.EntryApproval } ] }, { association: db.EntryVersion.Chapters }, { association: db.EntryVersion.TranslationEntryVersions } ] }).then(entryVersion => { if (entryVersion) { response.display("entry-version", { user: request.user, pageTitle: "Entry - Mantra", entryVersion: entryVersion }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/chapters/add') .get(function(request, response, next) { db.EntryVersion.findByPk(request.params.id, { include: [ { association: db.EntryVersion.Entry, include: [ { association: db.Entry.EntryApproval } ] }, { association: db.EntryVersion.Chapters } ] }).then(entryVersion => { if (entryVersion) { response.display("chapter-form", { user: request.user, pageTitle: "Library - Mantra", entryVersion: entryVersion }) } else { next() } }).catch(error => { next(error) }) }) .post(function(request, response, next) { db.EntryVersion.findByPk(request.params.id, { include: [ { association: db.EntryVersion.Entry, include: [ { association: db.Entry.EntryApproval } ] }, { association: db.EntryVersion.Chapters } ] }).then(entryVersion => { const isString = typeof request.body.text === "string" || request.body.text instanceof String if (entryVersion && isString) { const chunks = request.body.text.trim().split(/\r?\n\r?\n/) db.Chapter.create({ name: chunks[0].trim(), entryVersionId: entryVersion.id, chunks: chunks.map((chunk,index) => { return { text: chunk, index: index } }) }, { include: [ { association: db.Chapter.Chunks } ] }).then(chapter => { if (chapter) { response.redirect(`/v/${chapter.entryVersionId}/chapters/${chapter.id}`) } else { response.display("chapter-form", { user: request.user, pageTitle: "Library - Mantra", entryVersion: entryVersion }) } }).catch(error => { next(error) }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/chapters/:chapterId') .get(function(request, response, next) { db.Chapter.findByPk(request.params.chapterId, { include: [ { association: db.Chapter.Chunks }, { association: db.Chapter.EntryVersion, include: [ { association: db.EntryVersion.Entry } ] } ] }).then(chapter => { if (chapter) { response.display("chapter", { user: request.user, pageTitle: "Chapter - Mantra", chapter: chapter }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/translations/:translationEntryVersionId') .get(function(request, response, next) { db.TranslationEntryVersion.findByPk(request.params.translationEntryVersionId, { include: [ { association: db.TranslationEntryVersion.EntryVersion, include: [ { association: db.EntryVersion.Entry } ] }, { association: db.TranslationEntryVersion.ForkedFrom } ] }).then(translationEntryVersion => { if (translationEntryVersion) { response.display("translation-entry-version", { user: request.user, pageTitle: "Translation Entry - Mantra", translationEntryVersion: translationEntryVersion, }) } else { next() } }).catch(error => { next(error) }) }) router.route('/:id/translations/add') .get(function(request, response, next) { db.EntryVersion.findByPk(request.params.id, { include: [ { association: db.EntryVersion.Entry, include: [ { association: db.Entry.EntryApproval } ] }, { association: db.EntryVersion.TranslationEntryVersions } ] }).then(entryVersion => { if (entryVersion) { response.display("translation-form", { user: request.user, pageTitle: "Library - Mantra", entryVersion: entryVersion }) } else { next() } }).catch(error => { next(error) }) }) .post(function(request, response, next) { db.EntryVersion.findByPk(request.params.id, { include: [ { association: db.EntryVersion.Entry, include: [ { association: db.Entry.EntryApproval } ] }, { association: db.EntryVersion.TranslationEntryVersions } ] }).then(async (entryVersion) => { if (entryVersion) { const dialectTokens = request.body.dialect?.split(":") if (dialectTokens?.length == 2) { const countryId = dialectTokens[0].split("-")[0] const languageId = dialectTokens[0].split("-")[1] const dialect = await db.Dialect.findOne({ where: { countryId: countryId, languageId: languageId } }) if (dialect) { const translationEntryVersion = await db.TranslationEntryVersion.create({ name: dialect.name, entryVersionId: entryVersion.id, dialectId: dialect.id }) if (translationEntryVersion) { return response.redirect(`/v/${entryVersion.id}/translations/${translationEntryVersion.id}`) } } } } next() }).catch(error => { next(error) }) }) return router; };