-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
309 lines (288 loc) · 9 KB
/
app.js
File metadata and controls
309 lines (288 loc) · 9 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
const inquirer = require('inquirer');
const mysql = require('mysql');
require('dotenv').config();
// Create a connection to the MySQL database AND HIDE IT IN THE ENV FILE
const connection = mysql.createConnection({
host: 'localhost',
user: process.env.DB_USER,
password: process.env.DB_PW,
database: process.env.DB_NAME,
});
// Connect to the database
connection.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err);
return;
}
console.log('Connected to MySQL database');
startApp();
});
// Function to start the application
function startApp() {
inquirer
.prompt({
type: 'list',
name: 'action',
message: 'What would you like to do?',
choices: [
'View Departments',
'View Positions',
'View Employees',
'View Managers',
'Add Department',
'Add Position',
'Add Employee',
'Remove Position',
'Remove Department',
'Remove Employee',
'Exit'],
})
.then((answers) => {
switch (answers.action) {
case 'View Departments':
viewDepartments();
break;
case 'View Positions':
viewPositions();
break;
case 'View Employees':
viewEmployees();
break;
case 'View Managers':
viewManagers();
break;
case 'Add Department':
addDepartment();
break;
case 'Add Position':
addPosition();
break;
case 'Add Employee':
addEmployee();
break;
case 'Remove Position':
removePosition();
break;
case 'Remove Department':
removeDepartment();
break;
case 'Remove Employee':
removeEmployee();
break;
case 'Exit':
connection.end();
console.log('Goodbye!');
break;
default:
console.log('Invalid choice. Please try again.');
startApp();
}
});
}
// Function to view departments
function viewDepartments() {
connection.query('SELECT * FROM departments', (err, results) => {
if (err) throw err;
console.table(results);
startApp();
});
}
// Function to view positions
function viewPositions() {
connection.query('SELECT * FROM positions', (err, results) => {
if (err) throw err;
console.table(results);
startApp();
});
}
// Function to view employees
function viewEmployees() {
connection.query('SELECT * FROM employees', (err, results) => {
if (err) throw err;
console.table(results);
startApp();
});
}
// Function to view managers
function viewManagers() {
connection.query('SELECT * FROM managers', (err, results) => {
if (err) throw err;
console.table(results);
startApp();
});
}
// Function to add a department
function addDepartment() {
inquirer
.prompt({
type: 'input',
name: 'departmentName',
message: 'Enter the name of the department:',
})
.then((answers) => {
const departmentName = answers.departmentName;
// Insert the new department into the database
connection.query('INSERT INTO departments (department_name) VALUES (?)', [departmentName], (err, result) => {
if (err) throw err;
console.log(`Department "${departmentName}" added successfully!`);
startApp();
});
});
}
// Function to remove a department
function removeDepartment() {
inquirer
.prompt({
type: 'input',
name: 'departmentName',
message: 'Enter the name of the department:',
})
.then((answers) => {
const departmentName = answers.departmentName;
// Delete the department from the database
connection.query('DELETE FROM departments WHERE department_name = ?', [departmentName], (err, result) => {
if (err) throw err;
console.log(`Department "${departmentName}" removed successfully!`);
startApp();
});
});
}
// Function to add a position
function addPosition() {
// Fetch the list of departments from the database
connection.query('SELECT id, department_name FROM departments', (err, departmentResults) => {
if (err) throw err;
inquirer
.prompt([
{
type: 'input',
name: 'jobTitle',
message: 'Enter the job title:',
},
{
type: 'input',
name: 'salary',
message: 'Enter the salary:',
},
{
type: 'list',
name: 'departmentId',
message: 'Select the department for the position:',
choices: departmentResults.map(department => ({ name: department.department_name, value: department.id })),
},
])
.then((answers) => {
const jobTitle = answers.jobTitle;
const salary = answers.salary;
const departmentId = answers.departmentId;
// Insert the new position into the database
connection.query('INSERT INTO positions (job_title, salary, department_id) VALUES (?, ?, ?)', [jobTitle, salary, departmentId], (err, result) => {
if (err) throw err;
console.log(`Position "${jobTitle}" added successfully!`);
startApp();
});
});
});
}
// Function to remove a position
function removePosition() {
inquirer
.prompt({
type: 'input',
name: 'jobTitle',
message: 'Enter the job title:',
})
.then((answers) => {
const jobTitle = answers.jobTitle;
// Delete the position from the database
connection.query('DELETE FROM positions WHERE job_title = ?', [jobTitle], (err, result) => {
if (err) throw err;
console.log(`Position "${jobTitle}" removed successfully!`);
startApp();
});
});
}
// Function to add an employee
function addEmployee() {
// Fetch the list of departments from the database
connection.query('SELECT * FROM departments', (err, departmentResults) => {
if (err) throw err;
// Fetch the list of job titles from the database
connection.query('SELECT * FROM positions', (err, positionResults) => {
if (err) throw err;
// Fetch the list of managers' last names from the database
connection.query('SELECT * FROM managers', (err, managerResults) => {
if (err) throw err;
inquirer
.prompt([
{
type: 'input',
name: 'firstName',
message: 'Enter the employee\'s first name:',
},
{
type: 'input',
name: 'lastName',
message: 'Enter the employee\'s last name:',
},
{
type: 'list',
name: 'jobTitle',
message: 'Select the employee\'s job title:',
choices: positionResults.map(position => ({ name: position.job_title, value: position.id })),
},
{
type: 'list',
name: 'departmentId',
message: 'Select the department for the employee:',
choices: departmentResults.map(department => ({ name: department.department_name, value: department.id })),
},
{
type: 'list',
name: 'managerLastName',
message: 'Select the last name of the employee\'s manager:',
choices: managerResults.map(manager => ({ name: manager.last_name, value: manager.id })),
},
])
.then((answers) => {
const firstName = answers.firstName;
const lastName = answers.lastName;
const jobTitleId = answers.jobTitle;
const departmentId = answers.departmentId;
const managerId = answers.managerLastName;
// Insert the new employee into the database
connection.query('INSERT INTO employees (first_name, last_name, job_title, department_id, manager_id) VALUES (?, ?, ?, ?, ?)', [firstName, lastName, jobTitleId, departmentId, managerId], (err, result) => {
if (err) throw err;
console.log(`Employee "${firstName} ${lastName}" added successfully!`);
startApp();
});
});
});
});
});
}
// Function to remove an employee
function removeEmployee() {
// Fetch the list of employees from the database
connection.query('SELECT id, first_name, last_name FROM employees', (err, employeeResults) => {
if (err) throw err;
inquirer
.prompt([
{
type: 'list',
name: 'employeeId',
message: 'Select the employee to remove:',
choices: employeeResults.map(employee => ({ name: `${employee.first_name} ${employee.last_name}`, value: employee.id })),
},
])
.then((answer) => {
const employeeId = answer.employeeId;
// Remove the selected employee from the database
connection.query('DELETE FROM employees WHERE id = ?', [employeeId], (err, result) => {
if (err) throw err;
console.log(`Employee with ID ${employeeId} removed successfully!`);
startApp();
});
});
});
}