-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
291 lines (249 loc) · 9.59 KB
/
server.js
File metadata and controls
291 lines (249 loc) · 9.59 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// --------------------- //
// ----- server.js ----- //
// --------------------- //
import express from 'express';
import inquirer from 'inquirer';
import mysql from 'mysql2';
const app = express();
const PORT = process.env.PORT || 3001;
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'potato22',
database: 'user_db'
})
// time to wait (milliseconds)
let thisLong = 1800;
// wait function
function wait(ms) {
var start = new Date().getTime();
var end = start;
while (end < start + ms) {
end = new Date().getTime();
}
}
function init() {
// Menu with the following options: view all departments, view all roles, view all employees, add a department, add a role, add an employee, and update an employee role, exit.
inquirer
.prompt([
{
type: 'list',
message: '\nWelcome to the Main Menu. \nPlease make a choice to continue:',
name: 'mainMenu',
choices: [
'view all departments',
'view all roles',
'view all employees',
'add a department',
'add a role',
'add an employee',
'update an employee role',
'exit',
],
},
]).then(choice => {
if (choice.mainMenu === 'view all departments') {
viewDepartments();
} else if (choice.mainMenu === 'view all roles') {
viewRoles();
} else if (choice.mainMenu === 'view all employees') {
viewEmployees();
} else if (choice.mainMenu === 'add a department') {
addDepartments();
} else if (choice.mainMenu === 'add a role') {
addRoles();
} else if (choice.mainMenu === 'add an employee') {
addEmployee();
} else if (choice.mainMenu === 'update an employee role') {
updateEmpRole();
} else if (choice.mainMenu === 'exit') {
exit();
}
});
// formatted table showing department names and department ids
function viewDepartments() {
const sqlInput = `SELECT * FROM department`;
db.query(sqlInput, (err, data) => {
if (err) {
console.log(err);
};
console.log("All Departments:")
console.table(data);
wait(thisLong);
init();
});
};
// display the job title, role id, the department that role belongs to, and the salary for that role
function viewRoles() {
console.log('viewRoles function works');
const sqlInput = `
SELECT role.id, role.title, department.name, role.salary
FROM role, department
WHERE role.department_id = department.id;`
;
db.query(sqlInput, (err, data) => {
if (err) {
console.log(err);
};
console.log("The Roles:")
console.table(data);
wait(thisLong);
init();
});
};
// display employee data, including employee ids, first names, last names, job titles, departments, salaries, and managers that the employees report to
function viewEmployees() {
console.log('viewEmployees function works');
const sqlInput = `
SELECT employee.id, employee.first_name, employee.last_name, role.title, department.name AS department, role.salary, department.name AS manager
FROM employee
LEFT JOIN employee manager ON manager.id = employee.manager_id
INNER JOIN role ON (role.id = employee.role_id)
INNER JOIN department ON (department.id = role.department_id)`
;
db.query(sqlInput, (err, data) => {
if (err) {
console.log(err);
};
console.log("View All Employees:")
console.table(data);
wait(thisLong);
init();
});
};
// addDepartment function - prompt to enter name of the department and it is added to the database
function addDepartments() {
console.log('addDepartments function works');
inquirer
.prompt([
{
type: 'input',
message: '\nPlease enter the name of the department you would like to add:\n',
name: 'addDept',
}
]).then(userInput => {
const sqlInput = `INSERT INTO department (name) VALUES ("${userInput.addDept}");`;
console.log("input is: " + userInput.addDept);
db.query(sqlInput, (err, data) => {
if (err) {
console.log(err);
};
console.log("Here is the updated department database:")
console.table(data);
wait(thisLong);
init();
});
});
};
// addRoles - prompt to enter the name, salary, and department for the role and that role is added to the database
function addRoles() {
console.log('addRoles function works');
inquirer
.prompt([
{
type: 'input',
message: 'Please enter the title of the role you would like to add: ',
name: 'roleTitle',
},
{
type: 'input',
message: 'Please enter the salary of the role you would like to add: ',
name: 'roleSalary',
},
{
type: 'input',
message: 'Please enter the department_id of the role you would like to add: ',
name: 'roleDept',
},
]).then(userInput => {
const sqlInput = `INSERT INTO role (title, salary, department_id) VALUES ("${userInput.roleTitle}", "${userInput.roleSalary}", "${userInput.roleDept}" );`;
console.log("inputs are: " + userInput.roleTitle + ", " + userInput.roleSalary + ", " + userInput.roleDept);
db.query(sqlInput, (err, data) => {
if (err) {
console.log(err);
};
console.log("Here is the updated role database:")
console.table(data);
wait(thisLong);
init();
});
});
};
// addEmployee => prompts to enter the employee’s first name, last name, role, and manager, and that employee is added to the database
function addEmployee() {
console.log('addEmployee function works');
inquirer
.prompt([
{
type: 'input',
message: 'Employee first name? ',
name: 'empFirstName',
},
{
type: 'input',
message: 'Employee last name? ',
name: 'empLastName',
},
{
type: 'input',
message: 'What is the their role?',
name: 'empRole',
},
{
type: 'input',
message: 'Who is their manager?',
name: 'empManager',
},
]).then(userInput => {
const sqlInput = `INSERT INTO employee (first_name, last_name, role_id, manager_id) VALUES ("${userInput.empFirstName}", "${userInput.empLastName}", ${userInput.empRole}, ${userInput.empManager} );`;
console.log("inputs are: " + userInput.empFirstName + ", " + userInput.empLastName + ", " + userInput.empRole + ", " + userInput.empManager);
db.query(sqlInput, (err, data) => {
if (err) {
console.log(err);
};
console.log("Here is the updated role database:")
console.table(data);
wait(thisLong);
init();
});
});
};
// updateEmpRole => prompts to select an employee to update and their new role and this information is updated in the database
function updateEmpRole() {
const sqlInput = `SELECT * FROM employee`;
console.log("Update Employee's Role:")
inquirer
.prompt([
{
type: 'input',
message: 'Which Employee would you like to update',
name: 'updateEmpName',
},
{
type: 'input',
message: 'What is the their new role?',
name: 'updateEmpRole',
},
]).then(userInput => {
console.log("inputs are: " + userInput.updateEmpName + ", " + userInput.updateEmpRole);
db.query(sqlInput, (err, data) => {
if (err) {
console.log(err);
};
console.log("Here is the updated employee database:")
console.table(data);
wait(thisLong);
init();
});
});
};
// exit function
function exit() {
process.exit();
};
};
// ^^ end init() function ^^ //
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
init();