Add versioning
This commit is contained in:
@@ -23,14 +23,15 @@ module.exports = function (options) {
|
||||
var campaignsRouter = require('./campaigns/')(options);
|
||||
var apiRouter = require('./api/')(options);
|
||||
var translateRouter = require('./translate/')(options);
|
||||
var versionsRouter = require('./versions')(options);
|
||||
|
||||
|
||||
router.use('/library', libraryRouter);
|
||||
router.use('/pledges', pledgesRouter);
|
||||
router.use('/campaigns', campaignsRouter);
|
||||
router.use('/account', accountRouter);
|
||||
router.use('/api', apiRouter);
|
||||
router.use('/translate', translateRouter);
|
||||
router.use('/v', versionsRouter);
|
||||
|
||||
return router;
|
||||
};
|
||||
@@ -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;
|
||||
};
|
||||
@@ -7,14 +7,14 @@ module.exports = function (options) {
|
||||
|
||||
router.route('/:id')
|
||||
.get(function(request, response, next) {
|
||||
db.TranslationEntry.findByPk(request.params.id, {
|
||||
db.TranslationEntryVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.TranslationEntry.Entry,
|
||||
association: db.TranslationEntryVersion.EntryVersion,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.Chapters,
|
||||
association: db.EntryVersion.Chapters,
|
||||
required: true,
|
||||
limit: 1,
|
||||
// TODO: Order by chapter index
|
||||
@@ -22,9 +22,9 @@ module.exports = function (options) {
|
||||
]
|
||||
}
|
||||
]
|
||||
}).then(translationEntry => {
|
||||
if (translationEntry) {
|
||||
response.redirect(`/translate/${translationEntry.id}/chapter/${translationEntry.entry.chapters[0].id}`)
|
||||
}).then(translationEntryVersion => {
|
||||
if (translationEntryVersion) {
|
||||
response.redirect(`/translate/${translationEntryVersion.id}/chapter/${translationEntryVersion.entryVersion.chapters[0].id}`)
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
@@ -44,18 +44,23 @@ module.exports = function (options) {
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.Chapter.Entry,
|
||||
association: db.Chapter.EntryVersion,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.TranslationEntries,
|
||||
association: db.EntryVersion.TranslationEntryVersions,
|
||||
required: true,
|
||||
where: {
|
||||
id: request.params.id
|
||||
}
|
||||
},
|
||||
{
|
||||
association: db.Entry.Dialect
|
||||
association: db.EntryVersion.Entry,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.Dialect
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -99,18 +104,23 @@ module.exports = function (options) {
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Chapter.Entry,
|
||||
association: db.Chapter.EntryVersion,
|
||||
required: true,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.TranslationEntries,
|
||||
association: db.EntryVersion.TranslationEntryVersions,
|
||||
required: true,
|
||||
where: {
|
||||
id: request.params.id
|
||||
}
|
||||
},
|
||||
{
|
||||
association: db.Entry.Dialect
|
||||
association: db.EntryVersion.Entry,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.Dialect
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -122,7 +132,7 @@ module.exports = function (options) {
|
||||
const chunk = chunks.find(chunk => chunk.index == Number(request.params.chunkIndex))
|
||||
response.display("translate-chunk", {
|
||||
user: request.user,
|
||||
pageTitle: `Translate ${chunk.chapter.entry.name}`,
|
||||
pageTitle: `Translate ${chunk.chapter.entryVersion.entry.name}`,
|
||||
chunk: chunk,
|
||||
previousChunk: chunks.find(chunk => chunk.index == previousIndex),
|
||||
nextChunk: chunks.find(chunk => chunk.index == nextIndex)
|
||||
|
||||
268
server/router/versions/index.js
Normal file
268
server/router/versions/index.js
Normal file
@@ -0,0 +1,268 @@
|
||||
const express = require('express');
|
||||
const req = require('express/lib/request');
|
||||
|
||||
module.exports = function (options) {
|
||||
const db = options.db;
|
||||
var router = express.Router();
|
||||
|
||||
router.route('/:id')
|
||||
.get(function(request, response, next) {
|
||||
db.EntryVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.EntryVersion.Entry,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.EntryApproval
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.EntryVersion.Chapters
|
||||
},
|
||||
{
|
||||
association: db.EntryVersion.TranslationEntryVersions
|
||||
}
|
||||
]
|
||||
}).then(entryVersion => {
|
||||
if (entryVersion) {
|
||||
response.display("entry-version", {
|
||||
user: request.user,
|
||||
pageTitle: "Entry - Mantra",
|
||||
entryVersion: entryVersion
|
||||
})
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
})
|
||||
|
||||
router.route('/:id/chapters/add')
|
||||
.get(function(request, response, next) {
|
||||
db.EntryVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.EntryVersion.Entry,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.EntryApproval
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.EntryVersion.Chapters
|
||||
}
|
||||
]
|
||||
}).then(entryVersion => {
|
||||
if (entryVersion) {
|
||||
response.display("chapter-form", {
|
||||
user: request.user,
|
||||
pageTitle: "Library - Mantra",
|
||||
entryVersion: entryVersion
|
||||
})
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
})
|
||||
.post(function(request, response, next) {
|
||||
db.EntryVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.EntryVersion.Entry,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.EntryApproval
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.EntryVersion.Chapters
|
||||
}
|
||||
]
|
||||
}).then(entryVersion => {
|
||||
const isString = typeof request.body.text === "string" || request.body.text instanceof String
|
||||
if (entryVersion && isString) {
|
||||
const chunks = request.body.text.trim().split(/\r?\n\r?\n/)
|
||||
db.Chapter.create({
|
||||
name: chunks[0].trim(),
|
||||
entryVersionId: entryVersion.id,
|
||||
chunks: chunks.map((chunk,index) => {
|
||||
return {
|
||||
text: chunk,
|
||||
index: index
|
||||
}
|
||||
})
|
||||
}, {
|
||||
include: [
|
||||
{
|
||||
association: db.Chapter.Chunks
|
||||
}
|
||||
]
|
||||
}).then(chapter => {
|
||||
if (chapter) {
|
||||
response.redirect(`/v/${chapter.entryVersionId}/chapters/${chapter.id}`)
|
||||
} else {
|
||||
response.display("chapter-form", {
|
||||
user: request.user,
|
||||
pageTitle: "Library - Mantra",
|
||||
entryVersion: entryVersion
|
||||
})
|
||||
}
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
|
||||
} 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.EntryVersion,
|
||||
include: [
|
||||
{
|
||||
association: db.EntryVersion.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/:translationEntryVersionId')
|
||||
.get(function(request, response, next) {
|
||||
db.TranslationEntryVersion.findByPk(request.params.translationEntryVersionId, {
|
||||
include: [
|
||||
{
|
||||
association: db.TranslationEntryVersion.EntryVersion,
|
||||
include: [
|
||||
{
|
||||
association: db.EntryVersion.Entry
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}).then(translationEntryVersion => {
|
||||
if (translationEntryVersion) {
|
||||
response.display("translation-entry-version", {
|
||||
user: request.user,
|
||||
pageTitle: "Translation Entry - Mantra",
|
||||
translationEntryVersion: translationEntryVersion,
|
||||
})
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
})
|
||||
|
||||
router.route('/:id/translations/add')
|
||||
.get(function(request, response, next) {
|
||||
db.EntryVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.EntryVersion.Entry,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.EntryApproval
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.EntryVersion.TranslationEntryVersions
|
||||
}
|
||||
]
|
||||
}).then(entryVersion => {
|
||||
if (entryVersion) {
|
||||
response.display("translation-form", {
|
||||
user: request.user,
|
||||
pageTitle: "Library - Mantra",
|
||||
entryVersion: entryVersion
|
||||
})
|
||||
} else {
|
||||
next()
|
||||
}
|
||||
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
})
|
||||
.post(function(request, response, next) {
|
||||
db.EntryVersion.findByPk(request.params.id, {
|
||||
include: [
|
||||
{
|
||||
association: db.EntryVersion.Entry,
|
||||
include: [
|
||||
{
|
||||
association: db.Entry.EntryApproval
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
association: db.EntryVersion.TranslationEntryVersions
|
||||
}
|
||||
]
|
||||
}).then(async (entryVersion) => {
|
||||
if (entryVersion) {
|
||||
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 translationEntryVersion = await db.TranslationEntryVersion.create({
|
||||
name: dialect.name,
|
||||
entryVersionId: entryVersion.id,
|
||||
dialectId: dialect.id
|
||||
})
|
||||
|
||||
if (translationEntryVersion) {
|
||||
return response.redirect(`/v/${entryVersion.id}/translations/${translationEntryVersion.id}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
next()
|
||||
}).catch(error => {
|
||||
next(error)
|
||||
})
|
||||
})
|
||||
|
||||
return router;
|
||||
};
|
||||
Reference in New Issue
Block a user