-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
142 lines (129 loc) · 5.08 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Import and require Express
const express = require('express');
// Import and require mysql2
const mysql = require('mysql2');
// Specify on which port the Express.js server will run
const PORT = process.env.PORT || 3001;
const app = express();
// Middleware for parsing JSON and urlencoded form data
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Connect to database
const db = mysql.createConnection(
{
host: 'localhost',
user: 'root',
password: 'friends',
database: 'employee_db'
},
console.log(`You are connected to the employee_db database!`)
);
// db.query function to view all departments
function viewAllDepartments() {
db.query('SELECT * FROM department', function (err, results) {
if (err) {
console.log(err);
} else {
console.table(results);
}
});
};
// db.query function to view all roles
function viewAllRoles() {
db.query('SELECT role.id AS ID, role.title AS Title, role.salary AS Salary, department.name AS Dept_Name FROM role JOIN department ON role.department_id = department.id', function (err, results) {
if (err) {
console.log(err);
} else {
console.table(results);
}
});
};
// db.query function to view all employees
function viewAllEmployees() {
db.query('SELECT employee.id AS id, employee.first_name AS First_Name, employee.last_name AS Last_Name, role.title AS Title, department.name AS Dept_Name, role.salary AS Salary, CONCAT(manager.first_name, " ", manager.last_name) AS Manager FROM employee LEFT JOIN role ON employee.role_id = role.id LEFT JOIN department ON role.department_id = department.id LEFT JOIN employee manager ON employee.manager_id = manager.id AND CASE WHEN employee.id != manager.id THEN true ELSE false END', function (err, results) {
if (err) {
console.log(err);
} else {
console.table(results);
}
});
};
// db.query function to add a department
function addDepartment(newDeptName) {
// Requires the name of the new deparment to be passed into the prepared statement
db.query(`INSERT INTO department (name)
VALUES ("${newDeptName}")`, function (err) {
if (err) {
console.log(err);
} else {
console.log(`${newDeptName} Department Added!`);
}
});
db.query('SELECT * FROM department', function (err, results) {
if (err) {
console.log(err);
} else {
console.table(results);
}
});
};
// db.query function to add a role
function addRole(newRole) {
// Requires the name of the new role, its salary, and its department_id to be passed into the prepared statement
db.query(`INSERT INTO role (title, salary, department_id)
VALUES ("${newRole.title}", ${newRole.salary}, ${newRole.department_id})`, function (err) {
if (err) {
console.log(err);
} else {
console.log(`${newRole.title} Role Added!`);
}
});
db.query('SELECT role.id AS ID, role.title AS Title, role.salary AS Salary, department.name AS Dept_Name FROM role JOIN department ON role.department_id = department.id', function (err, results) {
if (err) {
console.log(err);
} else {
console.table(results);
}
});
};
// db.query function to add an employee
function addEmployee(newEmployee) {
// Requires the new employee's first name, last name, role_id, and manager_id to be passed into the prepared statement
db.query(`INSERT INTO employee (first_name, last_name, role_id, manager_id)
VALUES ("${newEmployee.first_name}", "${newEmployee.last_name}", ${newEmployee.role_id}, ${newEmployee.manager_id})`, function (err) {
if (err) {
console.log(err);
} else {
console.log(`${newEmployee.first_name} ${newEmployee.last_name} has been hired!`);
}
});
db.query('SELECT employee.id AS id, employee.first_name AS First_Name, employee.last_name AS Last_Name, role.title AS Title, CONCAT(manager.first_name, " ", manager.last_name) AS Manager FROM employee LEFT JOIN role ON employee.role_id = role.id LEFT JOIN employee manager ON employee.manager_id = manager.id AND CASE WHEN employee.id != manager.id THEN true ELSE false END', function (err, results) {
if (err) {
console.log(err);
} else {
console.table(results);
}
});
};
// db.query function to update an employee's role
function updateEmployeeRole(updateEmpRole) {
// Requires the updated employee's new role_id and id to be passed into the prepared statement
db.query(`UPDATE employee SET role_id = ${updateEmpRole.role_id} WHERE id = ${updateEmpRole.id}`, function (err) {
if (err) {
console.log(err);
} else {
console.log('Someone got a new role!');
}
});
db.query('SELECT employee.id AS id, employee.first_name AS First_Name, employee.last_name AS Last_Name, role.title AS Title FROM employee LEFT JOIN role ON employee.role_id = role.id', function (err, results) {
if (err) {
console.log(err);
} else {
console.table(results);
}
});
};
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
module.exports = { db, viewAllDepartments, viewAllRoles, viewAllEmployees, addDepartment, addRole, addEmployee, updateEmployeeRole };