-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
226 lines (201 loc) · 8.9 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
// import libraries, files, and dependencies
const cli = require('./lib/cli');
const Department = require('./lib/department');
const Role = require('./lib/role');
const Employee = require('./lib/employee');
require('dotenv').config();
const MySQL = require('mysql2');
const { table } = require('console');
// connection to database
const db = MySQL.createConnection(
{
host: 'localhost',
// MySQL username,
user: 'root',
// MySQL password
password: process.env.DB_PASSWORD,
database: 'company_db'
},
console.log(`Connected to the company_db database.`)
);
const department = new Department(db);
const role = new Role(db);
const employee = new Employee(db);
// switch statement for user to navigate the app
const askQuestions = async () => {
console.log(`
______ __ __ _____ _ ______ ________ ______
| ____| \\/ | __ \\| | / __ \\ \\ / / ____| ____|
| |__ | \\ / | |__) | | | | | \\ \\_/ /| |__ | |__
| __| | |\\/| | ___/| | | | | |\\ / | __| | __|
| |____| | | | | | |___| |__| | | | | |____| |____
|______|_| |_|_| |______\\____/ |_| |______|______|
__ __ _ _ _____ ______ _____
| \\/ | /\\ | \\ | | /\\ / ____| ____| __ \\
| \\ / | / \\ | \\| | / \\ | | __| |__ | |__) |
| |\\/| | / /\\ \\ | . ' | / /\\ \\| | |_ | __| | _ /
| | | |/ ____ \\| |\\ |/ ____ \\ |__| | |____| | \\ \\
|_| |_/_/ \\_\\_| \\_/_/ \\_\\_____|______|_| \\_\\
`);
// app runs forever untill user chooses 'Quit' option
while (true) {
try {
// initial/main cli for the user
const data = await cli.mainCli();
// instances of the child classes returning arrays
const getRoles = await role.getRoles();
const getEmployees = await employee.getEmployees();
const getDepartments = await department.getDepartments();
let tableData;
switch (data.choice) {
case 'View All Departments':
try {
tableData = await department.view();
console.table(tableData);
}
catch (error) {
console.error(`Error Viewing Departments: ${error}`);
}
break;
case 'Add Department':
try {
const newDepData = await cli.depCli();
const result = await department.enter(newDepData.department);
console.info(result);
}
catch (error) {
console.error(`Error Adding Department: ${error}`);
}
break;
case 'View All Roles':
try {
tableData = await role.view();
console.table(tableData);
}
catch (error) {
console.error(`Error Viewing Roles: ${error}`);
}
break;
case 'Add Role':
try {
const newRolData = await cli.rolCli(getDepartments);
const result = await role.enter(newRolData.role, newRolData.salary, newRolData.department);
console.info(result);
}
catch (error) {
console.error(`Error Adding Role: ${error}`);
}
break;
case 'View All Employees':
try {
tableData = await employee.view();
console.table(tableData);
}
catch (error) {
console.error(`Error Viewing Employees: ${error}`);
}
break;
case 'Add Employee':
try {
const newEmpData = await cli.empCli(getRoles, getEmployees);
const result = await employee.enter(newEmpData.firstname, newEmpData.lastname, newEmpData.role, newEmpData.manager);
console.info(result);
}
catch (error) {
console.error(`Error Adding Employee: ${error}`);
}
break;
case 'Update Employee Role':
try {
const updateEmpRole = await cli.empRolCli(getEmployees, getRoles);
const result = await employee.updateRole(updateEmpRole.updateEmployee, updateEmpRole.newRole);
console.info(result);
}
catch (error) {
console.error(`Error Updating Employee Role: ${error}`);
}
break;
case 'Update Employee Manager':
try {
const updateEmpManager = await cli.empManCli(getEmployees, getEmployees);
result = await employee.updateManager(updateEmpManager.updateEmployee, updateEmpManager.newManager);
console.info(result);
}
catch (error) {
console.error(`Error Updating Employee Manager: ${error}`);
}
break;
case 'View Employees By Manager':
try {
const chosenManager = await cli.empByManagerCli(getEmployees);
tableData = await employee.viewEmpByManager(chosenManager.manager);
console.table(tableData);
}
catch (error) {
console.error(`Error Viewing ${chosenManager.manager}'s Employees: ${error}`);
}
break;
case 'View Employees By Department':
try {
const chosenDepartment = await cli.empByDepartmentCli(getDepartments);
tableData = await employee.viewEmpByDepartment(chosenDepartment.department);
console.table(tableData);
}
catch (error) {
console.error(`Error Viewing ${chosenDepartment.department}'s Employees: ${error}`);
}
break;
case 'Delete Department':
try {
const deleteDepartment = await cli.delDepCli(getDepartments);
const result = await department.delete(deleteDepartment.department);
console.info(result);
}
catch (error) {
console.error(`Error Deleting ${deleteDepartment.department}: ${error}`);
}
break;
case 'Delete Role':
try {
const deleteRole = await cli.delRolCli(getRoles);
const result = await role.delete(deleteRole.role);
console.info(result);
}
catch (error) {
console.error(`Error Deleting ${deleteRole.role}: ${error}`);
}
break;
case 'Delete Employee':
try {
const deleteEmployee = await cli.delEmpCli(getEmployees);
const result = await employee.delete(deleteEmployee.employee);
console.info(result);
}
catch (error) {
console.error(`Error Deleting ${deleteEmployee.employee}: ${error}`);
}
break;
case 'View Department Budget':
try {
tableData = await department.depBudget();
console.table(tableData);
}
catch (error) {
console.error(`Error View ${viewDepBudget.department}'s Total Utilized Budget: ${error}`);
}
break;
case 'Quit':
console.log('Goodbye!');
// end the app
process.exit();
default:
console.log('Invalid choice. Please select a valid option.');
}
}
catch (error) {
console.error(`CLI Error: ${error}`);
}
}
};
// initiate the app
askQuestions();