Add chapters
This commit is contained in:
@@ -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;
|
||||
};
|
||||
Reference in New Issue
Block a user