-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
222 lines (204 loc) · 5.69 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
218
219
220
221
222
const mysql = require('mysql2');
const db = require('./db/connection');
const inquirer = require('inquirer');
const table = require('console.table');
//Start server after DB connection.
db.connect(err => {
if (err) throw err;
console.log('Database connected');
});
function init() {
inquirer.prompt([{
type: 'list',
name: 'menu',
message: 'What information would you like to see?',
choices: [
'View All Departments',
'View All Roles',
'View All Employees',
'Add A Department',
'Add A Role',
'Add An Employee',
// 'Update An Employee Role',
'Exit'
]
}]).then(({ menu }) => {
switch(menu) {
case 'View All Departments':
getAllDepartments();
break;
case 'View All Roles':
getAllRoles();
break;
case 'View All Employees':
getAllEmployees();
break;
case 'Add A Department':
addNewDepartment();
break;
case 'Add A Role':
addNewRole();
break;
case 'Add An Employee':
addNewEmployee();
break;
// case 'Update An Employee Role':
// updateEmployeeRole();
// break;
case 'Exit':
process.exit();
};
});
};
//Call for View All Departments.
function getAllDepartments() {
const sql = `SELECT * FROM departments`;
db.query(sql, (err, res) => {
if (err) {
throw err;
} else {
console.log("\n");
console.table(res);
}
init()
});
};
//Call for View All Roles.
async function getAllRoles() {
const sql = `SELECT * FROM roles`;
db.query(sql, (err, res) => {
if (err) {
throw err;
} else {
console.log("\n");
console.table(res);
}
init()
});
};
//Call for View All Employees.
function getAllEmployees() {
const sql = `SELECT * FROM employees`;
db.query(sql, (err, res) => {
if (err) {
throw err;
} else {
console.log("\n");
console.table(res);
}
init()
});
};
//Call for Add A Department.
function addNewDepartment() {
inquirer.prompt({
type: 'input',
name: 'addNewDepartment',
message: 'What department would you like to add?'
}).then(({ addNewDepartment }) => {
const sql = `INSERT INTO departments (dept_name)
VALUES (?)`;
const params = [addNewDepartment]
db.query(sql, params, err => {
if (err) {
throw err;
} else {
getAllDepartments();
};
});
});
};
//Call for Add A Role.
async function addNewRole() {
inquirer.prompt([
{
type: 'input',
name: 'title',
message: 'What role would you like to add?'
},
{
type: 'input',
name: 'salary',
message: 'What is the salary for the role?'
},
{
type: 'input',
name: 'department_id',
message: 'What is the department_id of the role?'
}
]).then(({ title, salary, department_id }) => {
const sql = `INSERT INTO roles (title, salary, department_id)
VALUES (?,?,?)`;
const params = [title, salary, department_id];
db.query(sql, params, async(err) => {
if (err) {
throw err;
} else {
await getAllRoles();
};
});
});
};
//Call for Add An Employee.
async function addNewEmployee() {
inquirer.prompt([
{
type: 'input',
name: 'first_name',
message: "What is the new employee's first name?"
},
{
type: 'input',
name: 'last_name',
message: "What is the new employee's last name?"
},
{
type: 'input',
name: 'role_id',
message: "What is the new employee's role ID?"
},
{
type: 'input',
name: 'manager_id',
message: "Who is the new employee's manager ID?"
}
]).then(({ first_name, last_name, role_id, manager_id }) => {
const sql = `INSERT INTO employees (first_name, last_name, role_id, manager_id)
VALUES (?,?,?,?)`;
const params = [first_name, last_name, role_id, manager_id];
db.query(sql, params, async(err) => {
if (err) {
throw err;
} else {
await getAllEmployees();
}
});
});
};
//Call for Update An Employee Role.
// function updateEmployeeRole() {
// return inquirer.prompt([
// {
// type: 'input',
// name: 'chooseEmployee',
// message: 'Choose an employee to update their role'
// },
// {
// type: 'input',
// name: 'updateRole',
// message: 'What role do you want to update this employee to?'
// }
// ]).then(({ chooseEmployee, updateRole }) => {
// const sql = `UPDATE employees SET role_id = ? WHERE id = ?`;
// const params = [chooseEmployee, updateRole];
// db.query(sql, params, err => {
// if (err) {
// throw err
// } else {
// getAllEmployees();
// }
// });
// init();
// });
// };
init();