From d6d5e3678f6418edbba42e4a842b073565bfc2d0 Mon Sep 17 00:00:00 2001
From: Eduardo Mathias <ems19@inf.ufpr.br>
Date: Thu, 27 Jul 2023 10:45:13 -0300
Subject: [PATCH] [Fix] Pub route

---
 src/libs/routes_v1/publication.js | 67 +++++++++++++++++++------------
 1 file changed, 41 insertions(+), 26 deletions(-)

diff --git a/src/libs/routes_v1/publication.js b/src/libs/routes_v1/publication.js
index 4aece71a..1b2e24e5 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) => {
-- 
GitLab