diff --git a/src/libs/db/postgres.js b/src/libs/db/postgres.js index c60d28c6932491dbee5ba88a5f86412d707cda23..2e16df38c760a208e13d79b4bfb0090534d23d0b 100644 --- a/src/libs/db/postgres.js +++ b/src/libs/db/postgres.js @@ -1,6 +1,5 @@ const Sequelize = require('sequelize'); -// if you are using postgres, your DB URL will look like this const DATABASE_URL = 'postgres://postgres:postgres@localhost:5432/postgres' const db = new Sequelize(DATABASE_URL) diff --git a/src/libs/routes_v1/activity.js b/src/libs/routes_v1/activity.js index 51ef67ea1ae60d35636dc42f3770f62b0fd9b29b..d1742dd9ae77e013830e8d7997114e881edfb927 100644 --- a/src/libs/routes_v1/activity.js +++ b/src/libs/routes_v1/activity.js @@ -28,16 +28,71 @@ function emailSyntax(email) { } activityApp.get('/', async (req, res, next) => { - const page = parseInt(req.query.page) || 1; - const pageSize = parseInt(req.query.pageSize) || 5; - try { + const ordenar = req.query.order || 'DESC'; + 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 Activity.count(); const offset = (page - 1) * pageSize; const acts = await Activity.findAll({ offset, limit: pageSize, - order:[ + order: [ + ['date', ordenar], + ], + }); + + res.json({ + page, + pageSize, + totalCount, + data: acts, + }); + } + else { + const totalCount = await Activity.count({ where: { is_draft: filter } }); + const offset = (page - 1) * pageSize; + + const acts = await Activity.findAll({ + offset, + limit: pageSize, + order: [ + ['date', ordenar], + ], + where: { + is_draft: filter + } + }); + + res.json({ + page, + pageSize, + totalCount, + data: acts, + }); + } +}); + +activityApp.get('/drafts', async (req, res, next) => { + const page = parseInt(req.query.page) || 1; + const pageSize = parseInt(req.query.pageSize) || 5; + try { + const totalCount = await Activity.count({ + where: { + is_draft: true + } + }); + const offset = (page - 1) * pageSize; + + const acts = await Activity.findAll({ + offset, + limit: pageSize, + where: { + is_draft: true + }, + order: [ ['date', 'DESC']] }); @@ -53,39 +108,8 @@ activityApp.get('/', async (req, res, next) => { } }); -activityApp.get('/drafts', async (req, res, next) => { - const page = parseInt(req.query.page) || 1; - const pageSize = parseInt(req.query.pageSize) || 5; - try { - const totalCount = await Activity.count({where: { - is_draft: true - }}); - const offset = (page - 1) * pageSize; - - const acts = await Activity.findAll({ - offset, - limit: pageSize, - where: { - is_draft: true - }, - order:[ - ['date', 'DESC']] - }); - - res.json({ - page, - pageSize, - totalCount, - data: acts, - }); - } catch (error) { - console.error(error); - res.status(500).json({ error: 'An error occurred' }); - } - }); - activityApp.get('/:id', (req, res, next) => { - Activity.findByPk(req.params.id).then((act) => { + Activity.findByPk(req.params.id).then((act) => { if (!act) { res.statusCode = 404; res.json({ msg: "A atividade não está cadastrada" }); @@ -121,11 +145,11 @@ activityApp.post('/', passport.authenticate('bearer', { session: false }), autho console.log(req.body); let act = await Activity.create({ id: 0, - type:req.body.tipo, + type: req.body.tipo, title: req.body.titulo, subtitle: req.body.subtitulo, date: transformDateFormat(req.body.dataDePostagem), - authors:req.body.autor, + authors: req.body.autor, text: req.body.texto, name_headline: req.body.nome, resume_headline: req.body.resumo, @@ -145,7 +169,7 @@ activityApp.post('/', passport.authenticate('bearer', { session: false }), autho return res.json({ err, errors }); // handle error; }); - if(!act){ + if (!act) { res.statusCode = 400; return res; } @@ -174,14 +198,14 @@ activityApp.put('/:id', passport.authenticate('bearer', { session: false }), aut act.subtitle = req.body.subtitle || act.subtitle; act.date = req.body.date || act.date; act.authors = req.body.autores || act.authors; - act.text= req.body.text || act.text; - act.name_headline= req.body.name_headline || act.name_headline; - act.resume_headline= req.body.resume_headline || act.resume_headline; - act.date_headline= req.body.date_headline || act.date_headline; - act.local_headline= req.body.local_headline || act.local_headline; - act.additional_headline= req.body.additional_headline || act.additional_headline; - act.is_draft= req.body.is_draft || act.is_draft; - act.is_headline= req.body.is_headline || act.is_headline; + act.text = req.body.text || act.text; + act.name_headline = req.body.name_headline || act.name_headline; + act.resume_headline = req.body.resume_headline || act.resume_headline; + act.date_headline = req.body.date_headline || act.date_headline; + act.local_headline = req.body.local_headline || act.local_headline; + act.additional_headline = req.body.additional_headline || act.additional_headline; + act.is_draft = req.body.is_draft || act.is_draft; + act.is_headline = req.body.is_headline || act.is_headline; act.save().catch(err => { if (err) { @@ -190,17 +214,17 @@ activityApp.put('/:id', passport.authenticate('bearer', { session: false }), aut } }) let activity = act.toJSON(); - res.json({ activity: activity}); + res.json({ activity: activity }); }); activityApp.delete('/:id', passport.authenticate('bearer', { session: false }), authorized('editar atividade'), async (req, res, next) => { - await Activity.destroy({where:{id:req.params.id}}).catch(function (err) { + await Activity.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: 'Activity Deleted', status: 200 }); }); diff --git a/src/libs/routes_v1/publication.js b/src/libs/routes_v1/publication.js index 1cad804bc86a4341471196abdf88c760a12e9f34..8fdc8c969b788dc3b918334f45bdcd4c2e2d9303 100644 --- a/src/libs/routes_v1/publication.js +++ b/src/libs/routes_v1/publication.js @@ -156,6 +156,7 @@ pubApp.post('/edit', passport.authenticate('bearer', { session: false }), author pb.link = data.link || pb.link; pb.is_homepage = data.homepage || pb.is_homepage; pb.is_draft = data.rascunho || pb.is_draft; + pb.upload = _file_id || pb.upload; pb.save().catch(function (err) { log.error(err); @@ -173,43 +174,6 @@ pubApp.post('/edit', passport.authenticate('bearer', { session: false }), author next(); }, response('publication')); -pubApp.put('/edit/:id', passport.authenticate('bearer', { session: false }), authorized('editar publicacao'), async (req, res, next) => { - console.log(req); - let pb = await Publication.findByPk(req.params.id).catch(function (err) { - if (err) { - log.error(err); - return next({ err }); - } - }) - if (!pb) { - res.statusCode = 404; - return next({ - err: { - message: 'Publicação não encontrada' - } - }); - } - pb.filter = req.body.categoria || pb.filter; - pb.title = req.body.title || pb.title; - pb.authors = req.body.autores || pb.authors; - pb.organization= req.body.organizacao || pb.organization; - pb.year= req.body.ano || pb.year; - pb.text= req.body.texto || pb.text; - pb.link= req.body.link || pb.link; - pb.upload= req.body.upload || pb.upload; - pb.is_homepage= req.body.homepage || pb.is_homepage; - console.log(pb); - pb.save().catch(err => { - if (err) { - log.error(err); - return next({ message: 'Erro ao atualizar publicacao' }); - } - }) - let p = pb.toJSON(); - res.json({ publication: p }); - -}); - 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) { if (err) {