Added DELETE route

This commit is contained in:
2021-01-30 12:30:37 +11:00
parent adb91947d7
commit d24c16c5ad
3 changed files with 63 additions and 4 deletions
+17 -4
View File
@@ -57,28 +57,41 @@ POST /news
"body": body //body of the article "body": body //body of the article
} }
//result //result:
{ {
"ok": ok //true on success, otherwise false "ok": ok //true on success, otherwise false
"index": index //new index of the article, or undefined "index": index //new index of the article, or undefined
"error": error //error encountered, or undefined "error": error //error encountered, or undefined
} }
//similar to `/news/publish`, but allows overwriting an existing post //similar to `/news/publish`, but allows overwriting an existing article
PATCH /news/:id PATCH /news/:id
//arguments: //arguments:
{ {
"key": key //the whitelist key, allows access to the POST routes "key": key //the whitelist key, allows access to the PATCH routes
"title": title //title of the article "title": title //title of the article
"author": author //author of the article "author": author //author of the article
"body": body //body of the article "body": body //body of the article
} }
//result //result:
{ {
"ok": ok //true on success, otherwise false "ok": ok //true on success, otherwise false
"error": error //error encountered, or undefined "error": error //error encountered, or undefined
} }
//remove an article from the news feed
DELETE /news/:id
//arguments:
{
"key": key //the whitelist key, allows access to the DELETE routes
}
//result:
{
"ok": ok //true on success, otherwise false
"error": error //error encountered, or undefined
}
``` ```
+3
View File
@@ -5,6 +5,7 @@ const router = express.Router();
const query = require('./query'); const query = require('./query');
const publish = require('./publish'); const publish = require('./publish');
const edit = require('./edit'); const edit = require('./edit');
const remove = require('./remove');
//basic route management //basic route management
router.get('/', query(false, false)); router.get('/', query(false, false));
@@ -20,4 +21,6 @@ router.post('/', publish);
router.patch('/:id(\\d+)', edit); router.patch('/:id(\\d+)', edit);
router.delete('/:id(\\d+)', remove);
module.exports = router; module.exports = router;
+43
View File
@@ -0,0 +1,43 @@
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 remove non-existing record' });
}
//store the revision
await revisions.upsert({
title: record.title,
author: record.author,
body: record.body,
originalIndex: record.index
});
//destroy the data
await articles.destroy({
where: {
index: req.params.id
}
});
return res.status(200).json({
ok: true
});
};
module.exports = route;