Skip to content
Snippets Groups Projects
Commit da7632ee authored by Fernando Gbur dos Santos's avatar Fernando Gbur dos Santos
Browse files

Merge branch 'homologa' into 'master'

homologa -> master

See merge request !379
parents a8dcf023 01f31301
No related branches found
No related tags found
3 merge requests!408Development,!407Master,!379homologa -> master
...@@ -26,3 +26,5 @@ docs/ ...@@ -26,3 +26,5 @@ docs/
package-lock.json package-lock.json
Dockerfile Dockerfile
DockerfileAntigo
src/libs/db/postgres.js
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)
module.exports = db
\ No newline at end of file
...@@ -21,7 +21,11 @@ var Activity = db.define("Activity",{ ...@@ -21,7 +21,11 @@ var Activity = db.define("Activity",{
type: Sequelize.STRING type: Sequelize.STRING
}, },
date:{ date:{
type: Sequelize.STRING, type: Sequelize.DATE,
allowNull:false
},
date_final:{
type: Sequelize.DATE,
allowNull:false allowNull:false
}, },
authors:{ authors:{
......
...@@ -61,6 +61,12 @@ var Publication = db.define("Publication",{ ...@@ -61,6 +61,12 @@ var Publication = db.define("Publication",{
type:Sequelize.BOOLEAN, type:Sequelize.BOOLEAN,
allowNull: false, allowNull: false,
defaultValue: false defaultValue: false
},
volume:{
type:Sequelize.STRING
},
pages:{
type:Sequelize.STRING
} }
},{timestamp:true, },{timestamp:true,
createdAt: 'created_at', createdAt: 'created_at',
......
...@@ -28,16 +28,71 @@ function emailSyntax(email) { ...@@ -28,16 +28,71 @@ function emailSyntax(email) {
} }
activityApp.get('/', async (req, res, next) => { activityApp.get('/', async (req, res, next) => {
const page = parseInt(req.query.page) || 1; const ordenar = req.query.order || 'DESC';
const pageSize = parseInt(req.query.pageSize) || 5; const filter = req.query.filter || 'all';
try { 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 totalCount = await Activity.count();
const offset = (page - 1) * pageSize; const offset = (page - 1) * pageSize;
const acts = await Activity.findAll({ const acts = await Activity.findAll({
offset, offset,
limit: pageSize, 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']] ['date', 'DESC']]
}); });
...@@ -53,44 +108,19 @@ activityApp.get('/', async (req, res, next) => { ...@@ -53,44 +108,19 @@ 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) => { activityApp.get('/:id', (req, res, next) => {
Activity.findByPk(req.params.id).then((act) => { Activity.findByPk(req.params.id).then((act) => {
if (!act) { if (!act) {
res.statusCode = 404; res.statusCode = 404;
res.json({ msg: "A atividade não está cadastrada" }); res.json({ msg: "A atividade não está cadastrada" });
} else { } else {
req.result = act.toJSON(); let actJSON = act.toJSON();
//transform data yyyy-mm-dd to dd/mm/yyyy
let date = actJSON.date.split('-');
let date_headline = actJSON.date_headline.split('-');
actJSON.date = date[2] + '/' + date[1] + '/' + date[0];
actJSON.date_headline = date_headline[2] + '/' + date_headline[1] + '/' + date_headline[0];
req.result = actJSON;
next(); next();
} }
}).catch(function (err) { }).catch(function (err) {
...@@ -121,11 +151,11 @@ activityApp.post('/', passport.authenticate('bearer', { session: false }), autho ...@@ -121,11 +151,11 @@ activityApp.post('/', passport.authenticate('bearer', { session: false }), autho
console.log(req.body); console.log(req.body);
let act = await Activity.create({ let act = await Activity.create({
id: 0, id: 0,
type:req.body.tipo, type: req.body.tipo,
title: req.body.titulo, title: req.body.titulo,
subtitle: req.body.subtitulo, subtitle: req.body.subtitulo,
date: transformDateFormat(req.body.dataDePostagem), date: transformDateFormat(req.body.dataDePostagem),
authors:req.body.autor, authors: req.body.autor,
text: req.body.texto, text: req.body.texto,
name_headline: req.body.nome, name_headline: req.body.nome,
resume_headline: req.body.resumo, resume_headline: req.body.resumo,
...@@ -145,7 +175,7 @@ activityApp.post('/', passport.authenticate('bearer', { session: false }), autho ...@@ -145,7 +175,7 @@ activityApp.post('/', passport.authenticate('bearer', { session: false }), autho
return res.json({ err, errors }); return res.json({ err, errors });
// handle error; // handle error;
}); });
if(!act){ if (!act) {
res.statusCode = 400; res.statusCode = 400;
return res; return res;
} }
...@@ -169,19 +199,19 @@ activityApp.put('/:id', passport.authenticate('bearer', { session: false }), aut ...@@ -169,19 +199,19 @@ activityApp.put('/:id', passport.authenticate('bearer', { session: false }), aut
}); });
} }
console.log("TEste"); console.log("TEste");
act.type = req.body.type || act.type; act.type = req.body.tipo || act.type;
act.title = req.body.title || act.title; act.title = req.body.titulo || act.title;
act.subtitle = req.body.subtitle || act.subtitle; act.subtitle = req.body.subtitulo || act.subtitle;
act.date = req.body.date || act.date; act.date = req.body.dataDePostagem || act.date;
act.authors = req.body.autores || act.authors; act.authors = req.body.autor || act.authors;
act.text= req.body.text || act.text; act.text = req.body.texto || act.text;
act.name_headline= req.body.name_headline || act.name_headline; act.name_headline = req.body.nome || act.name_headline;
act.resume_headline= req.body.resume_headline || act.resume_headline; act.resume_headline = req.body.resumo || act.resume_headline;
act.date_headline= req.body.date_headline || act.date_headline; act.date_headline = req.body.dataAtividade || act.date_headline;
act.local_headline= req.body.local_headline || act.local_headline; act.local_headline = req.body.local || act.local_headline;
act.additional_headline= req.body.additional_headline || act.additional_headline; act.additional_headline = req.body.informacoes || act.additional_headline;
act.is_draft= req.body.is_draft || act.is_draft; act.is_draft = req.body.rascunho || act.is_draft;
act.is_headline= req.body.is_headline || act.is_headline; //act.is_headline = req.body.is_headline || act.is_headline;
act.save().catch(err => { act.save().catch(err => {
if (err) { if (err) {
...@@ -190,17 +220,17 @@ activityApp.put('/:id', passport.authenticate('bearer', { session: false }), aut ...@@ -190,17 +220,17 @@ activityApp.put('/:id', passport.authenticate('bearer', { session: false }), aut
} }
}) })
let activity = act.toJSON(); 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) => { 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) { if (err) {
log.error(err); log.error(err);
return next({ err }); return next({ err });
} }
}); });
return next({ msg: 'Activity Deleted', status: 200 }); return next({ msg: 'Activity Deleted', status: 200 });
}); });
......
...@@ -30,15 +30,20 @@ function emailSyntax(email) { ...@@ -30,15 +30,20 @@ function emailSyntax(email) {
} }
pubApp.get('/', async (req, res, next) => { pubApp.get('/', async (req, res, next) => {
const ordenar = req.query.order || 'DESC';
const filter = req.query.filter || 'all';
const page = parseInt(req.query.page) || 1; // Current page number const page = parseInt(req.query.page) || 1; // Current page number
const pageSize = parseInt(req.query.pageSize) || 5; // Number of items per page const pageSize = parseInt(req.query.pageSize) || 5; // Number of items per page
try { if (filter === 'all') {
const totalCount = await Publication.count(); const totalCount = await Publication.count();
const offset = (page - 1) * pageSize; const offset = (page - 1) * pageSize;
const publis = await Publication.findAll({ const publis = await Publication.findAll({
offset, offset,
limit: pageSize, limit: pageSize,
order: [
['created_at', ordenar],
],
}); });
res.json({ res.json({
...@@ -47,43 +52,92 @@ pubApp.get('/', async (req, res, next) => { ...@@ -47,43 +52,92 @@ pubApp.get('/', async (req, res, next) => {
totalCount, totalCount,
data: publis, data: publis,
}); });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'An error occurred' });
} }
}); else {
const totalCount = await Publication.count({ where: { is_draft: filter } });
const offset = (page - 1) * pageSize;
pubApp.get('/drafts', async (req, res, next) => { const publis = await Publication.findAll({
const page = parseInt(req.query.page) || 1; // Current page number offset,
const pageSize = parseInt(req.query.pageSize) || 5; // Number of items per page limit: pageSize,
order: [
['created_at', ordenar],
],
where: {
is_draft: filter
}
});
try { res.json({
// Count total number of items page,
const totalCount = await Publication.count({ where: { pageSize,
is_draft: true 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,
});
});
// Calculate offset based on page and pageSize 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 offset = (page - 1) * pageSize;
// Query the database with pagination options const publis = await Publication.findAll({
const drafts = await Publication.findAll({
offset, offset,
limit: pageSize, 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: { where: {
is_draft: true filter: req.params.tp
} }
}); });
res.json({ res.json({
page, page,
pageSize, pageSize,
totalCount, totalCount,
data: drafts, data: publis,
}); });
} catch (error) {
console.error(error);
res.status(500).json({ error: 'An error occurred' });
} }
}); });
...@@ -91,12 +145,13 @@ pubApp.get('/drafts', async (req, res, next) => { ...@@ -91,12 +145,13 @@ pubApp.get('/drafts', async (req, res, next) => {
pubApp.get('/:id', async (req, res, next) => { pubApp.get('/:id', async (req, res, next) => {
let pb = await Publication.findByPk(req.params.id).catch(function (err) { let pb = await Publication.findByPk(req.params.id).catch(function (err) {
log.error(err); log.error(err);
return next(err);} return next(err);
}
); );
if (!pb) { if (!pb) {
res.statusCode = 404; res.statusCode = 404;
res.json({ msg: "A publicação não está cadastrada" }); res.json({ msg: "A publicação não está cadastrada" });
} }
else { else {
let publ = pb.toJSON(); let publ = pb.toJSON();
publ.Filename = null; publ.Filename = null;
...@@ -104,20 +159,21 @@ pubApp.get('/:id', async (req, res, next) => { ...@@ -104,20 +159,21 @@ pubApp.get('/:id', async (req, res, next) => {
log.error(err); log.error(err);
return next(err); return next(err);
}); });
if(file_){ if (file_) {
publ.Filename = file_.name; publ.Filename = file_.name;
} }
req.result = publ; req.result = publ;
next(); next();
} }
}, response('publication')); }, response('publication'));
pubApp.post('/', passport.authenticate('bearer', { session: false }), authorized('criar publicacao'), upload.single('file'), async (req, res, next) => { pubApp.post('/', passport.authenticate('bearer', { session: false }), authorized('criar publicacao'), upload.single('file'), async (req, res, next) => {
let _file_id = null let _file_id = null
if(req.file){ if (req.file) {
_file_id = await fileWorker.uploadFile(req.file); _file_id = await fileWorker.uploadFile(req.file);
if(!_file_id) if (!_file_id)
console.log("NAO ARQUIVO");} console.log("NAO ARQUIVO");
}
let data = JSON.parse(req.body.data); let data = JSON.parse(req.body.data);
let pb = await Publication.create({ let pb = await Publication.create({
id: 0, id: 0,
...@@ -130,7 +186,9 @@ pubApp.post('/', passport.authenticate('bearer', { session: false }), authorized ...@@ -130,7 +186,9 @@ pubApp.post('/', passport.authenticate('bearer', { session: false }), authorized
link: data.link, link: data.link,
upload: _file_id, upload: _file_id,
is_draft: data.rascunho, is_draft: data.rascunho,
is_homepage: data.homepage is_homepage: data.homepage,
volume: data.volume,
pages: data.pagina
}).catch(function (err) { }).catch(function (err) {
log.error(err); log.error(err);
let errors = []; let errors = [];
...@@ -148,60 +206,53 @@ pubApp.post('/', passport.authenticate('bearer', { session: false }), authorized ...@@ -148,60 +206,53 @@ 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) => { pubApp.post('/edit', passport.authenticate('bearer', { session: false }), authorized('editar publicacao'), upload.single('file'), async (req, res, next) => {
let _file_id = null let _file_id = null
if(req.file){ if (req.file) {
_file_id = await fileWorker.uploadFile(req.file); _file_id = await fileWorker.uploadFile(req.file);
if(!_file_id) if (!_file_id)
console.log("NAO ARQUIVO");} console.log("NAO ARQUIVO");
}
let data = JSON.parse(req.body.data); let data = JSON.parse(req.body.data);
console.log(data); console.log(data);
req.response = {'This is a test':'This is a test'}; let pb = await Publication.findByPk(data.id).catch(function (err) {
next(); log.error(err);
}, response('publication')); return next(err);
});
pb.filter = data.categoria || pb.filter;
pb.title = data.title || pb.title;
pb.authors = data.autores || pb.authors;
pb.organization = data.organizacao || pb.organization;
pb.year = data.ano || pb.year;
pb.text = data.texto || pb.text;
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.volume = data.volume || pb.volume;
pb.pages = data.pagina || pb.pages;
pubApp.put('/edit/:id', passport.authenticate('bearer', { session: false }), authorized('editar publicacao'), async (req, res, next) => { pb.save().catch(function (err) {
console.log(req); log.error(err);
let pb = await Publication.findByPk(req.params.id).catch(function (err) { let errors = [];
if (err) { for (let errName in err.errors) {
log.error(err); errors.push(err.errors[errName].message);
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' });
} }
}) log.error(errors);
let p = pb.toJSON(); res.statusCode = 400;
res.json({ publication: p }); return res.json({ err, errors });
// handle error;
});
}); req.result = pb.toJSON();
next();
}, response('publication'));
pubApp.delete('/:id', passport.authenticate('bearer', { session: false }), authorized('apagar publicacao'), async (req, res, next) => { 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) { if (err) {
log.error(err); log.error(err);
return next({ err }); return next({ err });
} }
}); });
return next({ msg: 'Publication Deleted', status: 200 }); return next({ msg: 'Publication Deleted', status: 200 });
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment