-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
354 lines (298 loc) · 11.3 KB
/
Copy pathserver.js
File metadata and controls
354 lines (298 loc) · 11.3 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*********************************************************************************
* WEB322 – Assignment 06
* I declare that this assignment is my own work in accordance with Seneca Academic Policy. No part
* of this assignment has been copied manually or electronically from any other source
* (including 3rd party web sites) or distributed to other students.
* Name: KEVIN SAN PEDRO Student ID:023503139 Date:2021-11-18
* Online (Heroku) Link: https://obscure-plateau-20353.herokuapp.com/home
* Mongoose setup Link: mongodb+srv://kevinUser:6y9S2qE2rvWL0kb5@assignment06.w3b58.mongodb.net/Assignment6?retryWrites=true&w=majority
********************************************************************************/
const data = require('./data-service.js');
const dataServiceAuth = require("./data-service-auth.js")
const express = require("express");
const multer = require("multer");
const clientSessions = require("client-sessions");
const fs = require('fs');
const app = express();
const path = require("path");
const bodyParser = require("body-parser");
const HTTP_PORT = process.env.PORT || 8080;
//handling handle-bars
/* engine is for creating a helper if each and else are build in helper for handlebars */
const exphbs = require('express-handlebars');
app.engine('.hbs', exphbs({extname: '.hbs'}));
app.set('view engine' , '.hbs');
app.engine('.hbs', exphbs({ extname: '.hbs', helpers: {
navLink: function(url, options){
return '<li' +
((url == app.locals.activeRoute) ? ' class="active" ' : '') +
'><a href="' + url + '">' + options.fn(this) + '</a></li>';
},
equal: function (lvalue, rvalue, options) {
if (arguments.length < 3)
throw new Error("Handlebars Helper equal needs 2 parameters");
if (lvalue != rvalue) {
return options.inverse(this);
} else {
return options.fn(this);
}
},
defaultLayout: 'main'
}
})
);
/************* Setup client-sessions ***************/
app.use(clientSessions({
cookieName: "session", // this is the object name that will be added to 'req'
secret: "week10example_web322", // this should be a long un-guessable string.
duration: 2 * 60 * 1000, // duration of the session in milliseconds (2 minutes)
activeDuration: 1000 * 60 // the session will be extended by this many ms each request (1 minute)
}));
app.use(function(req, res, next) {
res.locals.session = req.session;
next();
});
/*************Support Image uploads***************/
const storage = multer.diskStorage({
destination: "./public/images/uploaded",
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
/*************Middle ware navigation bar **************/
app.use(function(req,res,next){
let route = req.baseUrl + req.path;
app.locals.activeRoute = (route == "/") ? "/" : route.replace(/\/$/, "");
next();
});
/** use static folder to load image from the server after sending it**/
app.use(express.static('public'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.urlencoded({ extended: true }));
/*
The benefit here is that the navlink helper will
automatically render the correct <li> element add the class "active" if app.locals.activeRoute matches the
provided url, ie "/about"
*/
/*************Html get Routes **************/
app.get('/', (req, res) => {
res.render('home');
});
app.get('/home', (req, res) => {
res.render('home');
});
app.get('/about', (req, res) => {
res.render('about');
});
app.get("/employees", ensureLogin, (req, res) => {
if(req.query.status){
data.getEmployeesByStatus(req.query.status)
.then((data) => {
res.render("employees", {user: req.session.user, employees: data});
})
}
else if(req.query.department){
data.getEmployeesByDepartment(req.query.department)
.then((data) => {
res.render("employees", {user: req.session.user, employees: data});
})
}
else if(req.query.manager){
data.getEmployeesByManager(req.query.manager)
.then((data) => {
res.render("employees", {user: req.session.user, employees: data});
})
}
else
data.getAllEmployees()
.then((data) => {
if( data.length > 0 ){
res.render("employees", {user: req.session.user, employees: data});
}else{
res.render("employees",{user: req.session.user, message: "no results" });
}
}).catch((err) => {
res.render("employees", {user: req.session.user,message:"no results"})
})
});
app.get("/departments", ensureLogin,(req, res) => {
data.getDepartments().then((data) => {
if(data.length > 0 ){
res.render("departments",{departments: data} )
}else
res.render("departments", {message: "no results"});
}).catch((err) => {
res.render("departments", {message: err})
})
});
app.get("/images/add", ensureLogin, (req, res) => {
res.render(path.join(__dirname + "/views/addImage"));
});
app.get("/images", ensureLogin, (req, res) => {
fs.readdir(path.join(__dirname,"/public/images/uploaded"),
(err, items) =>{
res.render('images', {images: items});
});
});
app.get("/employee/:empNum", ensureLogin, (req, res) => {
// initialize an empty object to store the values
let viewData = {};
data.getEmployeeByNum(req.params.empNum).then((data) => {
if (data) {
viewData.employee = data; //store employee data in the "viewData" object as "employee"
} else {
viewData.employee = null; // set employee to null if none were returned
}
}).catch(() => {
viewData.employee = null; // set employee to null if there was an error
}).then(dataService.getDepartments)
.then((data) => {
viewData.departments = data; // store department data in the "viewData" object as "departments"
// loop through viewData.departments and once we have found the departmentId that matches
// the employee's "department" value, add a "selected" property to the matching
// viewData.departments object
for (let i = 0; i < viewData.departments.length; i++) {
if (viewData.departments[i].departmentId == viewData.employee.department) {
viewData.departments[i].selected = true;
}
}
}).catch(() => {
viewData.departments = []; // set departments to empty if there was an error
}).then(() => {
if (viewData.employee == null) { // if no employee - return an error
res.status(404).send("Employee Not Found");
} else {
res.render("employee", { viewData: viewData }); // render the "employee" view
}
});
});
app.get('/employees/add', ensureLogin, (req, res) => {
data.getDepartments().then((data) => {
res.render('addEmployee', {departments: data });
})
.catch((err) => {
res.render("addEmployee", {departments: []});
})
});
app.get('/departments/add', ensureLogin, (req, res) => {
res.render('addDepartment');
});
app.get('/department/:departmentId',ensureLogin, (req, res) => {
data.getDepartmentById(req.params.departmentId)
.then((data) => {
res.render('department', {department: data });})
.catch((err) =>{
res.status(404).send("Department Not Found");
}).catch((err) =>{
res.status(404).send("Department Not Found");
});
});
app.get('/departments/delete/:departmentId', ensureLogin, (req, res) => {
data.deleteDepartmentById(req.params.departmentId)
.then(res.redirect('/departments'))
.catch((err) =>{
res.status(500).send( "Unable to Remove Department / Department not found");
})
});
app.get('/employees/delete/:empNum',ensureLogin, (req, res) => {
data.deleteEmployeeByNum(req.params.empNum)
.then(() => {
res.redirect('/employees')
})
.catch((err) =>{
res.status(500).send( "Unable to Remove Employee / Employee not found");
})
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/register", (req, res) => {
res.render("register");
});
app.get("/logout", (req, res) => {
req.session.reset();
res.redirect("/");
});
app.get("/userHistory", ensureLogin, (req, res) => {
res.render("userHistory");
});
/****************** end of get route *****************/
/****************** middleware to ensure user login before accessing the website *****************/
function ensureLogin(req, res, next) {
if (!req.session.user) {
res.redirect("/login");
} else {
next();
}
}
/****************** start post route *****************/
app.post("/images/add", ensureLogin, upload.single("imageFile"), (req, res) => { //A3 part 2 step 2
res.redirect("/images")
});
app.post('/employees/add', ensureLogin, (req, res)=>{
data.addEmployee(req.body).then(res.redirect('/employees')).catch((err) =>{
res.status(500).send( "Unable to add Employee / Employee not found");
});
})
app.post("/employee/update", ensureLogin, (req, res) => {
data.updateEmployee(req.body)
.then(res.redirect('/employees'))
.catch((err) =>{
res.status(500).send( "Unable to update Employee / Employee not found");
});
});
app.post('/departments/add', ensureLogin, (req, res) =>{
data.addDepartment(req.body)
.then( () => {
res.redirect('/departments')
})
.catch((err) => {
res.status(500).send("Unable to add department");
});
})
app.post("/department/update", ensureLogin, (req, res) => {
data.updateDepartment(req.body)
.then( () => {
res.redirect('/departments')
})
.catch((err) => {
res.status(500).send("Unable to Update department");
});;
});
app.post('/register', (req, res)=>{
dataServiceAuth.registerUser(req.body)
.then(()=>{
res.render('register', {successMessage: "User created"} );
})
.catch((err)=>{
res.render('register', {errorMessage: err, userName: req.body.userName});
});
})
app.post('/login', (req, res)=>{
req.body.userAgent = req.get('User-Agent');
dataServiceAuth.checkUser(req.body).then((user) => {
req.session.user = {
userName: user.userName, // authenticated user's userName
email: user.email,// authenticated user's email
loginHistory: user.loginHistory // authenticated user's loginHistory
}
res.redirect('/employees');
}).catch((err)=>{
res.render('login', {errorMessage: err, userName: req.body.userName});
});
})
/****************** End post route *****************/
/****************** Error page incase a invalid route is input by the user *****************/
app.use((req, res) => {
res.status(404).send("Error 404! page Not Found");
});
//to review check week 2 notes and week 3
data.initialize().then(dataServiceAuth.initialize())
.then(function(){
app.listen(HTTP_PORT, function(){
console.log("app listening on: " + HTTP_PORT)
});
}).catch(function(err){
console.log("unable to start server: " + err);
});