diff --git a/server/router/library/index.js b/server/router/library/index.js index 8ae73cd..ce58384 100644 --- a/server/router/library/index.js +++ b/server/router/library/index.js @@ -72,6 +72,9 @@ module.exports = function (options) { include: [ { association: db.Entry.EntryApproval + }, + { + association: db.Entry.Chapters } ] }).then(entry => { @@ -84,5 +87,96 @@ module.exports = function (options) { next(error) }) }) + + 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, { + include: [ + { + association: db.Entry.EntryApproval + }, + { + association: db.Entry.Chapters + } + ] + }).then(entry => { + if (entry) { + response.display("chapter-form", { + user: request.user, + pageTitle: "Library - Mantra", + entry: entry + }) + } else { + next() + } + + }).catch(error => { + next(error) + }) + }) + .post(function(request, response, next) { + db.Entry.findByPk(request.params.id, { + include: [ + { + association: db.Entry.EntryApproval + }, + { + association: db.Entry.Chapters + } + ] + }).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/) + db.Chapter.create({ + name: chunks[0].trim(), + entryId: entry.id, + chunks: chunks.map((chunk,index) => { + return { + text: chunk, + index: index + } + }) + }, { + include: [ + { + association: db.Chapter.Chunks + } + ] + }).then(chapter => { + response.display("chapter-form", { + user: request.user, + pageTitle: "Library - Mantra", + entry: entry + }) + }) + + } else { + next() + } + }).catch(error => { + next(error) + }) + }) + return router; }; \ No newline at end of file diff --git a/server/views/chapter-form.pug b/server/views/chapter-form.pug new file mode 100644 index 0000000..e3aa2e3 --- /dev/null +++ b/server/views/chapter-form.pug @@ -0,0 +1,18 @@ +extend templates/layout.pug + +block content + .container + .center + h1 Chapter in #{entry.name} + + .row + .col.s12 + form.row(action=`/library/${entry.id}/chapters/add`, method="post") + .col.s12.input-field + textarea#textarea.materialize-textarea(name="text") + label(for="textarea") Chapter Text + .col.s12.input-field + input#index(type="number", name="index", value=entry.chapters.length+1) + label(for="index") Chapter Number + .col.s12 + button.btn.black(type="submit") add chapter \ No newline at end of file diff --git a/server/views/chapter.pug b/server/views/chapter.pug new file mode 100644 index 0000000..4eb5fe4 --- /dev/null +++ b/server/views/chapter.pug @@ -0,0 +1,13 @@ +extend templates/layout.pug + +block content + .container + + + //- h1= chapter.name + + a.btn.black(href=`/library/${chapter.entryId}`) go to entry + + .row + each chunk in chapter.chunks + p.flow-text= chunk.text \ No newline at end of file diff --git a/server/views/entry.pug b/server/views/entry.pug index 5507bbd..216b079 100644 --- a/server/views/entry.pug +++ b/server/views/entry.pug @@ -3,4 +3,16 @@ extend templates/layout.pug block content .container .center - h1= entry.name \ No newline at end of file + h1= entry.name + + a.btn.black(href=`/library/${entry.id}/chapters/add`) add chapter + + if entry.chapters.length == 0 + p.flow-text No chapters added + else + .row + each chapter in entry.chapters + .col.s12 + a(href=`/library/${entry.id}/chapters/${chapter.id}`) + .card-panel + p.flow-text= chapter.name \ No newline at end of file