Introduced help fnc for database

This commit is contained in:
Naida Vatric
2019-10-31 01:11:25 +01:00
parent 46d32196d9
commit 2193428609
5 changed files with 133 additions and 22 deletions

View File

@@ -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 = {

View File

@@ -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 = {

27
helpfunctions/addgroup.js Normal file
View File

@@ -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
}

View File

@@ -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
}

26
helpfunctions/adduser.js Normal file
View File

@@ -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
}