-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
237 lines (227 loc) · 6.96 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
//dependencies
var mysql = require("mysql");
var inquirer = require("inquirer")
//connection with database
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "Dragonfly89!",
database: "database_db",
});
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
init();
});
//starting function
function init() {
inquirer
.prompt([
{
//list of choices
type: "list",
name: "team",
message: "What would you like to do?",
choices: [
"View Employees",
"View Departments",
"View Roles",
"Add Employee",
"Add Role",
"Add Department",
"Update Employee Role",
"Exit",
],
},
])
.then((response) => {
//how to close ann app
if (response.team === "Exit") {
console.log("No problem!See you next time!");
connection.end();
} else {
//list of functions that gonna handle different responses
switch (response.team) {
case "View Employees":
//pull all the employees from the data base and display in console in table
connection.query(`SELECT e.id, e.first_name, e.last_name, r.title, r.salary, em.first_name as "manager first name" FROM employees e
LEFT JOIN employees em ON e.manager_id = em.id
INNER JOIN roles r ON e.role_id = r.id`, function (err, res) {
if (err) throw err;
console.table(res)
init();
});
break;
case "View Departments":
//pull all the departments from db and display in console in table
connection.query("SELECT * FROM departments", function (err, res) {
if (err) throw err;
console.table(res)
init();
});
break;
case "View Roles":
//pull all the roles from the data base and display in console in table
connection.query("SELECT * FROM roles", function (err, res) {
if (err) throw err;
console.table(res)
init();
});
break;
case "Add Employee":
//add the employee in a db
addEmployee();
break;
case "Add Department":
//add department in a db
addDepartment();
break;
case "Add Role":
//add the role in a db
addRoles();
break;
case "Update Employee Role":
//update the employee in a db
updateEmployeeRole();
break;
}
}
});
}
//fuctions that help to manipulate users input
function addEmployee() {
inquirer
.prompt([
{
type: "input",
name: "firstName",
message: "What is the first name of your employee?",
},
{
type: "input",
name: "lastName",
message: "What is the last name of your employee?",
},
{
type: "input",
name: "role_id",
message: "Please enter role id of your employee",
},
{
type: "input",
name: "manager_id",
message: "Who will be the manager of your employee(put the id of a manager)?",
},
])
.then((resp) => {
connection.query(
//formulating command to insert info into db
"INSERT INTO employees (first_name, last_name, role_id, manager_id) VALUES (?,?,?,?)",
[resp.firstName, resp.lastName, parseInt(resp.role_id), parseInt(resp.manager_id)],
function (err, result) {
if (err) throw err;
init()
}
);
});
}
function addDepartment() {
inquirer
.prompt([
{
type: "input",
name: "name",
message: "Please enter name of a new department",
}
])
.then((resp) => {
connection.query(
"INSERT INTO departments (name) VALUES (?)",
[resp.name],
function (err, result) {
if (err) throw err;
init()
}
);
});
}
function addRoles() {
inquirer
.prompt([
{
type: "input",
name: "title",
message: "What is the name of a new role?",
},
{
type: "input",
name: "salary",
message: "Please enter the salary?",
},
{
type: "input",
name: "department_id",
message: "Please enter department id",
}
])
.then((resp) => {
connection.query(
"INSERT INTO roles (title,salary,department_id) VALUES (?,?,?)",
[resp.title, parseFloat(resp.salary), parseInt(resp.department_id)],
function (err, result) {
if (err) throw err;
init()
}
);
});
}
let roles = []
let employeesList = []
function updateEmployeeRole() {
//SQL Query to get roles array
connection.query(`SELECT e.id, e.first_name, e.last_name FROM employees e`, function (err, res) {
if (err)
console.log(err)
console.log(res)
employeesList = res.map(({ id, first_name, last_name }) => ({ name: first_name + " " + last_name, value: id }))
connection.query(`SELECT r.id, r.title FROM roles r`, function (err, res) {
if (err)
console.log(err)
console.log(res)
roles = res.map(({ id, title }) => ({ name: title, value: id }))
inquirer
.prompt([
{
type: "list",
name: "employee_id",
message: "What is the employee's first and last name?",
choices: employeesList//employeesList //set type to be inquirer list and research how to attribute values to user choice.
},
{
type: "list",
name: "new_role",
message: "Please enter the new role",
choices: roles//roles
},
]).then((resp) => {//then use the ids of the values the user selected in stead of using the direct selection of the id. The code below should stay mostly the same unless you want to implement async away here a well.
connection.query("UPDATE employees SET role_id =? WHERE id =? ",
[parseInt(resp.new_role), parseInt(resp.employee_id)],
function (err, result) {
if (err) throw err;
console.log(result)
console.log("IDS", parseInt(resp.employee_id), parseInt(resp.new_role))
init()
})
})
})
})
// //SQL Query to get employees array (order doesn't matter)
// connection.query(`SELECT e.first_name, e.last_name FROM employees`, function (err, res) {
// console.log(res)
// employeesList = res
// connection.query(`SELECT e.id, e.first_name, e.last_name, r.title FROM employees e INNER JOIN roles r ON e.role_id = r.id`, function (err, res) {
// console.log(res)
//});
//Refer back to async await and try catch blocks, this will help make this easier and spare you nested callbacks
}