- Added Delete Record to the CGridTableRec

This commit is contained in:
Paolo Arena
2019-10-15 20:40:31 +02:00
parent 9120485939
commit 51ef3de29a
5 changed files with 82 additions and 13 deletions

View File

@@ -149,20 +149,22 @@ router.post(process.env.LINK_UPDATE_PASSWORD, (req, res) => {
});
router.post('/gettable', authenticate, (req, res) => {
const params = req.body;
function getTableByTableName(tablename) {
tools.mylog('GET ALL USERS: ', params);
let mytable = null;
if (params.table === 'users')
if (tablename === 'users')
mytable = User;
else if (params.table === 'booking')
else if (tablename === 'booking')
mytable = Booking;
return mytable
}
router.post('/gettable', authenticate, (req, res) => {
const params = req.body;
const mytable = getTableByTableName(params.table);
return mytable.queryTable(req.user.idapp, params).then(ris => {
tools.mylog('list', ris);
// tools.mylog('list', ris);
return res.send(ris);
}).catch((e) => {
console.log(e);
@@ -171,4 +173,62 @@ router.post('/gettable', authenticate, (req, res) => {
});
router.patch('/chval', authenticate, (req, res) => {
// const idapp = req.body.idapp;
const id = req.body.data.id;
const mydata = req.body.data;
const mytable = getTableByTableName(mydata.table);
const fieldsvalue = mydata.fieldsvalue;
tools.mylogshow('PATCH CHVAL: ', id);
if (!User.isAdmin(req.user) && !User.isManager(req.user)) {
// If without permissions, exit
return res.status(404).send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' });
}
mytable.findByIdAndUpdate(id, { $set: fieldsvalue }).then((rec) => {
tools.mylogshow(' REC TO MODIFY: ', rec);
if (!rec) {
return res.status(404).send();
} else {
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
}
}).catch((e) => {
tools.mylogserr('Error patch USER: ', e);
res.status(400).send();
})
});
router.delete('/delrec/:table/:id', authenticate, (req, res) => {
const id = req.params.id;
const tablename = req.params.table;
// const idapp = req.body.idapp;
console.log('id', id , 'table', tablename);
const mytable = getTableByTableName(tablename);
if (!User.isAdmin(req.user) && !User.isManager(req.user)) {
// If without permissions, exit
return res.status(404).send({ code: server_constants.RIS_CODE_ERR_UNAUTHORIZED, msg: '' });
}
mytable.findByIdAndRemove(id).then((rec) => {
if (!rec) {
return res.status(404).send();
}
tools.mylog('DELETED ', rec._id);
res.send({ code: server_constants.RIS_CODE_OK, msg: '' });
}).catch((e) => {
res.status(400).send();
});
});
module.exports = router;