-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
207 lines (196 loc) · 7.24 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
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
// GIVEN a command-line application that accepts user input
// WHEN I start the application
// THEN I am presented with the following options: view all departments, view all roles, view all employees, add a department, add a role, add an employee, and update an employee role
// WHEN I choose to view all departments
// THEN I am presented with a formatted table showing department names and department ids
// WHEN I choose to view all roles
// THEN I am presented with the job title, role id, the department that role belongs to, and the salary for that role
// WHEN I choose to view all employees
// THEN I am presented with a formatted table showing employee data, including employee ids, first names, last names, job titles, departments, salaries, and managers that the employees report to
// WHEN I choose to add a department
// THEN I am prompted to enter the name of the department and that department is added to the database
// WHEN I choose to add a role
// THEN I am prompted to enter the name, salary, and department for the role and that role is added to the database
// WHEN I choose to add an employee
// THEN I am prompted to enter the employee’s first name, last name, role, and manager, and that employee is added to the database
// WHEN I choose to update an employee role
// THEN I am prompted to select an employee to update and their new role and this information is updated in the database
//Initialize with inquirer prompts to select choices
//If view is selected, present a table with that table's information
//If add is selected, execute new set of inquirer prompts
const cTable = require('console.table');
const mysql = require('mysql2');
const inquirer = require('inquirer');
// Connect to database
const db = mysql.createConnection(
{
host: 'localhost',
// MySQL username,
user: 'root',
// MySQL password
password: 'rootroot',
database: 'employee_tracker_db'
},
console.log(`Connected to the employee_tracker_db database.`)
);
const init = () => {
inquirer
.prompt([
{
type: "list",
message: "Please select from the following options:",
name: "initialize",
choices: [
"View all departments",
"View all roles",
"View all employees",
"Add a department",
"Add a role",
"Add an employee",
"Update an employee role",
"I'm finished"
]
}
]).then(ans => {
// console.log(ans.initialize);
switch (ans.initialize) {
case "View all departments": viewDept();
break;
case "View all roles": viewRoles();
break;
case "View all employees": viewEmployees();
break;
case "Add a department": addDept();
break;
case "Add a role": addRole();
break;
case "Add an employee": addEmployee();
break;
case "Update an employee role": updateEmployee();
break;
case "I'm finished":
console.log("Thank you very much!");
process.exit();
}
}).catch(err => console.error(err));
}
init();
const viewDept = () => {
// console.log("Working")
db.query(`SELECT * FROM department`, (err, results) => {
err ? console.error(err) : console.table(results);
init();
})
};
const viewRoles = () => {
db.query(`SELECT * FROM roles`, (err, results) => {
err ? console.error(err) : console.table(results);
init();
})
};
const viewEmployees = () => {
db.query(`SELECT * FROM employees`, (err, results) => {
err ? console.error(err) : console.table(results);
init();
})
}
const addDept = () => {
inquirer
.prompt([
{
type: "input",
message: "What is the name of the department you'd like to add?",
name: "addDept"
}
]).then(ans => {
db.query(`INSERT INTO department(name)
VALUES(?)`, ans.addDept, (err, results) => {
if (err) {
console.log(err)
} else {
db.query(`SELECT * FROM department`, (err, results) => {
err ? console.error(err) : console.table(results);
init();
})
}
}
)
})
};
const addRole = () => {
const deptChoices = () => db.promise().query(`SELECT * FROM department`)
.then((rows) => {
let arrNames = rows[0].map(obj => obj.name);
return arrNames
})
inquirer
.prompt([
{
type: "input",
message: "What is the title of the role you'd like to add?",
name: "roleTitle"
},
{
type: "input",
message: "What is the salary for this role?",
name: "roleSalary"
},
{
type: "list",
message: "Which department is this role in?",
name: "addDept",
choices: deptChoices
}
]).then(ans => {
db.promise().query(`SELECT id FROM department WHERE name = ?`, ans.addDept)
.then(answer => {
let mappedId = answer[0].map(obj => obj.id);
// console.log(mappedId[0])
return mappedId[0]
})
.then((mappedId) => {
db.promise().query(`INSERT INTO roles(title, salary, department_id)
VALUES(?, ?, ?)`, [ans.roleTitle, ans.roleSalary, mappedId]);
init()
})
})
};
const addEmployee = () => {
// const rollChoices = () => db.promise().query(`SELECT * FROM roles`)
// .then((rows) => {
// let arrNames = rows[0].map(obj => obj.name);
// return arrNames
// })
inquirer
.prompt([
{
type: "input",
message: "What is the employee's first name?",
name: "firstName"
},
{
type: "input",
message: "What is the employee's last name?",
name: "lastName"
},
// {
// type: "list",
// message: "What is the employee's role?",
// name: "employeeRole",
// choices: rollChoices
// }
]).then(ans => {
db.query(`INSERT INTO employees(first_name, last_name)
VALUES(?, ?)`, [ans.firstName, ans.lastName], (err, results) => {
if (err) {
console.log(err)
} else {
db.query(`SELECT * FROM employees`, (err, results) => {
err ? console.error(err) : console.table(results);
init();
})
}
}
)
})
}