diff --git a/src/libs/routes_v1/publication.js b/src/libs/routes_v1/publication.js index 4aece71a9a99c4819bce41dadd56414341873ad4..1b2e24e5a286d6c15400e28046162173c85f0ca3 100644 --- a/src/libs/routes_v1/publication.js +++ b/src/libs/routes_v1/publication.js @@ -31,34 +31,49 @@ function emailSyntax(email) { pubApp.get('/', async (req, res, next) => { const order = req.query.order || 'DESC'; - const filter = req.query.is_draft || 'false' ; + const filter = req.query.filter || 'all' ; const page = parseInt(req.query.page) || 1; // Current page number const pageSize = parseInt(req.query.pageSize) || 5; // Number of items per page - try { - const totalCount = await Publication.count({where:{is_draft:filter}}); - const offset = (page - 1) * pageSize; - - const publis = await Publication.findAll({ - offset, - limit: pageSize, - order: [ - ['created_at', order], - ], - where:{ - is_draft:filter - } - }); - - res.json({ - page, - pageSize, - totalCount, - data: publis, - }); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'An error occurred' }); - } + if(filter === 'all'){ + const totalCount = await Publication.count(); + const offset = (page - 1) * pageSize; + + const publis = await Publication.findAll({ + offset, + limit: pageSize, + }); + + res.json({ + page, + pageSize, + totalCount, + data: publis, + });} + else { + const totalCount = await Publication.count({where:{is_draft:filter}}); + const offset = (page - 1) * pageSize; + + const publis = await Publication.findAll({ + offset, + limit: pageSize, + order: [ + ['created_at', order], + ], + where:{ + is_draft:filter + } + }); + + res.json({ + page, + pageSize, + totalCount, + data: publis, + }); + } +}).catch(function (err) { + log.error(err); + return next(err); }); pubApp.get('/:id', async (req, res, next) => {