From 850159d2d6d9777c7f920645e1e0fd390577bd4f Mon Sep 17 00:00:00 2001
From: Eduardo Mathias <ems19@inf.ufpr.br>
Date: Thu, 31 Aug 2023 10:38:30 -0300
Subject: [PATCH] [ADD] New routes

---
 src/libs/routes_v1/publication.js | 186 ++++++++++++++++++++----------
 1 file changed, 127 insertions(+), 59 deletions(-)

diff --git a/src/libs/routes_v1/publication.js b/src/libs/routes_v1/publication.js
index 441b8674..63a7cbd5 100644
--- a/src/libs/routes_v1/publication.js
+++ b/src/libs/routes_v1/publication.js
@@ -31,61 +31,127 @@ function emailSyntax(email) {
 
 pubApp.get('/', async (req, res, next) => {
   const ordenar = req.query.order || 'DESC';
-  const filter = req.query.filter || 'all' ;
+  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
-  if(filter === 'all'){
-      const totalCount = await Publication.count();
-      const offset = (page - 1) * pageSize;
-
-      const publis = await Publication.findAll({
-        offset,
-        limit: pageSize,
-        order: [
-          ['created_at', ordenar],
-        ],
-      });
-
-      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', ordenar],
-        ],
-        where:{
-          is_draft:filter
-        }
-      });
-
-      res.json({
-        page,
-        pageSize,
-        totalCount,
-        data: publis,
-      });
+  if (filter === 'all') {
+    const totalCount = await Publication.count();
+    const offset = (page - 1) * pageSize;
+
+    const publis = await Publication.findAll({
+      offset,
+      limit: pageSize,
+      order: [
+        ['created_at', ordenar],
+      ],
+    });
+
+    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', ordenar],
+      ],
+      where: {
+        is_draft: filter
+      }
+    });
+
+    res.json({
+      page,
+      pageSize,
+      totalCount,
+      data: publis,
+    });
+  }
+});
+
+pubApp.get('/homepage', async (req, res, next)=> {
+  const totalCount = await Publication.count({ where: { is_headline: true } });
+  const offset = (page - 1) * pageSize;
+  const publis = await Publication.findAll({
+    offset,
+    limit: pageSize, 
+    order: [
+      ['created_at', ordenar],
+    ],
+    where:{
+      is_headline:true
     }
+  });
+  res.json({
+    page,
+    pageSize,
+    totalCount,
+    data: publis,
+  });
+});
+
+pubApp.get('/type/:tp', async (req, res, next) => {
+  const tp = req.params.tp || 'all';
+  if(tp === 'all'){
+    const totalCount = await Publication.count();
+    const offset = (page - 1) * pageSize;
+
+    const publis = await Publication.findAll({
+      offset,
+      limit: pageSize,
+      order: [
+        ['created_at', ordenar],
+      ],
+    });
+
+    res.json({
+      page,
+      pageSize,
+      totalCount,
+      data: publis,
+    });
+  }
+  else{
+    const totalCount = await Publication.count({ where: { filter: req.params.tp } });
+    const offset = (page - 1) * pageSize;
+    const publis = await Publication.findAll({
+      offset,
+      limit: pageSize,
+      order: [
+        ['created_at', ordenar],
+      ],
+      where: {
+        filter: req.params.tp
+      }
+    });
+    res.json({
+      page,
+      pageSize,
+      totalCount,
+      data: publis,
+    });
+  }
 });
 
 
 pubApp.get('/:id', async (req, res, next) => {
   let pb = await Publication.findByPk(req.params.id).catch(function (err) {
     log.error(err);
-    return next(err);}
+    return next(err);
+  }
   );
   if (!pb) {
-      res.statusCode = 404;
-      res.json({ msg: "A publicação não está cadastrada" });
-    } 
+    res.statusCode = 404;
+    res.json({ msg: "A publicação não está cadastrada" });
+  }
   else {
     let publ = pb.toJSON();
     publ.Filename = null;
@@ -93,20 +159,21 @@ pubApp.get('/:id', async (req, res, next) => {
       log.error(err);
       return next(err);
     });
-    if(file_){
+    if (file_) {
       publ.Filename = file_.name;
-      }
+    }
     req.result = publ;
     next();
-    }
-  }, response('publication'));
+  }
+}, response('publication'));
 
 pubApp.post('/', passport.authenticate('bearer', { session: false }), authorized('criar publicacao'), upload.single('file'), async (req, res, next) => {
   let _file_id = null
-  if(req.file){
+  if (req.file) {
     _file_id = await fileWorker.uploadFile(req.file);
-    if(!_file_id)
-      console.log("NAO ARQUIVO");}
+    if (!_file_id)
+      console.log("NAO ARQUIVO");
+  }
   let data = JSON.parse(req.body.data);
   let pb = await Publication.create({
     id: 0,
@@ -139,10 +206,11 @@ pubApp.post('/', passport.authenticate('bearer', { session: false }), authorized
 
 pubApp.post('/edit', passport.authenticate('bearer', { session: false }), authorized('editar publicacao'), upload.single('file'), async (req, res, next) => {
   let _file_id = null
-  if(req.file){
+  if (req.file) {
     _file_id = await fileWorker.uploadFile(req.file);
-    if(!_file_id)
-      console.log("NAO ARQUIVO");}
+    if (!_file_id)
+      console.log("NAO ARQUIVO");
+  }
   let data = JSON.parse(req.body.data);
   console.log(data);
   let pb = await Publication.findByPk(data.id).catch(function (err) {
@@ -161,7 +229,7 @@ pubApp.post('/edit', passport.authenticate('bearer', { session: false }), author
   pb.upload = _file_id || pb.upload;
   pb.volume = data.volume || pb.volume;
   pb.pages = data.pagina || pb.pages;
-  
+
   pb.save().catch(function (err) {
     log.error(err);
     let errors = [];
@@ -179,12 +247,12 @@ pubApp.post('/edit', passport.authenticate('bearer', { session: false }), author
 }, response('publication'));
 
 pubApp.delete('/:id', passport.authenticate('bearer', { session: false }), authorized('apagar publicacao'), async (req, res, next) => {
-  await Publication.destroy({where:{id:req.params.id}}).catch(function (err) {
+  await Publication.destroy({ where: { id: req.params.id } }).catch(function (err) {
     if (err) {
-        log.error(err);
-        return next({ err });
+      log.error(err);
+      return next({ err });
     }
-});
+  });
   return next({ msg: 'Publication Deleted', status: 200 });
 });
 
-- 
GitLab