@@ -2,12 +2,12 @@ const { Op } = require('sequelize');
|
||||
const { articles } = require('../database/models');
|
||||
|
||||
//the query function that can be reused
|
||||
const query = (ascending, titlesOnly) => async (req, res) => {
|
||||
//specific search
|
||||
const query = (ascending, metadataOnly) => async (req, res) => {
|
||||
//specific search (id is defined)
|
||||
if (req.params.id && typeof(parseInt(req.params.id)) === 'number') {
|
||||
const result = await articles.findOne({
|
||||
attributes: [
|
||||
'index', 'title', 'author', 'edits', 'createdAt', 'updatedAt', ...(!titlesOnly ? ['body'] : [])
|
||||
'index', 'title', 'author', 'edits', 'createdAt', 'updatedAt', ...(!metadataOnly ? ['body'] : [])
|
||||
],
|
||||
where: {
|
||||
index: {
|
||||
@@ -16,15 +16,15 @@ const query = (ascending, titlesOnly) => async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
//returns null if failed to find
|
||||
return res.status(200).json(result);
|
||||
//result is null if failed to find
|
||||
return res.status(200).json(result || []);
|
||||
}
|
||||
|
||||
//default search
|
||||
else {
|
||||
const result = await articles.findAndCountAll({
|
||||
attributes: [
|
||||
'index', 'title', 'author', 'edits', 'createdAt', 'updatedAt', ...(!titlesOnly ? ['body'] : [])
|
||||
'index', 'title', 'author', 'edits', 'createdAt', 'updatedAt', ...(!metadataOnly ? ['body'] : [])
|
||||
],
|
||||
order: [
|
||||
['index', ascending ? 'ASC' : 'DESC']
|
||||
|
||||
Reference in New Issue
Block a user