-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.js
196 lines (193 loc) · 7.06 KB
/
class.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
const db = require('./connection')
const cTable = require('console.table')
class MyEmployees{
constructor(db){
this.db = db
}
noTableDepartments(){
const sql = `SELECT * FROM department`
return db.promise().execute(sql)
}
noTableRoles(){
const sql = `SELECT role.*, department.name AS department
FROM role LEFT JOIN department ON role.department_id= department.department_id`
return db.promise().execute(sql)
}
noTableEmployees(){
const sql = `SELECT employee.employee_id, employee.first_name, employee.last_name, employee.manager_id,
role.title AS title, role.salary AS salary, department.name AS department FROM employee
LEFT JOIN role ON employee.role_id = role.role_id
LEFT JOIN department ON role.department_id = department.department_id`
return db.promise().execute(sql)
}
departments(){
const sql = `SELECT * FROM department`
return db.promise().query(sql).then(([rows])=> {
console.log("\n")
console.table(rows)
console.log("\n")
}).catch(error=> {
throw error
})
}
roles(){
const sql = `SELECT role.*, department.name AS department
FROM role LEFT JOIN department ON role.department_id= department.department_id`
return db.promise().query(sql).then(([rows])=> {
console.log("\n")
console.table(rows)
console.log("\n")
}).catch(error=> {
throw error
})
}
employees(){
const sql = `SELECT employee.employee_id, employee.first_name, employee.last_name, employee.manager_id,
role.title AS title, role.salary AS salary, department.name AS department FROM employee
LEFT JOIN role ON employee.role_id = role.role_id
LEFT JOIN department ON role.department_id = department.department_id`
return db.promise().query(sql).then(([rows])=> {
console.log("\n")
console.table(rows)
console.log("\n")
}).catch(error=> {
throw error
})
}
managers(){
const sql = `SELECT employee.first_name, employee.last_name, department.name as department
from employee
LEFT JOIN role on employee.role_id=role.role_id
left join department on role.department_id=department.department_id
where manager_id IS NOT NULL`
return db.promise().query(sql).then(([rows])=> {
console.log("\n")
console.table(rows)
console.log("\n")
}).catch(error=> {
throw error
})
}
budgets(){
const sql = `SELECT count(*) as staff_count, sum(role.salary) as budget, department.name as department
from employee
left join role on employee.role_id=role.role_id
left join department on role.department_id=department.department_id
group by department`
return db.promise().query(sql).then(([rows])=> {
console.log("\n")
console.table(rows)
console.log("\n")
}).catch(error=> {
throw error
})
}
viewDept(data){
const sql = `SELECT employee.employee_id, employee.first_name, employee.last_name, department.name as department
from employee
LEFT JOIN role on employee.role_id=role.role_id
left join department on role.department_id=department.department_id
where department.department_id = ?`
const params = [data.dept]
return db.promise().query(sql, params).then(([rows])=> {
console.log("\n")
console.table(rows)
console.log("\n")
}).catch(error=> {
throw error
})
}
addDepartment(data){
const sql = `INSERT INTO department (name) VALUES(?)`
const params = data.name
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log(`Added department named ${params} into database.`)
console.log("\n")
}).catch(error=> {
throw error
})
}
addRoles(data){
const sql = `INSERT INTO role (title, salary, department_id) VALUES(?,?,?)`
const params = [data.title, data.salary, data.dept]
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log(`Added role named ${params[0]} into database.`)
console.log("\n")
}).catch(error=> {
throw error
})
}
addEmployees(data){
const sql = `INSERT INTO employee(first_name, last_name, role_id, manager_id)
VALUES(?,?,?,?)`
const params = [data.first_name, data.last_name, data.role, data.manager]
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log(`Added employee named ${params[0]} ${params[1]} into database.`)
console.log("\n")
}).catch(error=> {
throw error
})
}
delEmp(data){
const sql = `DELETE FROM employee WHERE employee_id=?`
const params = [data.delete]
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log(`Deleted employee with id ${params[0]} from database.`)
console.log("\n")
}).catch(error=> {
throw error
})
}
delRole(data){
const sql = `DELETE FROM role WHERE role.role_id=?`
const params = [data.delete]
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log(`Deleted role with id ${params[0]} from database.`)
console.log("\n")
}).catch(error=> {
throw error
})
}
delDept(data){
const sql = `DELETE FROM department WHERE department.department_id=?`
const params = [data.delete]
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log(`Deleted department with id ${params[0]} from database.`)
console.log("\n")
}).catch(error=> {
throw error
})
}
updateEmployees(data){
const sql = `UPDATE employee SET role_id=? WHERE employee_id =?`
const params = [data.role, data.employee]
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log(`Updated employee's role in database. View employees to confirm.`)
console.log("\n")
}).catch(error=> {
throw error
})
}
newManager(data){
if(data.manager_id ===0){
data.manager_id=null
}
const sql = `UPDATE employee SET manager_id=? WHERE employee_id =?`
const params = [data.manager_id, data.newManager]
return db.promise().query(sql, params).then(()=> {
console.log("\n")
console.log("Updated employee's role in database. View Managers to confirm.")
console.log("\n")
}).catch(error=> {
throw error
})
}
}
module.exports = new MyEmployees(db)