-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
229 lines (221 loc) · 5.77 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
//call once somewhere in the beginning of the app
const mysql = require("mysql2");
const inquirer = require("inquirer");
require("console.table");
//Use dotenv
require("dotenv").config();
//--CONNECTION---//
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
connection.connect(function (err) {
if (err) throw err;
init();
});
console.log("DB connection success!!");
//USE inquirer prompt with call back function
const init = () => {
inquirer
.prompt({
name: "main",
type: "list",
message: "What do you need to do?",
choices: [
"Add Department",
"Add Role",
"Add Employee",
"View All Departments",
"View All Roles",
"View All Employees",
"Update Employee Role",
"Exit",
],
})
.then((choices) => {
switch (choices.main) {
case "Add Department":
addDepartment();
break;
case "Add Role":
addRole();
break;
case "Add Employee":
addEmployee();
break;
case "View All Departments":
viewAllDepartments();
break;
case "View All Roles":
viewAllRoles();
break;
case "View All Employees":
viewAllEmployees();
break;
case "Update Employee Role":
updateRole();
break;
case "Exit":
connection.end();
break;
default:
console.log("Back to main menu: ${select.main}");
break;
}
})
.catch((err) => console.err(err));
};
///ADD DEPARTMENT
const addDepartment = () => {
inquirer
.prompt([
{
type: "input",
name: "name",
message: "What is the Department Name?",
},
])
.then((department) => {
connection.query("INSERT INTO department SET ?", department, (error) => {
init();
});
});
};
//ADD ROLES
const addRole = () => {
connection.query(
"SELECT name, id AS value FROM department",
(error, departmentNames) => {
inquirer
.prompt([
{
type: "input",
name: "title",
message: "Role's title?",
},
{
type: "input",
name: "salary",
message: "Role's salary?",
},
{
type: "list",
name: "department_id",
message: "Department ID?",
choices: departmentNames,
},
])
.then((role) => {
connection.query("INSERT INTO role SET ?", role, () => {
init();
});
});
}
);
};
//ADD EMPLOYEE
const addEmployee = () => {
connection.query(
`SELECT id AS a value, title AS a name FROM role`,
(err, roles) => {
inquirer
.prompt([
{
type: "input",
name: "first_name",
message: "What is the employee's first name?",
},
{
type: "input",
name: "last_name",
message: "What is the employee's last name?",
},
{
type: "list",
name: "role_id",
message: "What is the employee's role?",
choices: roles,
},
])
.then((employee) => {
connection.query("INSERT INTO employee SET ?", employee, (error) => {
init();
});
});
}
);
};
//VIEW ALL DEPARTMENTS
function viewAllDepartments() {
connection.query(
"SELECT employee.first_name, employee.last_name, department.name AS Department FROM employee JOIN role ON employee.role_id = role.id JOIN department ON role.department_id = department.id ORDER BY employee.id;",
function (error, information) {
if (error) throw error;
console.table(information);
init();
}
);
}
//VIEW ALL ROLES
function viewAllRoles() {
connection.query(
"SELECT employee.first_name, employee.last_name, role.title AS Title FROM employee JOIN role ON employee.role_id = role.id;",
function (error, information) {
if (error) throw error;
console.log(error);
console.table(information);
init();
}
);
}
//VIEW ALL EMPLOYEES
function viewAllEmployees() {
connection.query(
"SELECT employee.first_name, employee.last_name, role.title, role.salary, department.name, CONCAT(e.first_name, ' ' ,e.last_name) AS Manager FROM employee INNER JOIN role on role.id = employee.role_id INNER JOIN department on department.id = role.department_id left join employee e on employee.manager_id = e.id;",
function (error, information) {
if (error) throw error;
console.table(information);
init();
}
);
}
//UPDATE ROLES
const updateRole = () => {
connection.query(
"SELECT id AS value, CONCAT(first_name, ` ` , last_name) AS name FROM employee",
(error, Employees) => {
connection.query(
"SELECT id AS value, title AS name FROM role",
(error, Roles) => {
inquirer
.prompt([
{
type: "list",
name: "id",
message: "Do yo wanna update employee table?",
choices: Employees,
},
{
type: "list",
name: "role_id",
message: "Do yo wanna update role table?",
choices: Roles,
},
])
.then((answers) => {
connection.query(
"UPDATE employee SET role_id = ? WHERE id = ?",
[answers.role_id, answers.id],
(error) => {
init();
}
);
});
}
);
}
);
};
init();