-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (108 loc) · 4.2 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
//imports inquirer
const inquirer = require('inquirer');
//imports the additonal prompt questions for the user choices
const sideQuestions = require('./sideQuestions.js');
//imports functions stored in the functions.js to interact with database
const dbQueryFunctions = require('./functions.js');
/* [async]asynchronous function
--allowes to use "await"
*/
async function employeeTrackerMenu() {
process.stdout.write('\x1Bc');
/* [try] - creates a block of code for testing errors
if error happens it gets send to catch
also prevents code form crashing, if errors happens
*/
try {
/* starts Questions point
[await] + [inquirer.prompt()] -> await pauses the function execution until
prompt is completed and resuslts obtained
*/
process.stdout.write('\x1Bc');
const { menuOption } = await inquirer.prompt([
{
type: 'list',
name: 'menuOption',
message: 'What would you like to do?',
choices: [
'View Departments',
'View Roles',
'View Employees',
'Add Department',
'Add Role',
'Add an Employee',
'Update Employee',
'Quit'
]
}
]);
process.stdout.write('\x1Bc');
// [switch] used to execute diffrent blocks of code depending on the user choice, in this case
switch (menuOption) {
case 'View Departments':
process.stdout.write('\x1Bc');
await dbQueryFunctions.departments(); //calls the function that will execute te query to interact with database, [await] pauses until task is completed
break;
case 'View Roles':
process.stdout.write('\x1Bc');
await dbQueryFunctions.roles();
break;
case 'View Employees':
process.stdout.write('\x1Bc');
await dbQueryFunctions.employees();
break;
case 'Add Department':
//this section works similar to the the previous prompt,
// since it was just one needed input, did not built a seprate function
process.stdout.write('\x1Bc');
const { DepartmentName } = await inquirer.prompt([
{
type: 'input',
name: 'DepartmentName',
message: 'Enter Department Name:',
//trim() removed realding and trailing whitespace from the imput, if its an emty string it will prompt a message
validate: input => input.trim() === '' ? 'Please Enter a Name:' : true
}
]);
//once input is executed, function called to send info to database
await dbQueryFunctions.addDepartment(DepartmentName);
await dbQueryFunctions.departments();
break;
case 'Add Role':
process.stdout.write('\x1Bc');
const roleAnswers = await inquirer.prompt(sideQuestions.newRoleQuestions);
await dbQueryFunctions.addRoles(roleAnswers.title, roleAnswers.salary, roleAnswers.department_id);
await dbQueryFunctions.roles();
break;
case 'Add an Employee':
process.stdout.write('\x1Bc');
const employeeAnswers = await inquirer.prompt(sideQuestions.newEmployeeQuestions);
await dbQueryFunctions.addEmployee(employeeAnswers.first_name, employeeAnswers.last_name, employeeAnswers.role_id, employeeAnswers.manager_id);
await dbQueryFunctions.fullEmployeeData();
break;
case 'Update Employee':
process.stdout.write('\x1Bc');
const updateAnswers = await inquirer.prompt(sideQuestions.updateEmployeeQuestions);
await dbQueryFunctions.updateEmployeeRole(updateAnswers.employeeId, updateAnswers.newRoleId);
await dbQueryFunctions.fullEmployeeData();
break;
case 'Quit':
process.stdout.write('\x1Bc'); // clears terminal
console.log('Goodbye!');
process.exit(); // ends node process
default:
console.log('Invalid option selected.');
break;
}
// Prompt the menu again if not quitting
if (menuOption !== 'Quit') {
process.stdout.write('\x1Bc');
await employeeTrackerMenu();
}
/* [catch] - will handle any errors that may occur in hte [try] block. */
} catch (error) {
console.error('An error occurred:', error);
}
}
// Start the menu
employeeTrackerMenu();