Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rachel-Dominik-Hackathon #75

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
38 changes: 38 additions & 0 deletions controllers/departments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const mysql = require('mysql');
const pool = require("../mysql/connections");
const { handleSQLError } = require('../mysql/errors');

const getDepartments = (req,res) => {

pool.query("SELECT * FROM departments LIMIT 50", (err, rows) => {
//the following is assuming there is a function to handle errors named handleSQLErrors
if (err) return handleSQLError(res,err)
return res.json(rows);
})
}

const getDepartmentsById = (req,res) => {
//select the employees by ID
let id = req.params.id;
let sql ="SELECT * FROM departments WHERE dept_no = ?";
sql = mysql.format(sql, [id]);

pool.query(sql, (err, rows) => {
if (err) return handleSQLError(res,err)
return res.json(rows);
})
}

const getDepartmentManagers = (req,res) => {
let department_managers = req.params.first_name;
let sql = "select * from dept_manager join employees on employees.emp_no = dept_manager.emp_no join departments on departments.dept_no = dept_manager.dept_no"

sql = mysql.format(sql, [department_managers]);

pool.query(sql, (err, rows) => {
if (err) return handleSQLError(res,err)
return res.json(rows);
})
}

module.exports = { getDepartments, getDepartmentsById, getDepartmentManagers }
38 changes: 38 additions & 0 deletions controllers/employees.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const mysql = require('mysql');
const pool = require("../mysql/connections");
const { handleSQLError } = require('../mysql/errors');

const getEmployees = (req,res) => {

pool.query("SELECT * FROM employees LIMIT 50", (err, rows) => {
//the following is assuming there is a function to handle errors named handleSQLErrors
if (err) return handleSQLError(res,err)
return res.json(rows);
})
}

const getEmployeesById = (req,res) => {
//select the employees by ID
let id = req.params.id;
let sql ="SELECT * FROM employees WHERE emp_no = ?";
sql = mysql.format(sql, [id]);

pool.query(sql, (err, rows) => {
if (err) return handleSQLError(res,err)
return res.json(rows);
})
}

const getEmployeesByFirstName = (req,res) => {
let first_name = req.params.first_name;
let sql = "SELECT * FROM employees WHERE first_name = ?"

sql = mysql.format(sql, [first_name]);

pool.query(sql, (err, rows) => {
if (err) return handleSQLError(res,err)
return res.json(rows);
})
}

module.exports = { getEmployees, getEmployeesById, getEmployeesByFirstName }
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//Required Modules for Server
const express = require('express');
const app = express();
const employees = require('./routes/employees')
const departments = require('./routes/departments')

//Body-Parsing
app.use(express.json());
app.use(express.urlencoded({ extended:true}))

//routes
app.use("/", employees);
app.use("/", departments)

//Default Route
app.get('/', (req, res) =>{
res.send('Welcome To Our API')
})

//Port Number
const port = process.env.PORT || 3000;

//Open server Port
app.listen(port, (err) => {
if(err){
console.log("error in server")
}
console.log(`I am now listening on port ${port}`);
});
19 changes: 19 additions & 0 deletions mysql/connections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const mysql = require('mysql');

function connection() {
if(!this.pool){
console.log('creating pool');this.pool = mysql.createPool({
connectionLimit: 100,
host: 'den1.mysql4.gear.host',
user: 'aca311hackdb',
password: 'Tv3R_96le~b0',
database: 'aca311hackdb'
})
return this.pool;
}
return this.pool
}

const instance = connection();

module.exports = instance;
6 changes: 6 additions & 0 deletions mysql/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const handleSQLError = (res, err) => {
console.log('SQL Error: ', err)
return res.status(500).send('An unexpected error occurred');
}

module.exports = { handleSQLError }
Loading