Initial commit

This commit is contained in:
Naida Vatric
2019-10-30 23:45:40 +01:00
parent 2948ac6c38
commit 46d32196d9
6 changed files with 3406 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
//Call for adding a permission to a group or user
//
const handleAddPermission = (req, res, db) => {
//Parsing req body
const { reqowner, reqownertype, reqobjname, reqtype} =req.body;
res.json('Trying to add a permission');
}
module.exports = {
handleAddPermission
}

View File

@@ -0,0 +1,60 @@
//Call for adding a user to a group
//
const handleAddUserToGroup = (req, res, db) => {
//Parsing req body
const { reqgroup, requser} =req.body;
//Checking if user-group combination existis in DB table groups
//if yes returns error if no inserts in DB table
db.transaction (trx => {
trx.select('groupname')
.from('groups')
.where('groupname', 'ilike', reqgroup)
.andWhere('username','ilike', requser)
.then( data => {
if (data.length===0) {
return trx('groups')
.returning('*')
.insert({
groupname: reqgroup,
username: requser
})
.into('groups')
.then( group => {
res.json(group[0]);
})
}
return res.status(400).json('This user already exists in this group.');
})
.then(trx.commit)
.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)
})
}
module.exports = {
handleAddUserToGroup
}

23
endpoints/cleargroup.js Normal file
View File

@@ -0,0 +1,23 @@
//Call for clearing all users from group
//
const handleClearGroup = (req, res, db) => {
//Parsing req body
const { reqgroup} =req.body;
//Checking for groupname in table groups and deleting users
db('groups')
.where('groupname', 'ilike', reqgroup)
.del()
.then (count => {
if (count!==0) {
res.json(`Total of ${count} users cleared from group.`)
}
else {
throw err
}
})
.catch (err => res.status(400).json('Error finding group in database.'))
}
module.exports = {
handleClearGroup
}