const express = require('express'); const req = require('express/lib/request'); module.exports = function (options) { const db = options.db; var router = express.Router(); router.route('/') .get(function(request, response, next) { db.Entry.findAll({ }).then(entries => { response.display("library", { user: request.user, pageTitle: "Library - Mantra", entries: entries }) }).catch(error => { next(error) }) }) router.route('/add') .get(function(request, response, next) { if (request.user) { response.display("library-form", { user: request.user, pageTitle: "Add Library - Mantra", entry: { } }) } else { next() } }) .post(function(request, response, next) { if (request.user) { db.Dialect.findOne({ where: { languageId: "en", countryId: "int" } }).then(dialect => { if (dialect) { return db.Entry.create({ name: request.body.name, url: request.body.url, dialectId: dialect.id, licenseId: "copyright" }) } else { response.redirect("/library/add") // TODO: Show error message on missing dialect... } }).then(entry => { if (entry) { response.redirect(`/library/${entry.id}`) } else { next() } }).catch(error => { next(error) }) } else { next() } }) router.route('/:id') .get(function(request, response, next) { db.Entry.findByPk(request.params.id, { include: [ { association: db.Entry.EntryApproval }, { association: db.Entry.Chapters } ] }).then(entry => { response.display("entry", { user: request.user, pageTitle: "Library - Mantra", entry: entry }) }).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 = 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) }) }) 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; };