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