-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
497 lines (445 loc) · 15.2 KB
/
server.js
File metadata and controls
497 lines (445 loc) · 15.2 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
const mysql = require("mysql")
const inquirer = require("inquirer");
//creat connection for sql database
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "joychen5069",
database: "employee_db"
});
//connect to mysql server and sql database
connection.connect((err) => {
if (err) throw err;
start();
})
start = () => {
//only display the thing once
// logo();
//then ask question
userChoice();
}
//prompt question about what the user wants to do
userChoice = async () => {
const answer = await inquirer.prompt([
{
type: "list",
name: "action",
message: "What would you like to do?",
choices: [
"View All Employees",
// "View All Employees by Manager",
"View Department Table",
"View Role Table",
"Add Employee",
"Add Department",
"Add Role",
"Update Employee Role",
// "Update Employee Manager",
"Remove Employee",
"Finish"
]
}
]);
console.log(answer.action);
select(answer);
}
//user function wants to view all employees
viewAll = async () => {
connection.query(
"SELECT * FROM employeeInfo LEFT JOIN roleInfo ON employeeInfo.title=roleInfo.title LEFT JOIN departmentInfo ON roleInfo.department_id=departmentInfo.id",
(err, res) => {
if (err) throw err;
console.table(res)
userChoice();
})
}
//user function wants to view all employees by manager--THIS FUNCTION DOES NOT WORK YET
// viewEmpByMan = async () => {
// connection.query("SELECT * FROM roleInfo",
// (err, res) => {
// if (err) throw err
// //ask what manager they want to view
// inquirer.prompt([
// {
// type: "list",
// name: "manager",
// message: "Pick a manager to view the employees:",
// choices: () => {
// let choiceArray = [];
// for (let i = 0; i < res.length; i++) {
// choiceArray.push(res[i].manager);
// }
// return choiceArray;
// }
// }
// ])
// .then((answer) => {
// const manager = res.find(el => el.manager === answer.manager)
// console.log(manager)
// connection.query(`SELECT * FROM employeeInfo WHERE id = "${manager.id}"`,
// (err, res) => {
// if (err) throw err;
// console.table(res)
// userChoice();
// })
// })
// })
// }
//user function wants to ADD EMPLOYEE
addEmployee = () => {
//read the employees first
connection.query("SELECT * FROM roleInfo", async (err, res) => {
if (err) throw err;
//ask the key questions
const answer = await inquirer.prompt([
{
type: "input",
name: "first",
message: "What is the Employee's first name?"
},
{
type: "input",
name: "last",
message: "What is the Employee's last name?"
},
{
type: "list",
name: "role",
message: "What is the Employee's role?",
choices: () => {
let choiceArray = [];
for (let i = 0; i < res.length; i++) {
choiceArray.push(res[i].title);
}
return choiceArray;
}
},
{
type: "list",
name: "manager",
message: "What is the manager's ID?",
choices: [
0, 1, 2, 3, 4, 5
]
}
]);
connection.query("INSERT INTO employeeInfo SET ?", {
first_name: answer.first,
last_name: answer.last,
title: answer.role,
manager_id: answer.manager
}, (err) => {
if (err) throw err;
console.log("added successfully");
// re-prompt
userChoice();
});
})
};
//user function wants to REMOVE EMPLOYEE
removeEmployee = () => {
//read the employees first
connection.query("SELECT * FROM employeeInfo", (err, res) => {
if (err) throw err;
//ask which employee they want to remove
inquirer.prompt([
{
type: "list",
name: "name",
message: "Who would you like to remove?",
choices: () => {
let choiceArray = [];
for (let i = 0; i < res.length; i++) {
choiceArray.push(res[i].first_name + " " + res[i].last_name);
}
return choiceArray;
},
}
])
//now actually delete them
.then((answer) => {
let fullName = answer.name;
let remove = fullName.split(" ");
connection.query(
`DELETE FROM employeeInfo WHERE first_name = "${remove[0]}" AND last_name = "${remove[1]}"`,
(err) => {
if (err) throw err;
console.log("removed successfully");
// re-prompt
userChoice();
}
)
})
})
}
//user function wants to UPDATE Employee ROLE
updateRole = () => {
//pull all the employees first
connection.query("SELECT * FROM employeeInfo", (err, res) => {
if (err) throw err
console.table(res)
//ask who they want to modify
inquirer.prompt([
{
type: "list",
name: "name",
message: "Which employee would you like to update the role of?",
choices: () => {
let choiceArray = [];
for (let i = 0; i < res.length; i++) {
choiceArray.push(res[i].first_name + " " + res[i].last_name);
}
return choiceArray;
}
},
])
// now modify them
.then((answer) => {
//split the name
let fullName = answer.name;
console.log(fullName);
let splitName = fullName.split(" ");
console.log(splitName[0]);
connection.query("SELECT * FROM roleInfo", (err, res) => {
inquirer.prompt([
{
type: "list",
name: "role",
message: "What would you like to update the role to be?",
choices: () => {
let choiceArray = [];
for (let i = 0; i < res.length; i++) {
choiceArray.push(res[i].title);
}
return choiceArray;
}
}
])
.then((result) => {
const role = result.role
connection.query(
`UPDATE employeeInfo SET title = "${role}" WHERE first_name = "${splitName[0]}" and last_name = "${splitName[1]}"`,
// [ {title: answer.role} ]
(err) => {
if (err) throw err;
console.log("added successfully");
// re-prompt
userChoice();
}
)
})
})
})
}
)
}
//user function wants to UPDATE Employee MANAGER--THIS FUNCTION DOES NOT WORK YET
// updateManager = () => {
// //pull all the employees first
// connection.query("SELECT * FROM employeeInfo", (err, res) => {
// if (err) throw err
// //ask who they want to modify
// inquirer.prompt([
// {
// type: "list",
// name: "name",
// message: "Which employee would you like to update the manager of?",
// choices: () => {
// let choiceArray = [];
// for (let i = 0; i < res.length; i++) {
// choiceArray.push(res[i].first_name + " " + res[i].last_name);
// }
// return choiceArray;
// }
// }
// ])
// // now modify them
// .then((answer) => {
// //split the name
// let fullName = answer.name;
// console.log(fullName);
// let splitName = fullName.split(" ");
// console.log(splitName[0]);
// connection.query('SELECT * FROM roleInfo', (err, res) {
// inquirer.prompt([
// {
// type: "list",
// name: "manager",
// message: "Who is the new manager?",
// choices: () => {
// let choiceArray = [];
// for (let i = 0; i < res.length; i++) {
// choiceArray.push(res[i].manager);
// }
// return choiceArray;
// }
// }
// ])
// .then((result) => {
// connection.query(
// `UPDATE employeeInfo SET manager = "${answer.manager_id}" WHERE first_name = "${splitName[0]}" and last_name = "${splitName[1]}"`,
// (err) => {
// if (err) throw err;
// console.log("updated successfully");
// // re-prompt
// userChoice();
// }
// )
// })
// })
// })
// })
// }
//user function wants to view all departments
viewAllDepartment = () => {
connection.query(
"SELECT * FROM departmentInfo", (err, res) => {
if (err) throw err;
console.table(res);
userChoice();
}
)
}
//user function wants to view all roles, salaries, etc
viewAllRoles = () => {
connection.query(
"SELECT * FROM roleInfo", (err, res) => {
if (err) throw err;
console.table(res);
userChoice();
}
)
}
//user wants to add Department
addDepartment = () => {
inquirer.prompt([
{
type: "input",
name: "depName",
message: "What is the name of the new department?"
}
])
.then(answer => {
connection.query(
`INSERT INTO departmentInfo SET department = "${answer.depName}"`,
(err) => {
if (err) throw err;
console.log("added successfully");
// re-prompt
userChoice();
}
)
})
}
//user wants to add a Role
addRole = () => {
connection.query("SELECT * FROM departmentInfo", (err, res) => {
if (err) throw err;
inquirer.prompt([
{
type: "input",
name: "roleTitle",
message: "What is the name of the new role?"
},
{
type: "input",
name: "salary",
message: "What is the starting salary of this position?"
},
{
type: "input",
name: "manager",
message: "Who is the manager of this role? (if none, write None)"
},
{
type: "list",
name: "depID",
message: "What is the department ID?",
choices: () => {
let choiceArray = [];
for (let i = 0; i < res.length; i++) {
choiceArray.push(res[i].id)
}
return choiceArray
}
}
])
.then(answers => {
connection.query(
`INSERT INTO roleInfo SET ?`,
{
title: answers.roleTitle,
salary: answers.salary,
manager: answers.manager,
department_id: answers.depID
},
(err) => {
if (err) throw err;
console.log("added role successfully");
// re-prompt
userChoice();
}
)
})
})
}
//run a switch statement to switch between them all
select = (answers) => {
switch (answers.action) {
//view ALL
case ("View All Employees"):
console.log("View All Employees");
viewAll();
break;
//view Manager
case ("View All Employees by Manager"):
console.log("View All Employees by Manager");
viewEmpByMan();
break;
//View Department
case ("View Department Table"):
console.log("View Department");
viewAllDepartment();
break;
//View Role
case ("View Role Table"):
console.log("View Role");
viewAllRoles();
break;
//Add employee
case ("Add Employee"):
console.log("Add Employee");
addEmployee();
break;
//Remove employee
case ("Remove Employee"):
console.log("Remove Employee");
removeEmployee();
break;
//Update Role
case ("Update Employee Role"):
console.log("Update Employee Role");
updateRole();
break;
//Update Manager
case ("Update Employee Manager"):
console.log("Update Employee Manager");
updateManager();
break;
//Add Department
case ("Add Department"):
console.log("Adding department...")
addDepartment();
break;
//Add Role
case ("Add Role"):
console.log("adding role...");
addRole();
break;
default:
//End connection
console.log("Thank you for accessing the Database.");
connection.end();
}
}