-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
176 lines (166 loc) · 5.09 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
const inquirer = require('inquirer');
const connection = require("./connection");
require("console.table");
function viewDepartment() {
console.log("View all departments")
connection.promise().query("SELECT id, name FROM department;")
.then(result => {
console.table(result[0]);
mainMenu();
})
}
function createDepartment() {
console.log("Create Department")
inquirer.prompt([
{
name: "name",
message: "What is the name of the department?"
}
]).then(input => {
let name = input;
connection.promise().query("INSERT INTO department SET ?", name)
.then(() => console.log(`Added ${name.name} to the database`))
.then(() => mainMenu())
})
}
function createRole() {
console.log("Create Role")
inquirer.prompt([
{
name: "name",
message: "What is the name of the role?"
},
{
name: "salary",
message: "What is the salary of the role?"
},
{
name: "department_id",
message: "What is the department id of the role?"
}
]).then(input => {
let title = input;
console.log(title);
connection.query("INSERT INTO role (title, salary, department_id) VALUES ?", [[title.name, parseInt(title.salary), parseInt(title.department_id)]], () => {
console.log(`Added ${title.name} to the database`)
mainMenu()
})
// .then(() => console.log(`Added ${title.name} to the database`))
// .then(() => mainMenu())
})
}
function createEmployee() {
console.log("Create Employee")
inquirer.prompt([
{
name: "name",
message: "What is the first name of the employee?"
},
{
name: "last",
message: "What is the last name of the employee?"
},
{
name: "role_id",
message: "What is the role id of the employee?"
},
{
name: "manager_id",
message: "What is the manager id of the employee?"
}
]).then(input => {
let name = input;
connection.query("INSERT INTO employee (first_name, last_name, role_id, manager_id) VALUES ?", [[name.name, name.last, parseInt(name.role_id), parseInt(name.maneger_id)]], () => {
console.log(`Added ${name.name} to the database`)
mainMenu()
})
})
}
function viewEmployees() {
console.log("viewEmployees Function")
connection.promise().query("SELECT * FROM employee;")
.then(result => {
console.table(result[0]);
mainMenu();
})
}
function viewRoles() {
console.log("view all roles")
connection.promise().query("SELECT * FROM role")
.then(result => {
console.table(result[0]);
mainMenu();
})
}
function quit() {
console.log("exiting...done.")
process.exit();
}
function mainMenu() {
console.log("Main Menu")
inquirer.prompt(
{
type: "list",
name: "choice",
message: "Please make a selection:",
choices: [
{
name: "View All Department",
value: "VIEW_DEPARTMENT"
},
{
name: "Create new Department",
value: "CREATE_DEPARTMENT"
},
{
name: "View All Employees",
value: "VIEW_EMPLOYEES"
},
{
name: "Create new Employee",
value: "CREATE_EMPLOYEE"
},
{
name: "View All Roles",
value: "VIEW_ROLES"
},
{
name: "Create new Roll",
value: "CREATE_ROLE"
},
{
name: "Exit",
value: "QUIT"
}
]
}
).then((answers) => {
console.log(answers)
switch (answers.choice) {
case "VIEW_DEPARTMENT":
viewDepartment();
break;
case "CREATE_DEPARTMENT":
createDepartment();
break;
case "VIEW_ROLES":
viewRoles();
break;
case "CREATE_ROLE":
createRole();
break;
case "VIEW_EMPLOYEES":
viewEmployees();
break;
case "CREATE_EMPLOYEE":
createEmployee();
break;
default:
quit();
}
})
}
function init() {
mainMenu();
}
init();