-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
307 lines (260 loc) · 9.28 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
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
/***********************************************************************
* BTI325 – Assignment 3
* 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: Sina Lahsaee Student ID: 129948162 Date: DEC 28th, 2017
*
* Online (Heroku) Link: https://fast-fjord-15339.herokuapp.com/
*
***********************************************************************/
var HTTP_PORT = process.env.PORT || 8080;
var path = require("path");
var path1 = require("./data-service.js");
var express = require("express");
const clientSessions = require('client-sessions');
var dataServiceAuth = require("./data-service-auth.js");
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
var dataServiceComments = require('./data-service-comments.js');
var app = express();
app.use(express.static('public'));
//assignment 4
app.use(bodyParser.urlencoded({ extended: true }));
app.engine(".hbs", exphbs({
extname: ".hbs",
defaultLayout: 'layout',
helpers: {
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);
}
}
}
}));
app.set("view engine", ".hbs");
// setup a 'route' to listen on the default url path
app.get("/", function(req, res) {
res.render("home");
});
//A7
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();
});
function ensureLogin(req,res,next)
{
if (!req.session.user)
{
res.redirect("/login");
}
else
{
next();
}
};
//A6 updated
app.get("/about", function(req,res){
dataServiceComments.getAllComments().then(function(data){
res.render("about", {data: data});
}).catch(()=>{res.render("about");})
});
app.get('/employees', ensureLogin, function(req, res) {
if (req.query !== {} || req.query.manager !== {})
{
if (req.query.status)
path1.getEmployeesByStatus(req.query.status)
.then( function(data) {
res.render("employeeList", { data: data, title: "Employees" });;
})
.catch( function(err) {res.render("employeeList", { data: {}, title: "Employees" });});
else if (req.query.department)
path1.getEmployeesByDepartment(req.query.department)
.then( function(data) {
res.render("employeeList", { data: data, title: "Employees" });
})
.catch( function(err) {res.render("employeeList", { data: {}, title: "Employees" });});
else if (req.query.manager)
path1.getEmployeesByManager(req.query.manager)
.then( function(data) {
res.render("employeeList", { data: data, title: "Employees" });
})
.catch( function(err) {res.render("employeeList", { data: {}, title: "Employees" });});
else if (req.query)
path1.getAllEmployees().then( function(data) {
res.render("employeeList", { data: data, title: "Employees" });
})
.catch( function(err) {res.render("employeeList", { data: {}, title: "Employees" });});
else
{
res.render("employeeList", { data: {}, title: "Employees" });
}
}
});
app.get("/employee/:empNum", ensureLogin,(req, res) => {
// initialize an empty object to store the values
let viewData = {};
path1.getEmployeesByNum(req.params.empNum)
.then((data) => {
viewData.data = data; //store employee data in the "viewData" object as "data"
}).catch(()=>{
viewData.data = null; // set employee to null if there was an error
}).then(path1.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.data.department) {
viewData.departments[i].selected = true;
}
}
}).catch(()=>{
viewData.departments=[]; // set departments to empty if there was an error
}).then(()=>{
if(viewData.data == 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('/managers', ensureLogin, (req,res)=>{
path1.getManagers()
.then( function(data) {
res.render("employeeList", { data: data, title: "Employees (Managers)" });
})
.catch( function(err){res.render("employeeList", { data: {}, title: "Employees (Managers)" });});
});
app.get('/departments', ensureLogin, (req,res)=>{
path1.getDepartments()
.then( function(data) {
res.render("departmentList", { data: data, title: "Departments" });
})
.catch( function(err) {res.render("departmentList", { data: {}, title: "Departments" });});
});
//Updated in A5
app.get("/employees/add", ensureLogin, (req,res) => {
path1.getDepartments().then((data)=>{
res.render("addEmployee", {departments: data});
}).catch(()=>{
res.render("addEmployee", {departments: []});
})
});
app.post("/employees/add", ensureLogin, (req, res) => {
path1.addEmployee(req.body).then(function(data){
res.redirect("/employees");})
});
app.post("/employee/update", ensureLogin, (req, res) => {
path1.updateEmployee(req.body).then(function(data){
res.redirect("/employees");});
});
//Routes for A5
app.get("/departments/add", ensureLogin, (req,res) => {
res.render("addDepartment");
});
app.post("/departments/add", ensureLogin, (req, res) => {
path1.addDepartment(req.body).then(function(data){
res.redirect("/departments");})
});
app.post("/departments/update", ensureLogin, (req, res) => {
path1.updateDepartment(req.body).then(function(data){
res.redirect("/departments");});
});
app.get('/department/:depNum', ensureLogin, (req,res)=>{
path1.getDepartmentById(req.params.depNum)
.then( function(data){
res.render("department", { data: data });})
.catch(function(err) {res.status(404).send("Department Not Found");});
});
app.get('/employee/delete/:empNum', ensureLogin, (req,res)=>{
path1.deleteEmployeeByNum(req.params.empNum)
.then(function(data){
res.redirect("/employees");
}).catch(function(err){res.status(500).send("Unable to Remove Employee / Employee Not Found")});
});
///end of A5 Routes
///A6 POSTS
app.post("/about/addComment", (req, res) => {
dataServiceComments.addComment(req.body).then(function(){
res.redirect("/about");}).catch(function(){
console.log("addComment(data) failed.")
res.redirect("/about");});
});
app.post("/about/addReply", (req, res) => {
dataServiceComments.addReply(req.body).then(function(){
res.redirect("/about");}).catch(function(){
console.log("addReply(data) failed.")
res.redirect("/about");});
});
//A7
app.get("/login", function(req,res)
{
res.render("login");
});
app.get("/register", function(req,res)
{
res.render("register");
});
app.post("/register",(req,res) =>
{
dataServiceAuth.registerUser(req.body).then(() =>
{
res.render("register",{successMessage:"User created"});
}).catch((err) =>
{
res.render("register",{errorMessage:err, user:req.body.user});
});
});
app.post("/login",(req,res) =>
{
dataServiceAuth.checkUser(req.body).then(() =>
{
req.session.user = {
username:req.body.user
};
console.log("redirecting to /employees");
res.redirect("/employees");
}).catch((err) =>
{
res.render("login", {errorMessage:err, user:req.body.user});
});
});
app.get("/logout",(req,res) =>
{
req.session.reset();
res.redirect("/");
});
function onHttpStart(){
console.log("Express http server listening on: " + HTTP_PORT);
}
//end of A6 POST
app.get('*', (req,res)=>{
res.status(404);
res.send("Page Not Found");
});
// setup http server to listen on HTTP_PORT
path1.initialize().then( function() {
dataServiceComments.initialize();})
.then(function(){
dataServiceAuth.initialize();
})
.then(function(){
app.listen(HTTP_PORT, onHttpStart)})
.catch( function (){
console.log("unable to start dataService");
});