From 46ca5a9a6d588fe5ca7a0c244b3fd95d625969ff Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sun, 19 Dec 2021 21:25:47 +0200 Subject: [PATCH] View chapter --- server/router/library/index.js | 46 +++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/server/router/library/index.js b/server/router/library/index.js index ce58384..f4a542a 100644 --- a/server/router/library/index.js +++ b/server/router/library/index.js @@ -88,25 +88,6 @@ module.exports = function (options) { }) }) - router.route('/:id/chapters/:chapterId') - .get(function(request, response, next) { - db.Chapter.findByPk(request.params.chapterId, { - include: [ - { - association: db.Chapter.Chunks - } - ] - }).then(chapter => { - response.display("chapter", { - user: request.user, - pageTitle: "Chapter - Mantra", - chapter: chapter - }) - }).catch(error => { - next(error) - }) - }) - router.route('/:id/chapters/add') .get(function(request, response, next) { db.Entry.findByPk(request.params.id, { @@ -146,7 +127,7 @@ module.exports = function (options) { }).then(entry => { const isString = typeof request.body.text === "string" || request.body.text instanceof String if (entry && isString) { - const chunks = new String(request.body.text.trim()).split(/\r?\n\r?\n/) + const chunks = request.body.text.trim().split(/\r?\n\r?\n/) db.Chapter.create({ name: chunks[0].trim(), entryId: entry.id, @@ -178,5 +159,30 @@ module.exports = function (options) { }) }) + router.route('/:id/chapters/:chapterId') + .get(function(request, response, next) { + db.Chapter.findByPk(request.params.chapterId, { + include: [ + { + association: db.Chapter.Chunks + }, + { + association: db.Chapter.Entry + } + ] + }).then(chapter => { + if (chapter) { + response.display("chapter", { + user: request.user, + pageTitle: "Chapter - Mantra", + chapter: chapter + }) + } else { + next() + } + }).catch(error => { + next(error) + }) + }) return router; }; \ No newline at end of file