const Client = require('mariasql');
const table = {
"request": "request",
"employee": "employee",
"active": "active"
};
let c = new Client({
host: 'localhost',
port: 3306,
user: 'root',
password: 'toor',
db: 'randomcoffee'
});
//clarify query
function postEmployee(req, res) {
let item = req.body;
const query = 'use randomcoffee; Insert into employee values (:employee_id, :name, :surname, :email, :department, :location)';
let params = {
employee_id: item.employeeId,
name: item.name,
surname: item.surname,
email: item.email,
department: item.local_ou_id_sup,
location: item.location
};
c.query(query, params, function (err) {
if (err) {
res.status(400).send('failed to post');
throw err
} else {
res.status(200).send('posted');
}
});
c.end();
};
/**
* Inserts new request into DB
* @param req
* @param res
*/
function postRequest(req, res) {
let item = req.body;
const query = 'Insert into request values (:employeeId, :active, :timestamp)';
let params = {
employeeId: item.employeeId,
active: 1,
timestamp: new Date().valueOf(),
};
c.query(query, params, function (err) {
if (err) {
res.status(400).send('failed to post');
throw err
} else {
res.status(200).send('posted');
}
});
c.end();
};
function postInactiveRequest(req, res) {
let item = req.body;
const query = 'Insert into request values (:employeeId, :active, :timestamp)';
let params = {
employeeId: item.employeeId,
active: 0,
timestamp: new Date().valueOf(),
};
c.query(query, params, function (err) {
if (err) {
res.status(400).send('failed to post');
throw err
} else {
res.status(200).send('posted');
}
});
c.end();
};
/**
* retrieves employee based on employeeId
* returns
* @param req
* @param res
*/
function getEmployeeById(req, res) {
let item = req.body;
const query = 'Select * from request where employeeId = :employeeId';
let params = {employeeId: item.employeeId};
c.query(query, params, function (err, rows) {
if (err) {
res.status(400).send('failed to retrieve data');
throw err;
} else {
res.status(200).send(rows);
}
})
c.end();
};
//clarify query
/**
* retrieves all active requests from DB
* @param req
* @param res
*/
function getActiveRequests(req, res) {
const query = 'Select * from request where active = 1';
c.query(query, function (err, rows) {
if (err) {
res.status(400).send('failed to retrieve data');
throw err;
} else {
res.status(200).send(rows);
}
});
c.end();
}
/**
* Sets request as inactive by setting active = 0
* @param req
* @param res
*/
function setRequestInactive(req, res) {
let item = req.body;
const query = 'Update request set active = 0 where request_id = :request_id';
let params = {
request_id: item.requestId,
};
c.query(query, params, function (err, rows) {
if (err) {
res.status(400).send('failed to retrieve data');
throw err;
} else {
res.status(200).send(rows);
}
})
c.end();
};
function getRandomParticipants(req,) {
let item = req;
const query = 'SELECT r.requestId, b.employeeId, b.localOuIdSup, b.name, b.surname, b.email \n' +
'FROM employee b, employee a natural join request r WHERE a.localOuIdSup <> b.localOuIdSup \n' +
'and a.employeeId <> b.employeeId and a.employeeId = :employeeId and r.active = 1;';
let params = {
employeeId: item.employeeId,
// employeeId: 1,
};
c.query(query, params, function (err, rows) {
if (err) {
throw err;
} else {
return rows;
}
});
c.end();
}
module.exports = {
postEmployee: postEmployee,
postRequest: postRequest,
postInactiveRequest: postInactiveRequest,
getEmployeeById: getEmployeeById,
getActiveRequests: getActiveRequests,
setRequestInactive: setRequestInactive,
getRandomParticipants: getRandomParticipants
};
imretard's Introduction
imretard's People
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.