Publish and edit routes are working
This commit is contained in:
@@ -6,4 +6,5 @@ DB_USERNAME=news
|
|||||||
DB_PASSWORD=charizard
|
DB_PASSWORD=charizard
|
||||||
DB_TIMEZONE=Australia/Sydney
|
DB_TIMEZONE=Australia/Sydney
|
||||||
|
|
||||||
QUERY_LIMIT=10
|
QUERY_LIMIT=10
|
||||||
|
QUERY_KEY=key
|
||||||
@@ -5,7 +5,8 @@ An API centric news server. Uses Sequelize and mariaDB by default.
|
|||||||
# API
|
# API
|
||||||
|
|
||||||
```
|
```
|
||||||
//NOTE: you can add a "limit" parameter to change the default limit
|
//NOTE: you can add a "limit" query parameter to change the default limit
|
||||||
|
/news?limit=10
|
||||||
|
|
||||||
//GET get latest news, up to a default limit, or specify the index "id"
|
//GET get latest news, up to a default limit, or specify the index "id"
|
||||||
/news/:id
|
/news/:id
|
||||||
@@ -42,7 +43,7 @@ An API centric news server. Uses Sequelize and mariaDB by default.
|
|||||||
]
|
]
|
||||||
|
|
||||||
//POST send a formatted JSON object, returns new index on success, or error on failure
|
//POST send a formatted JSON object, returns new index on success, or error on failure
|
||||||
/publish
|
/news/publish
|
||||||
|
|
||||||
//arguments:
|
//arguments:
|
||||||
{
|
{
|
||||||
@@ -59,8 +60,8 @@ An API centric news server. Uses Sequelize and mariaDB by default.
|
|||||||
"error": error //error encountered, or undefined
|
"error": error //error encountered, or undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
//POST similar to /publish, but allows overwriting an existing post
|
//PATCH similar to `/news/publish`, but allows overwriting an existing post
|
||||||
/edit
|
/news/edit/:id
|
||||||
|
|
||||||
//arguments:
|
//arguments:
|
||||||
{
|
{
|
||||||
@@ -68,7 +69,6 @@ An API centric news server. Uses Sequelize and mariaDB by default.
|
|||||||
"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
|
||||||
"overwrite": index //the index to save as -
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//result
|
//result
|
||||||
@@ -78,4 +78,3 @@ An API centric news server. Uses Sequelize and mariaDB by default.
|
|||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Generated
+1
@@ -8,6 +8,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"mariadb": "^2.5.2",
|
"mariadb": "^2.5.2",
|
||||||
|
|||||||
+4
-3
@@ -4,9 +4,9 @@
|
|||||||
"description": "An API centric news server. Uses Sequelize and mariaDB by default.",
|
"description": "An API centric news server. Uses Sequelize and mariaDB by default.",
|
||||||
"main": "server/server.js",
|
"main": "server/server.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node server/server.js",
|
"start": "node server/server.js",
|
||||||
"dev": "npm run watch:server",
|
"dev": "npm run watch:server",
|
||||||
"watch:server": "nodemon . --ext js,jsx,json --ignore 'node_modules/*'"
|
"watch:server": "nodemon . --ext js,jsx,json --ignore 'node_modules/*'"
|
||||||
},
|
},
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
@@ -19,6 +19,7 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://github.com/krgamestudios/news-server#readme",
|
"homepage": "https://github.com/krgamestudios/news-server#readme",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.19.0",
|
||||||
"dotenv": "^8.2.0",
|
"dotenv": "^8.2.0",
|
||||||
"express": "^4.17.1",
|
"express": "^4.17.1",
|
||||||
"mariadb": "^2.5.2",
|
"mariadb": "^2.5.2",
|
||||||
|
|||||||
@@ -17,16 +17,11 @@ const revisions = sequelize.define('revisions', {
|
|||||||
body: {
|
body: {
|
||||||
type: Sequelize.TEXT,
|
type: Sequelize.TEXT,
|
||||||
defaultValue: ''
|
defaultValue: ''
|
||||||
},
|
|
||||||
|
|
||||||
revision: {
|
|
||||||
type: Sequelize.INTEGER(11),
|
|
||||||
defaultValue: 0
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//relationships
|
//relationships
|
||||||
revisions.hasOne(articles, { as: 'article' });
|
articles.hasOne(revisions, { as: 'original' });
|
||||||
|
|
||||||
sequelize.sync();
|
sequelize.sync();
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -3,6 +3,8 @@ const router = express.Router();
|
|||||||
|
|
||||||
//the routes
|
//the routes
|
||||||
const query = require('./query');
|
const query = require('./query');
|
||||||
|
const publish = require('./publish');
|
||||||
|
const edit = require('./edit');
|
||||||
|
|
||||||
//basic route management
|
//basic route management
|
||||||
router.get('/', query(false, false));
|
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', query(true, true));
|
||||||
router.get('/archive/titles/:id(\\d+)', query(true, true));
|
router.get('/archive/titles/:id(\\d+)', query(true, true));
|
||||||
|
|
||||||
|
router.post('/', publish);
|
||||||
|
|
||||||
|
router.patch('/:id(\\d+)', edit);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -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,9 +2,6 @@ const { articles } = require('../database/models');
|
|||||||
|
|
||||||
//the query function that can be reused
|
//the query function that can be reused
|
||||||
const query = (ascending, titlesOnly) => async (req, res) => {
|
const query = (ascending, titlesOnly) => async (req, res) => {
|
||||||
console.log(ascending, titlesOnly);
|
|
||||||
console.log(req.params);
|
|
||||||
|
|
||||||
//specific search
|
//specific search
|
||||||
if (req.params.id && typeof(parseInt(req.params.id)) === 'number') {
|
if (req.params.id && typeof(parseInt(req.params.id)) === 'number') {
|
||||||
const result = await articles.findOne({
|
const result = await articles.findOne({
|
||||||
@@ -14,7 +11,7 @@ const query = (ascending, titlesOnly) => async (req, res) => {
|
|||||||
order: [
|
order: [
|
||||||
['index', ascending ? 'ASC' : 'DESC']
|
['index', ascending ? 'ASC' : 'DESC']
|
||||||
],
|
],
|
||||||
offset: parseInt(req.params.id),
|
offset: parseInt(req.query.id) || 0,
|
||||||
limit: 1
|
limit: 1
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -30,7 +27,7 @@ const query = (ascending, titlesOnly) => async (req, res) => {
|
|||||||
order: [
|
order: [
|
||||||
['index', ascending ? 'ASC' : 'DESC']
|
['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);
|
return res.status(200).json(result.rows || result);
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ require('dotenv').config();
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
const server = require('http').Server(app);
|
const server = require('http').Server(app);
|
||||||
|
const bodyParser = require('body-parser');
|
||||||
|
|
||||||
|
//config
|
||||||
|
app.use(bodyParser.json());
|
||||||
|
|
||||||
//database connection
|
//database connection
|
||||||
const database = require('./database');
|
const database = require('./database');
|
||||||
|
|||||||
Reference in New Issue
Block a user