Files
mantra.press/server/router/library/index.js

118 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-12-19 18:17:51 +02:00
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.Artifact.findAll({
2021-12-19 18:17:51 +02:00
2022-01-07 00:28:07 +02:00
}).then(artifacts => {
2021-12-19 18:17:51 +02:00
response.display("library", {
user: request.user,
pageTitle: "Library - Mantra",
2022-01-07 00:28:07 +02:00
artifacts: artifacts
2021-12-19 18:17:51 +02:00
})
}).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",
artifact: {
2021-12-25 01:35:32 +02:00
version: "1.0"
}
2021-12-19 18:17:51 +02:00
})
} else {
next()
}
})
.post(function(request, response, next) {
if (request.user) {
db.Dialect.findOne({
where: {
languageId: "en",
countryId: "int"
}
}).then(dialect => {
if (dialect) {
return db.Artifact.create({
2022-01-07 00:27:14 +02:00
creatorId: request.user.id,
2021-12-19 18:17:51 +02:00
name: request.body.name,
url: request.body.url,
dialectId: dialect.id,
2021-12-25 01:35:32 +02:00
licenseId: "copyright",
artifactVersions: [
2021-12-25 01:35:32 +02:00
{
2022-01-07 02:15:44 +02:00
creatorId: request.user.id,
2021-12-25 01:35:32 +02:00
tag: request.body.version
}
],
entityId: request.user.individualEntityUser.entityUser.entityId
2021-12-25 01:35:32 +02:00
}, {
include: [
{
association: db.Artifact.ArtifactVersions,
2021-12-25 01:35:32 +02:00
}
]
2021-12-19 18:17:51 +02:00
})
} else {
response.redirect("/library/add") // TODO: Show error message on missing dialect...
}
}).then(artifact => {
if (artifact) {
response.redirect(`/library/${artifact.id}`)
2021-12-19 18:17:51 +02:00
} else {
next()
}
}).catch(error => {
next(error)
})
} else {
next()
}
})
router.route('/:id')
.get(function(request, response, next) {
db.Artifact.findByPk(request.params.id, {
2021-12-19 18:17:51 +02:00
include: [
{
association: db.Artifact.ArtifactApproval
2021-12-19 21:04:17 +02:00
},
{
association: db.Artifact.ArtifactVersions
2021-12-19 21:04:17 +02:00
}
]
}).then(artifact => {
if (artifact) {
if (artifact.artifactVersions.length == 1) {
response.redirect(`/v/${artifact.artifactVersions[0].id}`)
2021-12-25 01:35:32 +02:00
} else {
response.display("artifact", {
2021-12-19 21:04:17 +02:00
user: request.user,
pageTitle: "Library - Mantra",
artifact: artifact
2021-12-19 21:04:17 +02:00
})
2021-12-19 21:25:47 +02:00
}
2021-12-25 01:35:32 +02:00
} else {
next()
}
}).catch(error => {
next(error)
})
})
2021-12-19 18:17:51 +02:00
return router;
};