-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
343 lines (325 loc) · 9.38 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
//brings in the inquirer
const inquirer = require("inquirer");
// brings in mysql
const mysql = require("mysql");
//brings in fs
const fs = require("fs");
//brings in path
const path = require("path");
//brings in util
const util = require("util");
//brings in the tables
const cTable = require("console.table");
//brings in the rawlist
const RawList = require("prompt-rawlist");
//creates the connection to the database
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "",
database: "employeedb",
});
//makes the connection
connection.connect((err) => {
if (err) throw err;
//starts the application
runSearch();
});
//starts the application
const runSearch = () => {
//creates the list the user interacts with in the cli
inquirer
.prompt({
name: "action",
type: "rawlist",
message: "What would you like to do?",
choices: [
"Find Employee by Department",
"Find Employee by Role",
"Find all Employees",
"Find Employee by Manager",
"Add a Department",
"Add a Role",
"Add an Employee",
"Update Employee Role",
"Delete an Employee",
],
})
//one a the user makes a choice the function corresponding to their choice runs
.then((answer) => {
switch (answer.action) {
case "Find Employee by Department":
departmentSearch();
break;
case "Find Employee by Role":
roleSearch();
break;
case "Find all Employees":
employeeSearch();
break;
case "Find Employee by Manager":
managerSearch();
break;
case "Add a Department":
addDepartmentSearch();
break;
case "Add a Role":
addRole();
break;
case "Add an Employee":
addEmployeeSearch();
break;
case "Update Employee Role":
updateRoleSearch();
break;
case "Delete an Employee":
deleteEmployee();
break;
default:
console.log(`Invalid action: ${answer.action}`);
break;
}
});
};
//this shows the employee by department
const departmentSearch = () => {
const query =
"SELECT employee.id, employee.first_name, employee.last_name, department.name AS department from employee LEFT JOIN role on employee.role_id = role.id LEFT JOIN department on role.department_id = department.id;";
connection.query(query, (err, res) => {
if (err) throw err;
console.table(res);
runSearch();
});
};
//this shows the employee by role
const roleSearch = () => {
const query =
"SELECT employee.id, employee.first_name, employee.last_name, role.title from employee LEFT JOIN role on employee.role_id = role.id LEFT JOIN department on role.department_id = department.id;";
connection.query(query, (err, res) => {
if (err) throw err;
console.table(res);
runSearch();
});
};
// this shows all the employees
const employeeSearch = () => {
const query =
"SELECT employee.id, employee.first_name, employee.last_name, role.title, department.name AS department, role.salary from employee LEFT JOIN role on employee.role_id = role.id LEFT JOIN department on role.department_id = department.id;";
connection.query(query, (err, res) => {
if (err) throw err;
console.table(res);
runSearch();
});
};
//this shows all the tables availabe with the manager name and id
const managerSearch = () => {
const query =
"SELECT employee.id, employee.first_name, employee.last_name, role.title, department.name AS department, role.salary, manager.first_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 manager.id = employee.manager_id;";
connection.query(query, (err, res) => {
if (err) throw err;
console.table(res);
runSearch();
});
};
//this adds a department
const addDepartmentSearch = () => {
inquirer
.prompt({
name: "newDepartment",
type: "input",
message: "Please select a new Department.",
})
.then((answer) => {
connection.query(
"INSERT INTO department (department.name) VALUES (?)",
answer.newDepartment,
(err, res) => {
if (err) throw err;
{
console.log("Department change has been added.. good show");
runSearch();
}
}
);
});
};
//this allows the user to create a ne role with name, salary and department
const addRole = () => {
const query = "SELECT * FROM department;";
connection.query(query, (err, res) => {
if (err) throw err;
let NewDepartment = [];
res.map((results) => {
NewDepartment.push({
name: results.name,
value: results.id,
});
});
// NewDepartment.push("new department");
inquirer
.prompt([
{
name: "name",
type: "input",
message: "What is the name of the role you'd like to add?",
},
{
name: "salary",
type: "input",
message: "money.?", // salary question
},
{
name: "department",
type: "list",
message: "Please select a new Department.?",
choices: NewDepartment,
},
])
.then((res) => {
connection.query(
`INSERT INTO employeedb.role ( title, salary, department_id) values ("${res.name}", "${res.salary}", ${res.department})`,
(err, res) => {
if (err) {
console.log(err);
} else {
runSearch();
}
}
);
});
});
};
//this adds an empoyee
const addEmployeeSearch = () => {
const query = "SELECT id, title, department_id FROM role";
connection.query(query, (req, res) => {
let role_choice = res.map((role) => ({
name: role.title,
value: role.id,
}));
inquirer
.prompt([
{
name: "first_name",
type: "input",
message: "What is the Employees First Name?",
},
{
name: "last_name",
type: "input",
message: "What is the Employees Last Name?",
},
{
type: "list",
name: "role",
message: "What is the role assigned for the employee?",
choices: role_choice,
},
])
.then((res) => {
console.log(res);
connection.query(
`INSERT INTO employeedb.employee (first_name, last_name, role_id) VALUES ("${res.first_name}", "${res.last_name}", ${res.role});`,
(err, res) => {
console.log("employee has been added");
if (err) {
console.log(err);
} else {
runSearch();
}
}
);
});
});
};
// this updates the role id
const updateRoleSearch = () => {
const query = "SELECT id, title, department_id FROM role";
connection.query(query, (req, res) => {
let role_choice = res.map((role) => ({
name: role.title,
value: role.id,
}));
inquirer
.prompt([
{
name: "first_name",
type: "input",
message: "What is the Employees First Name?",
},
{
name: "last_name",
type: "input",
message: "What is the Employees Last Name?",
},
{
type: "list",
name: "role",
message: "What is the role assigned for the employee?",
choices: role_choice,
},
])
.then((res) => {
console.log(res);
connection.query(
`INSERT INTO employeedb.employee (first_name, last_name, role_id) VALUES ("${res.first_name}", "${res.last_name}", ${res.role});`,
(err, res) => {
console.log("employee and role has been added");
if (err) {
console.log(err);
} else {
runSearch();
}
}
);
});
});
};
// this almost deletes the employee, i just cant quite fix the mysql syntax error
const deleteEmployee = () => {
const query = "SELECT first_name, last_name, role_id FROM employee";
connection.query(query, (req, res) => {
let employee_choice = res.map((employee) => ({
name: employee.first_name + employee.last_name,
value: employee.role_id,
}));
inquirer
.prompt([
{
name: "first_name",
type: "input",
message: "What is the Employees First Name?",
},
{
name: "last_name",
type: "input",
message: "What is the Employees Last Name?",
},
{
type: "list",
name: "employee",
message: "Which employee would you like to delete?",
choices: employee_choice,
},
])
.then((res) => {
console.log(res);
connection.query(
`DELETE FROM employeedb.employee (first_name, last_name) WHERE ("${res.employee_choice = first_name}", "${res.employee_choice = last_name}";`,
(err, res) => {
console.log("employee and role has been deleted");
if (err) {
console.log(err);
} else {
runSearch();
}
}
);
});
});
};
// DELETE FROM employeedb.employee
// [WHERE where_condition]
// [ORDER BY ...]
// [LIMIT row_count]