Publish and edit routes are working

This commit is contained in:
2021-01-30 07:40:04 +11:00
parent 689a3371f1
commit 9b8ef8f2e7
10 changed files with 107 additions and 21 deletions
+48
View File
@@ -0,0 +1,48 @@
const { Op } = require('sequelize');
const { articles, revisions } = require('../database/models');
const route = async (req, res) => {
//check the key
if (req.body.key != process.env.QUERY_KEY) {
return res.status(401).json({ ok: false, error: 'invalid key' });
}
//get the existing record
const record = await articles.findOne({
where: {
index: {
[Op.eq]: req.params.id
}
}
});
if (!record) {
return res.status(500).json({ ok: false, error: 'failed to update non-existing record' });
}
//store the revision
await revisions.upsert({
title: record.title,
author: record.author,
body: record.body,
originalIndex: record.index
});
//update the data
await articles.update({
title: req.body.title,
author: req.body.author,
body: req.body.body,
edits: record.edits + 1
}, {
where: {
index: req.params.id
}
});
return res.status(200).json({
ok: true
});
};
module.exports = route;
+6
View File
@@ -3,6 +3,8 @@ const router = express.Router();
//the routes
const query = require('./query');
const publish = require('./publish');
const edit = require('./edit');
//basic route management
router.get('/', query(false, false));
@@ -14,4 +16,8 @@ router.get('/titles/:id(\\d+)', query(false, true));
router.get('/archive/titles', query(true, true));
router.get('/archive/titles/:id(\\d+)', query(true, true));
router.post('/', publish);
router.patch('/:id(\\d+)', edit);
module.exports = router;
+34
View File
@@ -0,0 +1,34 @@
const { articles } = require('../database/models');
const route = async (req, res) => {
//check the key
if (req.body.key != process.env.QUERY_KEY) {
return res.status(401).json({ ok: false, error: 'invalid key' });
}
//upsert the data
const [instance, created] = await articles.upsert({
title: req.body.title,
author: req.body.author,
body: req.body.body
});
if (!created) {
return res.status(500).json({ ok: false, error: 'failed to create record' });
}
//BUGFIX
const result = await articles.findOne({
order: [
['index', 'DESC']
]
});
return res.status(200).json({
ok: true,
// index: instance.get('index')
index: result.index
});
};
module.exports = route;
+2 -5
View File
@@ -2,9 +2,6 @@ const { articles } = require('../database/models');
//the query function that can be reused
const query = (ascending, titlesOnly) => async (req, res) => {
console.log(ascending, titlesOnly);
console.log(req.params);
//specific search
if (req.params.id && typeof(parseInt(req.params.id)) === 'number') {
const result = await articles.findOne({
@@ -14,7 +11,7 @@ const query = (ascending, titlesOnly) => async (req, res) => {
order: [
['index', ascending ? 'ASC' : 'DESC']
],
offset: parseInt(req.params.id),
offset: parseInt(req.query.id) || 0,
limit: 1
});
@@ -30,7 +27,7 @@ const query = (ascending, titlesOnly) => async (req, res) => {
order: [
['index', ascending ? 'ASC' : 'DESC']
],
count: req.params.limit || process.env.QUERY_LIMIT || 999
limit: parseInt(req.query.limit) || parseInt(process.env.QUERY_LIMIT) || 999
});
return res.status(200).json(result.rows || result);