Fixed querying a specific article

This commit is contained in:
2021-01-30 03:13:23 +00:00
parent 31d85a1760
commit c582abb1e1
2 changed files with 9 additions and 6 deletions
+1
View File
@@ -9,6 +9,7 @@ Run the SQL script `sql/create_database.sql`. This will set up an empty database
# 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
+8 -6
View File
@@ -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;
module.exports = query;