-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp.js
268 lines (244 loc) · 7.39 KB
/
app.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
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
//======== Dependencies===================//
const inquirer = require("inquirer")
const mysql = require("mysql")
const cTable = require('console.table');
const connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "password",
database: "employee_trackerDB"
});
//========== Connection ID ==========================//
connection.connect(function(err) {
if (err) throw err
console.log("Connected as Id" + connection.threadId)
startPrompt();
});
//================== Initial Prompt =======================//
function startPrompt() {
inquirer.prompt([
{
type: "list",
message: "What would you like to do?",
name: "choice",
choices: [
"View All Employees?",
"View All Employee's By Roles?",
"View all Emplyees By Deparments",
"Update Employee",
"Add Employee?",
"Add Role?",
"Add Department?"
]
}
]).then(function(val) {
switch (val.choice) {
case "View All Employees?":
viewAllEmployees();
break;
case "View All Employee's By Roles?":
viewAllRoles();
break;
case "View all Emplyees By Deparments":
viewAllDepartments();
break;
case "Add Employee?":
addEmployee();
break;
case "Update Employee":
updateEmployee();
break;
case "Add Role?":
addRole();
break;
case "Add Department?":
addDepartment();
break;
}
})
}
//============= View All Employees ==========================//
function viewAllEmployees() {
connection.query("SELECT employee.first_name, employee.last_name, role.title, role.salary, department.name, CONCAT(e.first_name, ' ' ,e.last_name) AS Manager FROM employee INNER JOIN role on role.id = employee.role_id INNER JOIN department on department.id = role.department_id left join employee e on employee.manager_id = e.id;",
function(err, res) {
if (err) throw err
console.table(res)
startPrompt()
})
}
//============= View All Roles ==========================//
function viewAllRoles() {
connection.query("SELECT employee.first_name, employee.last_name, role.title AS Title FROM employee JOIN role ON employee.role_id = role.id;",
function(err, res) {
if (err) throw err
console.table(res)
startPrompt()
})
}
//============= View All Employees By Departments ==========================//
function viewAllDepartments() {
connection.query("SELECT employee.first_name, employee.last_name, department.name AS Department FROM employee JOIN role ON employee.role_id = role.id JOIN department ON role.department_id = department.id ORDER BY employee.id;",
function(err, res) {
if (err) throw err
console.table(res)
startPrompt()
})
}
//================= Select Role Quieries Role Title for Add Employee Prompt ===========//
var roleArr = [];
function selectRole() {
connection.query("SELECT * FROM role", function(err, res) {
if (err) throw err
for (var i = 0; i < res.length; i++) {
roleArr.push(res[i].title);
}
})
return roleArr;
}
//================= Select Role Quieries The Managers for Add Employee Prompt ===========//
var managersArr = [];
function selectManager() {
connection.query("SELECT first_name, last_name FROM employee WHERE manager_id IS NULL", function(err, res) {
if (err) throw err
for (var i = 0; i < res.length; i++) {
managersArr.push(res[i].first_name);
}
})
return managersArr;
}
//============= Add Employee ==========================//
function addEmployee() {
inquirer.prompt([
{
name: "firstname",
type: "input",
message: "Enter their first name "
},
{
name: "lastname",
type: "input",
message: "Enter their last name "
},
{
name: "role",
type: "list",
message: "What is their role? ",
choices: selectRole()
},
{
name: "choice",
type: "rawlist",
message: "Whats their managers name?",
choices: selectManager()
}
]).then(function (val) {
var roleId = selectRole().indexOf(val.role) + 1
var managerId = selectManager().indexOf(val.choice) + 1
connection.query("INSERT INTO employee SET ?",
{
first_name: val.firstName,
last_name: val.lastName,
manager_id: managerId,
role_id: roleId
}, function(err){
if (err) throw err
console.table(val)
startPrompt()
})
})
}
//============= Update Employee ==========================//
function updateEmployee() {
connection.query("SELECT employee.last_name, role.title FROM employee JOIN role ON employee.role_id = role.id;", function(err, res) {
// console.log(res)
if (err) throw err
console.log(res)
inquirer.prompt([
{
name: "lastName",
type: "rawlist",
choices: function() {
var lastName = [];
for (var i = 0; i < res.length; i++) {
lastName.push(res[i].last_name);
}
return lastName;
},
message: "What is the Employee's last name? ",
},
{
name: "role",
type: "rawlist",
message: "What is the Employees new title? ",
choices: selectRole()
},
]).then(function(val) {
var roleId = selectRole().indexOf(val.role) + 1
connection.query("UPDATE employee SET WHERE ?",
{
last_name: val.lastName
},
{
role_id: roleId
},
function(err){
if (err) throw err
console.table(val)
startPrompt()
})
});
});
}
//============= Add Employee Role ==========================//
function addRole() {
connection.query("SELECT role.title AS Title, role.salary AS Salary FROM role", function(err, res) {
inquirer.prompt([
{
name: "Title",
type: "input",
message: "What is the roles Title?"
},
{
name: "Salary",
type: "input",
message: "What is the Salary?"
}
]).then(function(res) {
connection.query(
"INSERT INTO role SET ?",
{
title: res.Title,
salary: res.Salary,
},
function(err) {
if (err) throw err
console.table(res);
startPrompt();
}
)
});
});
}
//============= Add Department ==========================//
function addDepartment() {
inquirer.prompt([
{
name: "name",
type: "input",
message: "What Department would you like to add?"
}
]).then(function(res) {
var query = connection.query(
"INSERT INTO department SET ? ",
{
name: res.name
},
function(err) {
if (err) throw err
console.table(res);
startPrompt();
}
)
})
}