Merge remote-tracking branch 'refs/remotes/origin/main' into main

This commit is contained in:
2021-01-31 08:13:05 +11:00
3 changed files with 10 additions and 7 deletions
+1
View File
@@ -10,6 +10,7 @@ An API centric news server. Uses Sequelize and mariaDB by default.
# API # API
``` ```
//NOTE: GET will return null if a specific article can't be found
//NOTE: you can add a "limit" query parameter to change the default limit //NOTE: you can add a "limit" query parameter to change the default limit
GET /news?limit=10 GET /news?limit=10
+1 -1
View File
@@ -4,7 +4,7 @@
"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": "pm2 start server/server.js --watch",
"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/*'"
}, },
+8 -6
View File
@@ -1,3 +1,4 @@
const { Op } = require('sequelize');
const { articles } = require('../database/models'); const { articles } = require('../database/models');
//the query function that can be reused //the query function that can be reused
@@ -8,13 +9,14 @@ const query = (ascending, titlesOnly) => async (req, res) => {
attributes: [ attributes: [
'index', 'title', 'author', ...(!titlesOnly ? ['body', 'edits'] : []) 'index', 'title', 'author', ...(!titlesOnly ? ['body', 'edits'] : [])
], ],
order: [ where: {
['index', ascending ? 'ASC' : 'DESC'] index: {
], [Op.eq]: ascending ? parseInt(req.params.id) : (await articles.max('index')) - parseInt(req.params.id) + 1
offset: parseInt(req.query.id) || 0, }
limit: 1 }
}); });
//returns null if failed to find
return res.status(200).json(result); return res.status(200).json(result);
} }
@@ -34,4 +36,4 @@ const query = (ascending, titlesOnly) => async (req, res) => {
} }
}; };
module.exports = query; module.exports = query;