This repository has been archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
719 lines (627 loc) · 22.5 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
const express = require('express');
const fileUpload = require('express-fileupload');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const path = require('path');
const cors = require('cors');
const dotenv = require('dotenv');
const hbs = require('hbs');
const cookieSession = require('cookie-session');
const bcrypt = require('bcryptjs');
const { body, validationResult } = require('express-validator');
const dbConnection = require('./database');
dotenv.config();
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
//const dbService = require('./dbService');
//const {getHomePage} = require('./routes/index');
//const {addPlayerPage, addPlayer, deletePlayer, editPlayer, editPlayerPage} = require('./routes/player');
const port = 3000;
app.use(cors());
app.use(express.json());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
io.sockets.on('connection', function(socket) {
socket.on('username', function(username) {
socket.username = username;
io.emit('is_online', '🔵 <i>' + socket.username + ' join the chat..</i>');
});
socket.on('disconnect', function(username) {
io.emit('is_online', '🔴 <i>' + socket.username + ' left the chat..</i>');
})
socket.on('chat_message', function(message) {
io.emit('chat_message', '<strong>' + socket.username + '</strong>: ' + message);
});
});
// create connection to database
// the mysql.createConnection function takes in a configuration object which contains host, user, password and the database name.
const db = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'Password123@',
database: 'socka'
});
// APPLY COOKIE SESSION MIDDLEWARE
app.use(cookieSession({
name: 'session',
keys: ['key1', 'key2'],
maxAge: 5 * 1000 //5 seconds
}));
// DECLARING CUSTOM MIDDLEWARE
const ifNotLoggedin = (req, res, next) => {
if(!req.session.isLoggedIn){
return res.render('login');
}
next();
}
const ifLoggedin = (req,res,next) => {
if(req.session.isLoggedIn){
return res.redirect('/');
}
next();
}
// END OF CUSTOM MIDDLEWARE
//connect to database
db.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to database');
});
global.db = db;
// configure middleware
app.set('port', process.env.port || port); // set express to use this port
// app.set('views', __dirname + '/views'); // set express to look in this folder to render our view
app.set('view engine', 'ejs'); // configure template engine
// app.set('view engine', 'handlebars');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); // parse form data client
app.use(express.static(path.join(__dirname, 'public'))); // configure express to use public folder
app.use(fileUpload()); // configure fileupload
// routes for the app
app.set("views", "./views");
app.get("/", (req, res) => {
res.render("index");
});
app.get("/app-student-billing", (req, res) => {
res.render("app-student-billing");
});
app.get('/add-student-warden', (req, res) => {
let sql = "SELECT * FROM Student";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.render('student_view.hbs', {
results: results
});
});
});
app.get('/add-student-localguardian', (req, res) => {
let sql = "SELECT * FROM LocalGuardian";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.render('studentGuardian_view.hbs', {
results: results
});
});
});
//route for insert data
app.post('/saveLocalGuardian', (req, res) => {
let data = { guardian_name: req.body.guardian_name, reg_no: req.body.reg_no, gender: req.body.gender, relation: req.body.relation, email_id: req.body.email_id, address: req.body.address};
let sql = "INSERT INTO LocalGuardian SET ?";
let query = db.query(sql, data, (err, results) => {
if (err) throw err;
res.redirect('/add-student-localguardian');
});
});
//route for update data
app.post('/updateLocalGuardian', (req, res) => {
let sql = "UPDATE LocalGuardian SET guardian_name='" + req.body.guardian_name + "',reg_no='" + req.body.reg_no + "',gender='" + req.body.gender + "', relation='" + req.body.relation + "',email_id='" + req.body.email_id + "',address='" + req.body.address + "' WHERE reg_no='" + req.body.reg_no + "'";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/add-student-localguardian');
});
});
//route for delete data
app.post('/deleteLocalGuardian', (req, res) => {
let sql = "DELETE FROM LocalGuardian WHERE reg_no='" + req.body.reg_no + "'";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/add-student-localguardian');
});
});
//route for insert data
app.post('/save', (req, res) => {
let data = { reg_no: req.body.reg_no, room_no: req.body.room_no, block_id: req.body.block_id, stud_name: req.body.stud_name, gender: req.body.gender, dob: req.body.dob, blood_group: req.body.blood_group, email_id: req.body.email_id, address: req.body.address, father_name: req.body.father_name, mother_name: req.body.mother_name, parent_email: req.body.parent_email, course_id: req.body.course_id };
let sql = "INSERT INTO Student SET ?";
let query = db.query(sql, data, (err, results) => {
if (err) throw err;
res.redirect('/add-student-warden');
});
});
//route for update data
app.post('/update', (req, res) => {
let sql = "UPDATE Student SET reg_no='" + req.body.reg_no + "',room_no='" + req.body.room_no + "', block_id='" + req.body.block_id + "',stud_name='" + req.body.stud_name + "',gender='" + req.body.gender + "',dob='" + req.body.dob + "',blood_group='" + req.body.blood_group + "', email_id='" + req.body.email_id + "', address='" + req.body.address + "', father_name='" + req.body.father_name + "', mother_name='" + req.body.mother_name + "', parent_email='" + req.body.parent_email + "', course_id='" + req.body.course_id + "' WHERE reg_no='" + req.body.reg_no + "'";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/add-student-warden');
});
});
//route for delete data
app.post('/delete', (req, res) => {
let sql = "DELETE FROM Student WHERE reg_no='" + req.body.reg_no + "'";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/add-student-warden');
});
});
// // create
// app.post('/insert', (request, response) => {
// const { name } = request.body;
// const db = dbService.getDbServiceInstance();
// const result = db.insertNewName(name);
// result
// .then(data => response.json({ data: data }))
// .catch(err => console.log(err));
// });
// // read
// app.get('/getAll', (request, response) => {
// const db = dbService.getDbServiceInstance();
// const result = db.getAllData();
// result
// .then(data => response.json({ data: data }))
// .catch(err => console.log(err));
// })
// // update
// app.patch('/update', (request, response) => {
// const { id, name } = request.body;
// const db = dbService.getDbServiceInstance();
// const result = db.updateNameById(id, name);
// result
// .then(data => response.json({ success: data }))
// .catch(err => console.log(err));
// });
// // delete
// app.delete('/delete/:id', (request, response) => {
// const { id } = request.params;
// const db = dbService.getDbServiceInstance();
// const result = db.deleteRowById(id);
// result
// .then(data => response.json({ success: data }))
// .catch(err => console.log(err));
// });
// app.get('/search/:name', (request, response) => {
// const { name } = request.params;
// const db = dbService.getDbServiceInstance();
// const result = db.searchByName(name);
// result
// .then(data => response.json({ data: data }))
// .catch(err => console.log(err));
// });
app.get('/add-warden-chiefwarden', (req, res) => {
let sql = "SELECT * FROM Staff";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.render('warden_view.hbs', {
results: results
});
});
});
//route for insert data
app.post('/saveStaff', (req, res) => {
let data = { staff_id: req.body.staff_id, staff_name: req.body.staff_name, gender: req.body.gender, dob: req.body.dob, email_id: req.body.email_id, staff_role: req.body.staff_role, salary: req.body.salary };
let sql = "INSERT INTO Staff SET ?";
let query = db.query(sql, data, (err, results) => {
if (err) throw err;
res.redirect('/add-warden-chiefwarden');
});
});
//route for update data
app.post('/updateStaff', (req, res) => {
let sql = "UPDATE Staff SET staff_id='" + req.body.staff_id + "',staff_name='" + req.body.staff_name + "', gender='" + req.body.gender + "',dob='" + req.body.dob + "',email_id='" + req.body.email_id + "',staff_role='" + req.body.staff_role + "',salary='" + req.body.salary + "' WHERE staff_id='" + req.body.staff_id + "'";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/add-warden-chiefwarden');
});
});
//route for delete data
app.post('/deleteStaff', (req, res) => {
let sql = "DELETE FROM Staff WHERE staff_id='" + req.body.staff_id + "'";
console.log(sql)
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/add-warden-chiefwarden');
});
});
// app.get("/add-student", (req, res) => {
// res.render("student_view");
// });
app.get('/outing-request', (req, res) => {
let sql = "SELECT * FROM outing";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.render('outing_view_student.hbs', {
results: results
});
});
});
//route for insert student outing data
app.post('/request', (req, res) => {
let data = { reg_no: req.body.reg_no, outing_type: req.body.outing_type, purpose: req.body.purpose, out_date_time: req.body.out_date_time, expectedin_date_time: req.body.expectedin_date_time, actualin_date_time: req.body.actualin_date_time, staff_id: req.body.staff_id};
let sql = "INSERT INTO outing SET ?";
let query = db.query(sql, data, (err, results) => {
if (err) throw err;
res.redirect('/outing-request');
});
});
//route for update student outing actual intime
app.post('/updateTime', (req, res) => {
let sql = "UPDATE Outing SET actualin_date_time='" + req.body.actualin_date_time + "'";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/outing-request');
});
});
app.get('/outing-grant', (req, res) => {
let sql = "SELECT * FROM outing";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.render('outing_view_warden.hbs', {
results: results
});
});
});
//route for grant gatepass
app.post('/grant', (req, res) => {
let sql = "UPDATE outing SET current_status='" + req.body.current_status + "'";
let query = db.query(sql, (err, results) => {
if (err) throw err;
res.redirect('/outing-grant');
});
});
app.get("/pricing", (req, res) => {
res.render("pricing");
});
app.get("/survey", (req, res) => {
res.render("survey");
});
app.get('/website-forum', ifNotLoggedin, (req,res,next) => {
dbConnection.execute("SELECT `name` FROM `users` WHERE `id`=?",[req.session.userID])
.then(([rows]) => {
res.render('website-forum',{
name:rows[0].name
});
});
});
app.get('/add-student-complaint', ifNotLoggedin, (req,res,next) => {
dbConnection.execute("SELECT `name` FROM `usersStud` WHERE `id`=?",[req.session.userID])
.then(([rows]) => {
res.render('add-student-complaint',{
name:rows[0].name
});
});
});
app.get("/website-forum-thread", (req, res) => {
res.render("website-forum-thread");
});
app.get("/website-contact", (req, res) => {
res.render("website-contact");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/loginStud", (req, res) => {
res.render("loginStud");
});
app.get("/sign-up", (req, res) => {
res.render("sign-up");
});
app.get("/sign-upStud", (req, res) => {
res.render("sign-upStud");
});
// app.get("/website-student-dashboard", (req, res) => {
// res.render("website-student-dashboard");
// });
app.get('/website-student-dashboard', ifNotLoggedin, (req,res,next) => {
dbConnection.execute("SELECT `name` FROM `usersStud` WHERE `id`=?",[req.session.userID])
.then(([rows]) => {
res.render('website-student-dashboard',{
name:rows[0].name
});
});
});
app.get("/website-student-profile", (req, res) => {
res.render("website-student-profile");
});
app.get("/realTimeForum", (req, res) => {
res.render("realTimeForum");
});
// app.get("/website-warden-dashboard", (req, res) => {
// res.render("website-warden-dashboard");
// });
// app.get('/website-chiefwarden-dashboard', ifNotLoggedin, (req,res,next) => {
// dbConnection.execute("SELECT `name` FROM `users` WHERE `id`=?",[req.session.userID])
// .then(([rows]) => {
// res.render('website-warden-dashboard',{
// name:rows[0].name
// });
// });
// });
app.get("/website-chiefwarden-dashboard", (req, res) => {
res.render("website-chiefwarden-dashboard");
});
app.get('/website-warden-dashboard', ifNotLoggedin, (req,res,next) => {
dbConnection.execute("SELECT `name` FROM `users` WHERE `id`=?",[req.session.userID])
.then(([rows]) => {
res.render('website-warden-dashboard',{
name:rows[0].name
});
});
});
// END OF ROOT PAGE
// app.get('/add', addPlayerPage);
// app.get('/edit/:id', editPlayerPage);
// app.get('/delete/:id', deletePlayer);
// app.post('/add', addPlayer);
// app.post('/edit/:id', editPlayer);
// REGISTER PAGE
app.post('/register', ifLoggedin,
// post data validation(using express-validator)
[
body('user_email','Invalid email address!').isEmail().custom((value) => {
return dbConnection.execute('SELECT `email` FROM `users` WHERE `email`=?', [value])
.then(([rows]) => {
if(rows.length > 0){
return Promise.reject('This E-mail already in use!');
}
return true;
});
}),
body('user_name','Username is Empty!').trim().not().isEmpty(),
body('user_pass','The password must be of minimum length 6 characters').trim().isLength({ min: 6 }),
],// end of post data validation
(req,res,next) => {
const validation_result = validationResult(req);
const {user_name, user_pass, user_email} = req.body;
// IF validation_result HAS NO ERROR
if(validation_result.isEmpty()){
// password encryption (using bcryptjs)
bcrypt.hash(user_pass, 12).then((hash_pass) => {
// INSERTING USER INTO DATABASE
dbConnection.execute("INSERT INTO `users`(`name`,`email`,`password`) VALUES(?,?,?)",[user_name,user_email, hash_pass])
.then(result => {
res.send(`your account has been created successfully, Now you can <a href="/login">Login</a>`);
}).catch(err => {
// THROW INSERTING USER ERROR'S
if (err) throw err;
});
})
.catch(err => {
// THROW HASING ERROR'S
if (err) throw err;
})
}
else{
// COLLECT ALL THE VALIDATION ERRORS
let allErrors = validation_result.errors.map((error) => {
return error.msg;
});
// REDERING login-register PAGE WITH VALIDATION ERRORS
res.render('sign-up',{
register_error:allErrors,
old_data:req.body
});
}
});// END OF REGISTER PAGE
//Registration page for student
app.post('/registerStud', ifLoggedin,
// post data validation(using express-validator)
[
body('user_email','Invalid email address!').isEmail().custom((value) => {
return dbConnection.execute('SELECT `email` FROM `usersStud` WHERE `email`=?', [value])
.then(([rows]) => {
if(rows.length > 0){
return Promise.reject('This E-mail already in use!');
}
return true;
});
}),
body('user_name','Username is Empty!').trim().not().isEmpty(),
body('user_pass','The password must be of minimum length 6 characters').trim().isLength({ min: 6 }),
],// end of post data validation
(req,res,next) => {
const validation_result = validationResult(req);
const {user_name, user_pass, user_email} = req.body;
// IF validation_result HAS NO ERROR
if(validation_result.isEmpty()){
// password encryption (using bcryptjs)
bcrypt.hash(user_pass, 12).then((hash_pass) => {
// INSERTING USER INTO DATABASE
dbConnection.execute("INSERT INTO `usersStud`(`name`,`email`,`password`) VALUES(?,?,?)",[user_name,user_email, hash_pass])
.then(result => {
res.send(`your account has been created successfully, Now you can <a href="/loginStud">Login</a>`);
}).catch(err => {
// THROW INSERTING USER ERROR'S
if (err) throw err;
});
})
.catch(err => {
// THROW HASING ERROR'S
if (err) throw err;
})
}
else{
// COLLECT ALL THE VALIDATION ERRORS
let allErrors = validation_result.errors.map((error) => {
return error.msg;
});
// REDERING login-register PAGE WITH VALIDATION ERRORS
res.render('sign-upStud',{
register_error:allErrors,
old_data:req.body
});
}
});// END OF REGISTER PAGE
// LOGIN PAGE
app.post('/signin', ifLoggedin, [
body('user_email').custom((value) => {
return dbConnection.execute('SELECT `email` FROM `users` WHERE `email`=?', [value])
.then(([rows]) => {
if(rows.length == 1){
return true;
}
return Promise.reject('Invalid Email Address!');
});
}),
body('user_pass','Password is empty!').trim().not().isEmpty(),
], (req, res) => {
const validation_result = validationResult(req);
const {user_pass, user_email} = req.body;
if(validation_result.isEmpty()){
dbConnection.execute("SELECT * FROM `users` WHERE `email`=?",[user_email])
.then(([rows]) => {
// console.log(rows[0].password);
bcrypt.compare(user_pass, rows[0].password).then(compare_result => {
if(compare_result === true){
req.session.isLoggedIn = true;
req.session.userID = rows[0].id;
res.redirect('/website-warden-dashboard');
}
else{
res.render('sign-up',{
login_errors:['Invalid Password!']
});
}
})
.catch(err => {
if (err) throw err;
});
}).catch(err => {
if (err) throw err;
});
}
else{
let allErrors = validation_result.errors.map((error) => {
return error.msg;
});
// REDERING login-register PAGE WITH LOGIN VALIDATION ERRORS
res.render('sign-up',{
login_errors:allErrors
});
}
});
// END OF LOGIN PAGE
// LOGIN PAGE for student
app.post('/signinStud', ifLoggedin, [
body('user_email').custom((value) => {
return dbConnection.execute('SELECT `email` FROM `usersStud` WHERE `email`=?', [value])
.then(([rows]) => {
if(rows.length == 1){
return true;
}
return Promise.reject('Invalid Email Address!');
});
}),
body('user_pass','Password is empty!').trim().not().isEmpty(),
], (req, res) => {
const validation_result = validationResult(req);
const {user_pass, user_email} = req.body;
if(validation_result.isEmpty()){
dbConnection.execute("SELECT * FROM `usersStud` WHERE `email`=?",[user_email])
.then(([rows]) => {
// console.log(rows[0].password);
bcrypt.compare(user_pass, rows[0].password).then(compare_result => {
if(compare_result === true){
req.session.isLoggedIn = true;
req.session.userID = rows[0].id;
res.redirect('/website-student-dashboard');
}
else{
res.render('sign-up',{
login_errors:['Invalid Password!']
});
}
})
.catch(err => {
if (err) throw err;
});
}).catch(err => {
if (err) throw err;
});
}
else{
let allErrors = validation_result.errors.map((error) => {
return error.msg;
});
// REDERING login-register PAGE WITH LOGIN VALIDATION ERRORS
res.render('sign-upStud',{
login_errors:allErrors
});
}
});
// END OF LOGIN PAGE for student
// Forume Code
io.on("connection", function (socket) {
console.log("socket connected = " + socket.id);
socket.on("delete_message", function (id) {
// console.log('time to delete');
db.query("DELETE FROM messages WHERE id = '" + id + "'", function (error, result) {
io.emit("delete_message", id);
});
});
socket.on("new_message", function (data) {
console.log("Client says", data)
io.emit("new_message", data);
db.query("INSERT INTO messages(message) VALUES('" + data + "')", function (error, result) {
//data.id = result.insertId;
io.emit("new_message", {
id: result.insertId,
message: data
})
});
});
});
app.get("/get_messages", function (request, result) {
db.query("SELECT * FROM messages", function (error, messages) {
result.end(JSON.stringify(messages));
});
});
//Complaint Raise Code
io.on("connection", function (socket) {
console.log("socket connected = " + socket.id);
socket.on("delete_complaint", function (id) {
// console.log('time to delete');
db.query("DELETE FROM Complaints WHERE id = '" + id + "'", function (error, result) {
io.emit("delete_complaint", id);
});
});
socket.on("new_complaint", function (data) {
console.log("Client says", data)
io.emit("new_complaint", data);
db.query("INSERT INTO Complaints(complaint) VALUES('" + data + "')", function (error, result) {
data.id = result.insertId;
io.emit("new_complaint", {
id: result.insertId,
complaint: data
})
});
});
});
app.get("/get_complaint", function (request, result) {
db.query("SELECT * FROM Complaints", function (error, Complaints) {
result.end(JSON.stringify(Complaints));
});
});
// LOGOUT
app.get('/logout',(req,res)=>{
//session destroy
req.session = null;
res.redirect('/');
});
// END OF LOGOUT
// set the app to listen on the port
http.listen(port, () => {
console.log(`Server running on port: ${port}`);
});