From 21934286099b4964b9ed3ef714f004d6eda88ae1 Mon Sep 17 00:00:00 2001 From: Naida Vatric Date: Thu, 31 Oct 2019 01:11:25 +0100 Subject: [PATCH] Introduced help fnc for database --- endpoints/addpermission.js | 48 ++++++++++++++++++++++++++++++++++++- endpoints/addusertogroup.js | 28 ++++++---------------- helpfunctions/addgroup.js | 27 +++++++++++++++++++++ helpfunctions/addobject.js | 26 ++++++++++++++++++++ helpfunctions/adduser.js | 26 ++++++++++++++++++++ 5 files changed, 133 insertions(+), 22 deletions(-) create mode 100644 helpfunctions/addgroup.js create mode 100644 helpfunctions/addobject.js create mode 100644 helpfunctions/adduser.js diff --git a/endpoints/addpermission.js b/endpoints/addpermission.js index 6416bf2..2c96778 100644 --- a/endpoints/addpermission.js +++ b/endpoints/addpermission.js @@ -1,9 +1,55 @@ //Call for adding a permission to a group or user // +//Requiremenst for inserting data in DB +const adduser= require('../helpfunctions/adduser'); +const addgroup= require('../helpfunctions/addgroup'); +const addobject= require('../helpfunctions/addobject'); + const handleAddPermission = (req, res, db) => { //Parsing req body const { reqowner, reqownertype, reqobjname, reqtype} =req.body; - res.json('Trying to add a permission'); + //Checking if req input combination existis in DB table permissions + //if yes returns error if no inserts permission in DB table + db.transaction (trx => { + trx.select('objname') + .from('permissions') + .where('owner', 'ilike', reqowner) + .andWhere('ownertype','ilike', reqownertype) + .andWhere('objname', 'ilike', reqobjname) + .andWhere('type', 'ilike', reqtype) + .then( data => { + if (data.length===0) { + return trx('permissions') + .returning('*') + .insert({ + owner: reqowner, + ownertype: reqownertype, + objname: reqobjname, + type: reqtype + }) + .into('permissions') + .then( permission => { + res.json(permission[0]); + }) + } + return res.status(400).json('This permission already exists.'); + }) + .then(trx.commit) + .catch(trx.rollback) + .catch (err => res.status(400).json('Error accesing database.')) + }) + if (reqownertype.toLowerCase()==='user') { + //Inserts new user (implicitly) if needed + adduser.handleAddUser(reqowner, db); + } + if (reqownertype.toLowerCase()==='group') { + //Inserts new group (implicitly) if needed + addgroup.handleAddGroup(reqowner, db); + } + //Inserts new object (implicitly) if needed + addobject.handleAddObject(reqobjname, db); + + } module.exports = { diff --git a/endpoints/addusertogroup.js b/endpoints/addusertogroup.js index a1ce792..6d3e80b 100644 --- a/endpoints/addusertogroup.js +++ b/endpoints/addusertogroup.js @@ -1,5 +1,8 @@ //Call for adding a user to a group // +//Requiremenst for inserting data in DB +const adduser= require('../helpfunctions/adduser'); + const handleAddUserToGroup = (req, res, db) => { //Parsing req body const { reqgroup, requser} =req.body; @@ -29,27 +32,10 @@ const handleAddUserToGroup = (req, res, db) => { .catch(trx.rollback) .catch (err => res.status(400).json('Error accesing database.')) }) - //Checking if user existis in DB table users - //if no inserts new user (implicitly) - db.transaction (trx => { - trx.select('username') - .from('users') - .where('username', 'ilike', requser) - .then ( data => { - if (data.length===0) { - return trx('users') - .returning('*') - .insert( { - username: requser - }) - .then(user => { - console.log('Inserted new user implicitly.'); - }) - } - }) - .then(trx.commit) - .catch(trx.rollback) - }) + + //Inserts new user (implicitly) if needed + adduser.handleAddUser(requser, db); + } module.exports = { diff --git a/helpfunctions/addgroup.js b/helpfunctions/addgroup.js new file mode 100644 index 0000000..e2a7387 --- /dev/null +++ b/helpfunctions/addgroup.js @@ -0,0 +1,27 @@ +//Checking if group existis in DB table groups +//if no inserts new group with no users (implicitly) +const handleAddGroup = (reqgroup, db) => { + db.transaction (trx => { + trx.select('groupname') + .from('groups') + .where('groupname', 'ilike', reqgroup) + .then ( data => { + if (data.length===0) { + return trx('groups') + .returning('*') + .insert( { + groupname: reqgroup, + username: '' + }) + .then(group => { + console.log('Inserted new empty group implicitly.'); + }) + } + }) + .then(trx.commit) + .catch(trx.rollback) + }) +} +module.exports = { + handleAddGroup +} \ No newline at end of file diff --git a/helpfunctions/addobject.js b/helpfunctions/addobject.js new file mode 100644 index 0000000..c29d436 --- /dev/null +++ b/helpfunctions/addobject.js @@ -0,0 +1,26 @@ +//Checking if object existis in DB table objects +//if no inserts new object (implicitly) +const handleAddObject = (reqobjname, db) => { + db.transaction (trx => { + trx.select('objname') + .from('objects') + .where('objname', 'ilike', reqobjname) + .then ( data => { + if (data.length===0) { + return trx('objects') + .returning('*') + .insert( { + objname: reqobjname + }) + .then(obj => { + console.log('Inserted new object implicitly.'); + }) + } + }) + .then(trx.commit) + .catch(trx.rollback) + }) +} +module.exports = { + handleAddObject +} \ No newline at end of file diff --git a/helpfunctions/adduser.js b/helpfunctions/adduser.js new file mode 100644 index 0000000..f6c2196 --- /dev/null +++ b/helpfunctions/adduser.js @@ -0,0 +1,26 @@ +//Checking if user existis in DB table users +//if no inserts new user (implicitly) +const handleAddUser = (requser, db) => { + db.transaction (trx => { + trx.select('username') + .from('users') + .where('username', 'ilike', requser) + .then ( data => { + if (data.length===0) { + return trx('users') + .returning('*') + .insert( { + username: requser + }) + .then(user => { + console.log('Inserted new user implicitly.'); + }) + } + }) + .then(trx.commit) + .catch(trx.rollback) + }) +} +module.exports = { + handleAddUser +} \ No newline at end of file