-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
385 lines (356 loc) · 12.5 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
const express = require('express');
const inquirer = require("inquirer");
const mysql = require('mysql2/promise');
const db = require('./db/connection');
const cTable = require('console.table');
const PORT = process.env.PORT || 3000;
const app = express();
// Express middleware
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Default response for any other request (Not Found)
app.use((req, res) => {
res.status(404).end();
});
// Start server after DB connection
db.connect(err => {
if (err) throw err;
app.listen(PORT, () => {});
});
// Start the prompt functions
function startPrompt() {
inquirer.prompt({
type: 'list',
name: 'menu',
message: 'What would you like to do?',
choices: ['View All Departments', 'View All Roles', 'View All Employees', 'Add A Department', 'Add A Role', 'Add An Employee', 'Update An Employee Role', 'Update An Employee Manager', 'Delete Department', 'Delete Role', 'Delete Employee'],
}).then( answer => {
switch (answer.menu) {
case 'View All Departments':
viewAllDepartments();
break;
case 'View All Roles':
viewAllRoles();
break;
case 'View All Employees':
viewAllEmployees();
break;
case 'Add A Department':
addDepartment();
break;
case 'Add A Role':
addRole();
break;
case 'Add An Employee':
addEmployee();
break;
case 'Update An Employee Role':
updateEmployeeRole();
break;
case 'Update An Employee Manager':
updateEmployeeManager();
break;
case 'Delete Department':
deleteDepartment();
break;
case 'Delete Role':
deleteRole();
break;
case 'Delete Employee':
deleteEmployee();
break;
}
})
};
// View all departments
function viewAllDepartments() {
const sql = `SELECT * FROM department`;
db.query(sql, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
return;
}
console.table(result);
startPrompt();
});
};
// View all roles
function viewAllRoles() {
const sql = `SELECT * FROM role`;
db.query(sql, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
return;
}
console.table(result);
startPrompt();
});
};
// View all employees
function viewAllEmployees() {
const sql = `SELECT employee.id,
employee.first_name,
employee.last_name,
role.title AS job_title,
department.department_name,
role.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 AS manager ON employee.manager_id = manager.id
ORDER By employee.id`;
db.query(sql, (err, result) => {
if (err) throw err;
console.table(result);
startPrompt();
});
};
// Add departments
function addDepartment() {
inquirer.prompt([
{
name: "department_name",
type: "input",
message: "Please enter the name of the department you want to add to the database."
}
]).then((answer) => {
const sql = `INSERT INTO department (department_name)
VALUES (?)`;
const params = [answer.department_name];
db.query(sql, params, (err, result) => {
if (err) throw err;
console.log('The new department entered has been added successfully to the database.');
db.query(`SELECT * FROM department`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
return;
}
console.table(result);
startPrompt();
});
});
});
};
// Add a role
function addRole() {
inquirer.prompt([
{
name: "title",
type: "input",
message: "Please enter the title of role you want to add to the database."
},
{
name: "salary",
type: "input",
message: "Please enter the salary associated with the role you want to add to the database. (no dots, space or commas)"
},
{
name: "department_id",
type: "number",
message: "Please enter the department's id associated with the role you want to add to the database."
}
]).then(function (response) {
db.query("INSERT INTO role (title, salary, department_id) VALUES (?, ?, ?)", [response.title, response.salary, response.department_id], function (err, data) {
if (err) throw err;
console.log('The new role entered has been added successfully to the database.');
db.query(`SELECT * FROM role`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
startPrompt();
}
console.table(result);
startPrompt();
});
})
});
};
// Add employees
function addEmployee() {
inquirer.prompt([
{
name: "first_name",
type: "input",
message: "Please enter the first name of the employee you want to add to the database."
},
{
name: "last_name",
type: "input",
message: "Please enter the last name of the employee you want to add to the database."
},
{
name: "role_id",
type: "number",
message: "Please enter the role id associated with the employee you want to add to the database. Enter ONLY numbers."
},
{
name: "manager_id",
type: "number",
message: "Please enter the manager's id associated with the employee you want to add to the database. Enter ONLY numbers."
}
]).then(function (response) {
db.query("INSERT INTO employee (first_name, last_name, role_id, manager_id) VALUES (?, ?, ?, ?)", [response.first_name, response.last_name, response.role_id, response.manager_id], function (err, data) {
if (err) throw err;
console.log('The new employee entered has been added successfully to the database.');
db.query(`SELECT * FROM employee`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
startPrompt();
}
console.table(result);
startPrompt();
});
})
});
};
// Update employee role
function updateEmployeeRole() {
inquirer.prompt([
{
name: "first_name",
type: "input",
message: "Please enter the first name of the employee you want update in the database."
},
{
name: "role_id",
type: "number",
message: "Please enter the new role number id associated with the employee you want to update in the database. Enter ONLY numbers."
}
]).then(function (response) {
db.query("UPDATE employee SET role_id = ? WHERE first_name = ?", [response.role_id, response.first_name], function (err, data) {
if (err) throw err;
console.log('The new role entered has been added successfully to the database.');
db.query(`SELECT * FROM employee`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
startPrompt();
}
console.table(result);
startPrompt();
});
})
});
};
// Update employee manager
function updateEmployeeManager() {
inquirer.prompt([
{
name: "first_name",
type: "input",
message: "Please enter the first name of the employee you want update in the database."
},
{
name: "manager_id",
type: "number",
message: "Please enter the new manager's id number associated with the employee you want to update in the database. Enter ONLY numbers."
}
]).then(function (response) {
db.query("UPDATE employee SET manager_id = ? WHERE first_name = ?", [response.manager_id, response.first_name], function (err, data) {
if (err) throw err;
console.log("The new manager's id entered has been added successfully to the database.");
db.query(`SELECT * FROM employee`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
startPrompt();
}
console.table(result);
startPrompt();
});
})
});
};
// Delete department
function deleteDepartment() {
inquirer.prompt([
{
name: "department_id",
type: "number",
message: "Please enter the id of the department you want to delete from the database. Enter ONLY numbers."
}
]).then(function (response) {
db.query("DELETE FROM department WHERE id = ?", [response.department_id], function (err, data) {
if (err) throw err;
console.log("The department entered has been deleted successfully from the database.");
db.query(`SELECT * FROM department`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
startPrompt();
}
console.table(result);
startPrompt();
});
})
});
};
// Delete role
function deleteRole() {
inquirer.prompt([
{
name: "role_id",
type: "number",
message: "Please enter the id of the role you want to delete from the database. Enter ONLY numbers."
}
]).then(function (response) {
db.query("DELETE FROM role WHERE id = ?", [response.role_id], function (err, data) {
if (err) throw err;
console.log("The role entered has been deleted successfully from the database.");
db.query(`SELECT * FROM role`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
startPrompt();
}
console.table(result);
startPrompt();
});
})
});
};
// Delete Employee
function deleteEmployee() {
inquirer.prompt([
{
name: "employee_id",
type: "number",
message: "Please enter the id of the employee you want to delete from the database. Enter ONLY numbers."
}
]).then(function (response) {
db.query("DELETE FROM employee WHERE id = ?", [response.employee_id], function (err, data) {
if (err) throw err;
console.log("The employee entered has been deleted successfully from the database.");
db.query(`SELECT * FROM employee`, (err, result) => {
if (err) {
res.status(500).json({ error: err.message })
startPrompt();
}
console.table(result);
startPrompt();
});
})
});
};
// // View employee by manager
// function viewAllEmpByManager() {
// const sql = ` SELECT
// CONCAT(manager.first_name, ' ' ,manager.last_name) AS manager,
// employee.id,
// employee.first_name,
// employee.last_name,
// roles.title AS Title,
// department.department_name,
// roles.salary
// FROM employee
// JOIN roles ON employee.role_id = roles.id
// JOIN department ON roles.department_id = department.id
// LEFT JOIN employee AS manager ON employee.manager_id = manager.id
// ORDER BY manager`;
// db.query(sql, (err, result) => {
// if(err) {
// res.status(400).json({ error: err.message });
// return;
// }
// console.table(result);
// startPrompt();
// });
// }
// Call to start app
startPrompt();