Skip to content

Commit 4fc9885

Browse files
committed
cleaned up code in the CLI class, and commented out console logs- moved printTable method to the CLI class as opposed to the Interface class, corrected error when adding a manager to the db
1 parent 7ed2e7b commit 4fc9885

File tree

2 files changed

+107
-73
lines changed

2 files changed

+107
-73
lines changed

lib/CLI.js

+92-21
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22
const Interface = require('./Interface');
33
const inquirer = require('inquirer');
44
inquirer.registerPrompt('search-list', require('inquirer-search-list'));
5-
const artwork = require('ascii-art');
5+
const cTable = require('console.table');
66

77
const queryInterface = new Interface();
88

99
class CommandLinePrompts {
1010
constructor() {
11-
11+
//used by printTable method
12+
this.printValues = [
13+
//example according to console.table README
14+
//['name', 'age'],
15+
//['max', 20],
16+
//['joe', 30]
17+
];
1218
}
1319

1420
optionMenu() {
@@ -59,8 +65,19 @@ class CommandLinePrompts {
5965
}
6066

6167
viewDepartments(){
68+
69+
console.log(`
70+
=======================================
71+
Viewing All Departments
72+
=======================================
73+
`);
74+
6275
queryInterface.allDepartments()
63-
.then(() => {
76+
.then((rows) => {
77+
//print db data from query
78+
this.printValues = rows;
79+
this.printTable();
80+
6481
inquirer.prompt({
6582
type: 'confirm',
6683
name: 'userChoice',
@@ -77,8 +94,19 @@ class CommandLinePrompts {
7794
}
7895

7996
viewRoles() {
97+
98+
console.log(`
99+
=======================================
100+
Viewing All Roles
101+
=======================================
102+
`);
103+
80104
queryInterface.allRoles()
81-
.then(() => {
105+
.then((rows) => {
106+
//print db data from query
107+
this.printValues = rows;
108+
this.printTable();
109+
82110
inquirer.prompt({
83111
type: 'confirm',
84112
name: 'userChoice',
@@ -95,8 +123,19 @@ class CommandLinePrompts {
95123
}
96124

97125
viewEmployees() {
126+
127+
console.log(`
128+
=======================================
129+
Viewing All Employees
130+
=======================================
131+
`);
132+
98133
queryInterface.allEmployees()
99-
.then(() => {
134+
.then((rows) => {
135+
//print db data from query
136+
this.printValues = rows;
137+
this.printTable();
138+
100139
inquirer.prompt({
101140
type: 'confirm',
102141
name: 'userChoice',
@@ -113,6 +152,13 @@ class CommandLinePrompts {
113152
}
114153

115154
addDepartmentMenu() {
155+
156+
console.log(`
157+
=======================================
158+
Add a Department
159+
=======================================
160+
`);
161+
116162
inquirer.prompt(
117163
{
118164
type: 'input',
@@ -148,14 +194,21 @@ class CommandLinePrompts {
148194
}
149195

150196
addRoleMenu() {
197+
198+
console.log(`
199+
=======================================
200+
Add a Role
201+
=======================================
202+
`);
203+
151204
queryInterface.allDepartments()
152205
.then(RowDataPacket => {
153206
//valid RowDataPacket conversion solution #1
154207
//let packetObj = Object.assign({}, RowDataPacket);
155208
//valid RowDataPacket conversion solution #2
156209
const packetArr = Array.from(RowDataPacket);
157210
//console.log(packetObj);
158-
console.log(packetArr);
211+
//console.log(packetArr);
159212

160213
const departmentNamesArr = packetArr.map(packet => {
161214
return packet.name;
@@ -182,7 +235,7 @@ class CommandLinePrompts {
182235
validate: salary => {
183236
let salaryInt = Number.parseInt(salary);
184237

185-
console.log(salaryInt);
238+
//console.log(salaryInt);
186239

187240
if(!salaryInt || salaryInt === NaN) {
188241
console.log("Please enter a salary value!");
@@ -203,9 +256,9 @@ class CommandLinePrompts {
203256
}
204257
])
205258
.then((answers)=> {
206-
console.log(answers);
259+
//console.log(answers);
207260
answers.salary = Number.parseFloat(answers.salary).toFixed(2);
208-
console.log(answers);
261+
//console.log(answers);
209262
const updatedRole = {
210263
title: answers.role,
211264
salary: Number.parseFloat(answers.salary).toFixed(2),
@@ -221,7 +274,7 @@ class CommandLinePrompts {
221274
//package up the data to send to the db
222275
updatedRole.department_id = packetFound[0].id.toString();
223276

224-
console.log(updatedRole);
277+
//console.log(updatedRole);
225278

226279
queryInterface.addRole(updatedRole)
227280
.then(() => {
@@ -243,6 +296,13 @@ class CommandLinePrompts {
243296
}
244297

245298
addEmployeeMenu() {
299+
300+
console.log(`
301+
=======================================
302+
Add an Employee
303+
=======================================
304+
`);
305+
246306
queryInterface.allEmployees()
247307
.then(RowDataPacketEmployees => {
248308
//valid RowDataPacket conversion solution #2
@@ -257,7 +317,7 @@ class CommandLinePrompts {
257317
}).map(manager => {
258318
return manager.Employee;
259319
});
260-
console.log(managerNamesArr);
320+
//console.log(managerNamesArr);
261321

262322
queryInterface.allRoles()
263323
.then(RowDataPacketRoles => {
@@ -267,7 +327,7 @@ class CommandLinePrompts {
267327
const rolesArr = rolesPacketArr.map(role => {
268328
return role.title;
269329
});
270-
console.log(rolesArr);
330+
//console.log(rolesArr);
271331

272332
inquirer.prompt([
273333
{
@@ -312,7 +372,7 @@ class CommandLinePrompts {
312372
])
313373
.then((answers)=> {
314374

315-
console.log(answers);
375+
//console.log(answers);
316376

317377
const newEmployee = {
318378
first_name: answers.first_name,
@@ -332,18 +392,19 @@ class CommandLinePrompts {
332392
return packet.id;
333393
}
334394
});
335-
395+
//package up the data to send to the db
396+
newEmployee.manager_id = manager[0].id.toString();
397+
}
336398
let role = rolesPacketArr.filter(packet => {
337399
if(answers.role === packet.title) {
338400
return packet.id;
339401
}
340402
});
403+
341404
//package up the data to send to the db
342-
newEmployee.manager_id = manager[0].id.toString();
343405
newEmployee.role_id = role[0].id.toString();
344-
}
345406

346-
console.log(newEmployee);
407+
//console.log(newEmployee);
347408

348409
queryInterface.addEmployee(newEmployee)
349410
.then(() => {
@@ -366,6 +427,13 @@ class CommandLinePrompts {
366427
}
367428

368429
updateEmployeeRoleMenu() {
430+
431+
console.log(`
432+
=======================================
433+
Update an Employee's Role
434+
=======================================
435+
`);
436+
369437
queryInterface.allEmployees()
370438
.then(RowDataPacketEmployees => {
371439
//valid RowDataPacket conversion solution #2
@@ -375,7 +443,7 @@ class CommandLinePrompts {
375443
return employee.Employee;
376444
});
377445

378-
console.log(employeeNamesArr);
446+
//console.log(employeeNamesArr);
379447

380448
queryInterface.allRoles()
381449
.then(RowDataPacketRoles => {
@@ -385,7 +453,7 @@ class CommandLinePrompts {
385453
const rolesArr = rolesPacketArr.map(role => {
386454
return role.title;
387455
});
388-
console.log(rolesArr);
456+
//console.log(rolesArr);
389457

390458
inquirer.prompt([
391459
{
@@ -403,7 +471,7 @@ class CommandLinePrompts {
403471

404472
])
405473
.then((answers) => {
406-
console.log(answers);
474+
//console.log(answers);
407475

408476
const updatedEmployee = {
409477
id: '',
@@ -426,7 +494,7 @@ class CommandLinePrompts {
426494
updatedEmployee.id = employee[0].id.toString();
427495
updatedEmployee.role_id = role[0].id.toString();
428496

429-
console.log(updatedEmployee);
497+
//console.log(updatedEmployee);
430498

431499
queryInterface.updateRole(updatedEmployee)
432500
.then(() => {
@@ -449,6 +517,9 @@ class CommandLinePrompts {
449517

450518
});
451519
}
520+
printTable() {
521+
console.table(this.printValues);
522+
}
452523
}
453524

454525
module.exports = CommandLinePrompts;

0 commit comments

Comments
 (0)