-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
217 lines (168 loc) · 4.23 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
'use strict';
const inquirer = require('inquirer');
const logo = require('asciiart-logo');
const prompts = require('./prompts');
const db = require('./db');
const { right } = require('inquirer/lib/utils/readline');
require('console.table');
function exit() {
db.end();
process.on('exit', function (code) {
return console.log(`Ending Good bye ${code}`);
});
process.exit();
}
async function viewAllDepartment() {
const department = await db.viewAllDepartment();
console.log('\n');
console.table(department);
mainPrompt();
}
async function viewAllRoles() {
const role = await db.viewAllRoles();
console.log('\n');
console.table(role);
mainPrompt();
}
async function viewAllEmployees() {
const employee = await db.viewAllEmployees();
console.log('\n');
console.table(employee);
mainPrompt();
}
async function addDepartment() {
const { name } = await inquirer.prompt(prompts.addDepartment);
const newDept = await db.addDepartment(name);
console.log('\n');
console.log(`New Department ${name} added.`);
mainPrompt();
}
async function addRole() {
const {
title,
salary,
department_id
} = await inquirer.prompt(prompts.addRole);
const newRole = await db.addRole(
title,
salary,
department_id
);
console.log('\n');
console.log(
`New Role added:
Title: ${title}
Salary: ${salary}
Department_ID: ${department_id}
`
);
mainPrompt();
}
async function addEmployee() {
const {
first_name,
last_name,
role_id,
manager_id
} = await inquirer.prompt(prompts.addEmployee);
const newEmployee = await db.addEmployee(first_name,
last_name,
role_id,
manager_id);
console.log('\n');
console.log(
`New Role added:
First Name: ${first_name}
Last Name: ${last_name}
Role_ID: ${role_id}
Manager_ID: ${manager_id}
`
);
mainPrompt();
}
async function updateEmployee() {
const { id } = await inquirer.prompt(prompts.selectEmployee_askID);
const selectedEmployee = await db.viewEmployee(id);
console.log('\n');
console.table(selectedEmployee);
const {
first_name: firstName,
last_name: lastName,
role_id: roleID,
manager_id: managerID
} = selectedEmployee[0];
const updateEmployee_details = prompts.updateEmployee_function(
firstName,
lastName,
roleID,
managerID
);
const {
first_name,
last_name,
role_id,
manager_id
} = await inquirer.prompt(updateEmployee_details);
const updatedEmployee = await db.updateEmployee(
id,
first_name,
last_name,
role_id,
manager_id
);
const newSelectedEmployee = await db.viewEmployee(id);
console.log('\n');
console.log(`Update ${id} new employee:`);
console.table(newSelectedEmployee);
mainPrompt();
}
async function mainPrompt() {
const { mainPrompt } = await inquirer.prompt(prompts.mainPrompt);
switch (mainPrompt) {
case "View All Departments":
viewAllDepartment();
break;
case "View All Roles":
viewAllRoles();
break;
case "View All Employees":
viewAllEmployees();
break;
case "Add New Department":
addDepartment();
break;
case "Add New Role":
addRole();
break;
case "Add New Employee":
addEmployee();
break;
case "Update Employee":
updateEmployee();
break;
case "Exit":
exit();
break;
}
}
function myLogo() {
console.log(
logo({
name: 'Employee Tracker',
font: '',
lineChars: 0.5,
borderColor: 'yellow',
logoColor: 'bold-blue',
textColor: 'white',
})
.emptyLine()
.center('Here is the Employee Tracker.')
.render()
);
}
function init() {
myLogo();
console.log("Employee Tracker");
mainPrompt();
}
init();