-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
260 lines (222 loc) · 7.81 KB
/
index.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
require('dotenv').config();
const questions = require('./questions.js');
const mysql = require('mysql2/promise');
const inquirer = require('inquirer');
async function init() {
const db = await mysql.createConnection({
host: 'localhost',
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME
});
db.connect( (error) => {
if (error) {
console.error(error);
};
});
async function menuLoop () {
var menuChoice = "";
do {
menuChoice = await viewMenu();
await enactChoice(menuChoice);
console.log('------');
} while (menuChoice != 'Quit');
};
async function viewMenu() {
try {
const mainMenu = await inquirer.prompt(questions.menu);
return mainMenu.menuChoice;
} catch (error) {
console.error(error);
}
};
async function enactChoice (menuChoice) {
switch (menuChoice) {
case 'View All Departments':
await viewDepts();
break;
case 'View All Roles':
await viewRoles();
break;
case 'View All Employees':
await viewEmployees();
break;
case 'Add A Department':
await addDept();
break;
case 'Add A Role':
await addRole();
break;
case 'Add An Employee':
await addEmployee();
break;
case 'Update An Employee Role':
await updateEmployee();
break;
default:
return quit();
};
};
async function quit() {
console.log('Thank you for using Simple Employee Department CMS');
await db.end();
};
// Get from the db
async function getDepts(id) {
if (id === undefined) {
id = '%';
};
try {
const depts = await db.execute(`SELECT * FROM departments WHERE id LIKE ?;`, [id]);
return depts[0];
} catch (error) {
console.error(error);
};
};
async function getRoles(id) {
if (id === undefined) {
id = '%';
};
try {
const roles = await db.execute(`
SELECT
roles.id,
roles.role_title,
roles.salary,
departments.department_name
FROM roles
LEFT JOIN departments ON departments.id = roles.department_id
WHERE roles.id LIKE ?;`, [id]);
return roles[0];
} catch (error) {
console.error(error);
};
};
async function getEmployees(id) {
if (id === undefined) {
id = '%';
};
try {
const emps = await db.execute(`
SELECT
employees.id,
employees.first_name,
employees.last_name,
departments.department_name,
roles.role_title,
roles.salary,
CONCAT(managers.first_name,' ', managers.last_name) AS manager
FROM employee_db.employees
LEFT JOIN
employees AS managers ON employees.manager_id = managers.id
LEFT JOIN
roles ON roles.id = employees.role_id
LEFT JOIN
departments ON departments.id = roles.department_id
WHERE employees.id LIKE ?;`, [id]);
return emps[0];
} catch (error) {
console.error(error);
};
};
// View results
async function viewDepts(id) {
try {
console.table(await getDepts(id));
} catch (error) {
console.error(error);
}
};
async function viewRoles(id) {
try {
console.table(await getRoles(id));
} catch (error) {
console.error(error);
}
};
async function viewEmployees(id) {
try {
console.table(await getEmployees(id));
} catch (error) {
console.error(error);
}
};
// Add to db
async function addDept() {
try {
const newDept = await inquirer.prompt(questions.addDeptQ);
const dbAddRow = await db.execute("INSERT INTO departments (department_name) VALUES (?)", [newDept.deptName]);
console.log('\nAdded to departments:');
await viewDepts(dbAddRow[0].insertId);
} catch (error) {
console.error(error);
};
};
async function addRole() {
try {
const deptList = await getDepts();
deptList.forEach((department) => {
questions.addRoleQ[2].choices.push({
name: department.department_name,
value: department.id,
});
});
const newRole = await inquirer.prompt(questions.addRoleQ);
const dbAddRow = await db.execute("INSERT INTO roles SET role_title = ?, salary = ?, department_id = ?;", [newRole.roleTitle, newRole.roleSalary, newRole.roleDept]);
console.log('\nAdded to roles:');
await viewRoles(dbAddRow[0].insertId);
} catch (error) {
console.error(error);
};
};
async function addEmployee() {
try {
const roleList = await getRoles();
roleList.forEach((role) => {
questions.addEmployeeQ[2].choices.push({
name: role.role_title,
value: role.id,
});
});
const employeesList = await getEmployees();
employeesList.forEach((employee) => {
questions.addEmployeeQ[3].choices.push({
name: employee.first_name + ' ' + employee.last_name,
value: employee.id,
});
});
const newEmployee = await inquirer.prompt(questions.addEmployeeQ);
const dbAddRow = await db.execute("INSERT INTO employees SET first_name = ?, last_name = ?, role_id = ?, manager_id = ?;", [newEmployee.firstName, newEmployee.lastName, newEmployee.employeeRole, newEmployee.employeeManager]);
console.log('\nAdded to employees:');
await viewEmployees(dbAddRow[0].insertId);
} catch (error) {
console.error(error);
};
};
async function updateEmployee() {
try {
const employeesList = await getEmployees();
employeesList.forEach((employee) => {
questions.updateEmployeeQ[0].choices.push({
name: employee.first_name + ' ' + employee.last_name,
value: employee.id,
});
});
const roleList = await getRoles();
roleList.forEach((role) => {
questions.updateEmployeeQ[1].choices.push({
name: role.role_title,
value: role.id,
});
});
const updateEmployee = await inquirer.prompt(questions.updateEmployeeQ);
await db.execute("UPDATE employees SET role_id = ? WHERE id = ?;", [updateEmployee.updatedRole, updateEmployee.employeeToUpdate]);
console.log('\nUpdated employee:');
await viewEmployees(updateEmployee.employeeToUpdate);
} catch (error) {
console.error(error);
};
};
menuLoop();
};
init();