Skip to content
Snippets Groups Projects
Commit 850159d2 authored by ems19's avatar ems19
Browse files

[ADD] New routes

parent a56bc9e0
No related branches found
No related tags found
4 merge requests!379homologa -> master,!378dev -> homologa,!375Prd migration,!374Prd migration
......@@ -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 });
});
......
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