Queries are working

This commit is contained in:
2021-01-30 05:50:04 +11:00
parent 488c32eea2
commit 689a3371f1
12 changed files with 3824 additions and 1 deletions
+40
View File
@@ -0,0 +1,40 @@
const { articles } = require('../database/models');
//the query function that can be reused
const query = (ascending, titlesOnly) => async (req, res) => {
console.log(ascending, titlesOnly);
console.log(req.params);
//specific search
if (req.params.id && typeof(parseInt(req.params.id)) === 'number') {
const result = await articles.findOne({
attributes: [
'index', 'title', 'author', ...(!titlesOnly ? ['body', 'edits'] : [])
],
order: [
['index', ascending ? 'ASC' : 'DESC']
],
offset: parseInt(req.params.id),
limit: 1
});
return res.status(200).json(result);
}
//default search
else {
const result = await articles.findAndCountAll({
attributes: [
'index', 'title', 'author', ...(!titlesOnly ? ['body', 'edits'] : [])
],
order: [
['index', ascending ? 'ASC' : 'DESC']
],
count: req.params.limit || process.env.QUERY_LIMIT || 999
});
return res.status(200).json(result.rows || result);
}
};
module.exports = query;