-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
244 lines (215 loc) · 6.5 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
const express = require('express');
const mysql = require('mysql2');
const cTable = require('console.table');
const inquirer = require('inquirer');
const PORT = process.env.PORT || 3001;
const app = express();
// middleware
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
// connect to database
const db = mysql.createConnection(
{
host: 'localhost',
user: 'root',
password: 'password',
database: 'workplace'
},
console.log('you are connected to the workplace database')
);
// init app with a menu selection
db.connect((err) => {
if (err) throw err;
console.log('you are connected to the workplace database')
menu();
});
function menu() {
inquirer
.prompt([
{
type: 'list',
choices: ['View All DEPARTMENTS',
'View All ROLES',
'View All EMPLOYEES',
'ADD a DEPARTMENT ',
'ADD a ROLE',
'ADD an EMPLOYEE',
'UPDATE an EMPLOYEE ROLE',
'Finish'
],
name: 'menu',
message: "choose one of the following options",
}
])
.then(function (data) {
switch (data.menu) {
case 'View All DEPARTMENTS':
viewDepartments();
break;
case 'View All ROLES':
viewRoles();
break;
case 'View All EMPLOYEES':
viewEmployees();
break;
case 'ADD a DEPARTMENT ':
addDepartment();
break;
case 'ADD a ROLE':
addRole();
break;
case 'ADD an EMPLOYEE':
addEmployee();
break;
case 'UPDATE an EMPLOYEE ROLE':
updateEmployee();
break;
case 'Finish':
finish();
break;
}
});
}
// functions
const viewDepartments = () => {
console.log('Viewing ALL DEPARTMENTS\n');
let sql = `SELECT * FROM department`;
db.query(sql, (err, res) => {
if (err) throw err;
console.table(res);
menu();
})
};
const viewRoles = () => {
console.log('Viewing ALL ROLES\n')
const sql = ` SELECT r.id, r.title, r.salary, d.name AS department
FROM role r
LEFT JOIN department d
ON r.department_id = d.id;`;
db.query(sql, (err, res) => {
if (err) throw err;
console.table(res);
menu();
})
};
const viewEmployees = () => {
console.log('You are viewing ALL EMPLOYEES');
const sql = `SELECT e.id, e.first_name, e.last_name, e.role_id,CONCAT (m.first_name, ' ', m.last_name) AS manager, r.title AS job_title, r.salary AS salary
FROM employee e
LEFT JOIN role r
ON e.role_id = r.id
LEFT JOIN employee m
ON e.manager_id = m.id
;
`
db.query(sql, (err, res) => {
if (err) throw err;
console.table(res);
menu();
})
};
const addDepartment = () => {
inquirer.prompt([
{
type: 'input',
name: 'dept',
message: 'Enter the name of the department you wish to add'
}
])
.then(function (data) {
db.query(`INSERT INTO department(name) VALUES (?) `, [data.dept], function (err, res) {
if (err) throw err;
console.log([data.dept] + ` id is added to database`);
menu();
})
})
};
const addEmployee = () => {
inquirer.prompt([
{
type: 'input',
name: 'empfirstname',
message: 'What is the employee first name?'
},
{
type: 'input',
name: 'emplastname',
message: 'What is employee last name?'
},
{
type: 'input',
name: 'emprole',
message: `What is this employee's role id ?`
}
,
{
type: 'input',
name: 'empmanager',
message: `What is the manager id of this employee?`
}
]).then(function (data) {
db.query(`INSERT INTO employee (first_name, last_name, role_id, manager_id) VALUES (?, ?, ?, ?)`, [data.empfirstname, data.emplastname, data.emprole, data.empmanager], (err, res) => {
if (err) throw err;
console.log([data.empfirstname, data.emplastname] + ' has been added to the database as an employee');
menu();
})
})
};
const addRole = () => {
inquirer.prompt([
{
type:'input',
name:'rolename',
message:'What is the name of this role?'
},
{
type:'input',
name:'rolesalary',
message:'What is the salary for this role?'
},
{
type:'input',
name:'roledept',
message:'What is the department number for this role?'
}
]).then (function(data) {
db.query(`INSERT INTO role(title, salary, department_id) VALUES(?, ?, ?)`, [data.rolename, data.rolesalary, data.roledept],(err,res) => {
if(err)throw err;
console.log([data.rolename] + ` has been added to database as a role`);
menu();
})
})
};
const updateEmployee = () => {
const sql = `SELECT * FROM employee;`
db.query(sql,(err,res)=> {
if(err) throw err;
console.table('_________________',res);
})
inquirer.prompt([
{
type:'input',
name:'eid',
message:"Enter the employee ID of the employee youd want to update from the list below "
},
{
type:'input',
name:'newrole',
message:'Enter the role id number for the employee from the following: 1= Teller / 2= Banker/ 3= Lead Teller / 4 = Manager (Must be a # id) ',
}
]).then (function(data) {
db.query(`UPDATE employee SET role_id = (?) WHERE employee.id = (?)`, [data.newrole, data.eid], (err,res) => {
if(err) throw err;
console.log('Employee role has been updated! ');
menu();
})
})
}
const finish = () => {
console.log('Thank you for using BEEHIVE database services!');
db.end();
process.exit();
};
app.listen(PORT, () => {
console.log(`you are now on port ${PORT}`);
});