-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
197 lines (146 loc) · 4.76 KB
/
app.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
/// imports
const express = require("express");
// const sessions = require('express-session');
const fs = require("fs");
/// app and ports
const app = express();
const APP_PORT = process.env.APP_PORT || 3000;
/// their middleware
app.use(express.static("static"));
app.set("view engine", "ejs");
// app.use(sessions({
// secret: process.env.SESSIONS_SECRET,
// saveUninitialized: false,
// resave: false
// })
// );
app.use(express.urlencoded({ extended: true }));
/// my middleware
app.use((req, res, next) => {
res.locals.query = req.query;
// res.locals.user_id = req.session.user_id;
// res.locals.member = req.session.sess_valid;
next();
}); /// exposes query and session to ejs templates
// roles data
let roles = [
{ role_id: 1, role: "Tech Lead" },
{ role_id: 2, role: "Senior Security Engineer" },
{ role_id: 3, role: "Software Engineer" },
{ role_id: 4, role: "UX Lead" },
{ role_id: 5, role: "Data Architect" },
{ role_id: 6, role: "UI Lead" },
{ role_id: 7, role: "Workday Consultant" },
];
/// initial data
let employeeData = [
{ first_name: "Seamus", last_name: "McBride", salary: 30000, role: "Project Manager", address: "here", employee_number: 42069 },
{ first_name: "Sam", last_name: "Millar", salary: 40000, role: "Tech Lead", address: "here", employee_number: 42070 },
{ first_name: "Paula", last_name: "Santkiewicz", salary: 50000, role: "UX Lead", address: "here", employee_number: 42071 },
{ first_name: "Bethani", last_name: "Gourlay", salary: 60000, role: "UI Lead", address: "here", employee_number: 42072 },
{ first_name: "Dylan", last_name: "Robinson", salary: 70000, role: "Database Manager", address: "here", employee_number: 42073 },
];
/// routes
app.get("/", (req, res) => {
res.render("home", {
title: "Employees",
employeeData,
});
});
app.get("/employee", (req, res) => {
try {
let n = req.query.n;
let employee = {};
for (i = 0; i < employeeData.length; i++) {
if (employeeData[i].employee_number == n) employee = employeeData[i];
}
res.render("employee", {
title: `Employee ${n}`,
employee,
});
} catch {
res.redirect("/");
}
});
app.get("/add", (req, res) => {
res.render("add", {
title: "Add employee",
roles,
});
});
app.post("/add", (req, res) => {
let employee = {
first_name: req.body.firstName,
last_name: req.body.lastName,
salary: req.body.employeeSalary,
role: req.body.jobTitle,
address: req.body.address,
employee_number: req.body.employeeID,
};
console.log(employee.role);
employeeData.push(employee);
res.redirect("/");
});
app.get("/login", (req, res) => {
let title = "Login";
res.render("login", { title });
});
app.get('/edit', (req, res) => {
let employeeNum = req.query.n;
let employee = employeeData.filter((employee) => {
return employee.employee_number.toString() === employeeNum;
});
res.render('edit', {
title: 'Edit employees', employee, roles, employeeNum
});
});
app.post('/edit', (req, res) => {
let employeeNum = req.body.employeeNumber;
console.log("in post route");
let employee = employeeData.filter((employee) => {
return employee.employee_number.toString() === employeeNum;
});
let index = employeeData.findIndex((emp) => {
return emp.employee_number.toString() === employeeNum
});
if (index !== -1) {
employeeData[index].salary = req.body.employeeSalary;
employeeData[index].first_name = req.body.firstName;
employeeData[index].last_name = req.body.lastName;
employeeData[index].address = req.body.address;
employeeData[index].role = req.body.jobTitle;
}
// res.render('edit', {
// title: 'Edit employees', roles, employee, employeeNum
// });
res.redirect('/');
});
app.post("/delete", (req, res) => {
let employeedelete = req.body.n;
employeedelete = parseInt(employeedelete);
const index = function findEmployeeNumber(employeeData) {
for(var indexnum = 0; indexnum < employeeData.length; ++indexnum) {
var object = arr[indexnum];
if(object.employee_number == employeedelete) {
return indexnum;
}
}
return -1;
}
employeeData.splice(index, 1);
res.redirect("/");
});
// /// route handler
// const routeFiles = fs.readdirSync('./routes')
// .filter(file => ( file.endsWith('.js') && !file.startsWith('_') ));
// for (const file of routeFiles) {
// const route = require(`./routes/${file}`);
// routePath = file.slice(0,-3);
// if (file=='home.js') routePath="";
// app.use(`/${routePath}`, route);
// }
// // console.log("Routes:", routeFiles);
/// server
const server = app.listen(APP_PORT, () => {
console.log(`App started at http://localhost:${server.address().port}/`);
});