Add versioning

This commit is contained in:
2021-12-25 01:35:32 +02:00
parent 1a539e673f
commit da9d91502c
14 changed files with 388 additions and 261 deletions

View File

@@ -26,7 +26,9 @@ module.exports = function (options) {
response.display("library-form", {
user: request.user,
pageTitle: "Add Library - Mantra",
entry: { }
entry: {
version: "1.0"
}
})
} else {
next()
@@ -45,7 +47,19 @@ module.exports = function (options) {
name: request.body.name,
url: request.body.url,
dialectId: dialect.id,
licenseId: "copyright"
licenseId: "copyright",
entryVersions: [
{
tag: request.body.version
}
]
}, {
include: [
{
association: db.Entry.EntryVersions,
}
]
})
} else {
response.redirect("/library/add") // TODO: Show error message on missing dialect...
@@ -74,85 +88,20 @@ module.exports = function (options) {
association: db.Entry.EntryApproval
},
{
association: db.Entry.Chapters
},
{
association: db.Entry.TranslationEntries
}
]
}).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
association: db.Entry.EntryVersions
}
]
}).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", {
if (entry.entryVersions.length == 1) {
response.redirect(`/v/${entry.entryVersions[0].id}`)
} else {
response.display("entry", {
user: request.user,
pageTitle: "Library - Mantra",
entry: entry
})
})
}
} else {
next()
@@ -162,131 +111,5 @@ 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)
})
})
router.route('/:id/translations/:translationEntryid')
.get(function(request, response, next) {
db.TranslationEntry.findByPk(request.params.translationEntryid, {
include: [
{
association: db.TranslationEntry.Entry
}
]
}).then(translationEntry => {
if (translationEntry) {
response.display("translation-entry", {
user: request.user,
pageTitle: "Translation Entry - Mantra",
translationEntry: translationEntry,
})
} else {
next()
}
}).catch(error => {
next(error)
})
})
router.route('/:id/translations/add')
.get(function(request, response, next) {
db.Entry.findByPk(request.params.id, {
include: [
{
association: db.Entry.EntryApproval
},
{
association: db.Entry.TranslationEntries
}
]
}).then(entry => {
if (entry) {
response.display("translation-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.TranslationEntries
},
{
association: db.Entry.Chapters,
include: [
{
association: db.Chapter.Chunks
}
]
}
]
}).then(async (entry) => {
if (entry) {
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 translationEntry = await db.TranslationEntry.create({
name: dialect.name,
entryId: entry.id,
dialectId: dialect.id
})
if (translationEntry) {
return response.redirect(`/library/${entry.id}/translations/${translationEntry.id}`)
}
}
}
}
next()
}).catch(error => {
next(error)
})
})
return router;
};