diff --git a/README.md b/README.md index bf6f055..982b3a9 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ An API centric news server. Uses Sequelize and mariaDB by default. # 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 GET /news?limit=10 diff --git a/package.json b/package.json index 1b8410b..8370bf4 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "An API centric news server. Uses Sequelize and mariaDB by default.", "main": "server/server.js", "scripts": { - "start": "node server/server.js", + "start": "pm2 start server/server.js --watch", "dev": "npm run watch:server", "watch:server": "nodemon . --ext js,jsx,json --ignore 'node_modules/*'" }, diff --git a/server/news/query.js b/server/news/query.js index 705e77c..22cc216 100644 --- a/server/news/query.js +++ b/server/news/query.js @@ -1,3 +1,4 @@ +const { Op } = require('sequelize'); const { articles } = require('../database/models'); //the query function that can be reused @@ -8,13 +9,14 @@ const query = (ascending, titlesOnly) => async (req, res) => { attributes: [ 'index', 'title', 'author', ...(!titlesOnly ? ['body', 'edits'] : []) ], - order: [ - ['index', ascending ? 'ASC' : 'DESC'] - ], - offset: parseInt(req.query.id) || 0, - limit: 1 + where: { + index: { + [Op.eq]: ascending ? parseInt(req.params.id) : (await articles.max('index')) - parseInt(req.params.id) + 1 + } + } }); + //returns null if failed to find return res.status(200).json(result); } @@ -34,4 +36,4 @@ const query = (ascending, titlesOnly) => async (req, res) => { } }; -module.exports = query; \ No newline at end of file +module.exports = query;