-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
502 lines (430 loc) · 16.8 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
const express = require('express');
const app = express();
const path = require("path");
const { User, Course, Chapter, Page, Enrollment, Completed } = require("./models");
const bodyParser = require("body-parser");
const passport = require('passport');
const connectEnsureLogin = require('connect-ensure-login');
const session = require('express-session');
const LocalStrategy = require('passport-local');
const bcrypt = require('bcryptjs');
const { constants } = require('fs/promises');
const saltRounds = 10;
const flash = require('connect-flash');
app.use(bodyParser.json());
app.use(express.urlencoded({ extended: false }));
app.set("views", path.join(__dirname, "views"));
app.set("view engine", "ejs");
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({ secret: "my-super-key 1234567890",
resave: false,
saveUninitialized: true,
cookie: { maxAge: 24 * 60 * 60 * 1000 } }));
app.use(passport.initialize());
app.use(passport.session())
app.use(require('express-session')({
secret: 'your-secret-key',
resave: false,
saveUninitialized: true,
cookie: { secure: false }
}));
app.use(flash());
passport.use(new LocalStrategy({
usernameField: "email",
passwordField: "password",
}, async (username, password, done) => {
try {
const user = await User.findOne({ where: { email: username } });
if (!user) {
return done(null, false, { message: "User not found" });
}
console.log("User found in Database")
const result = await bcrypt.compare(password, user.password);
if (result) {
return done(null, user);
} else {
return done(null, false, { message: "Invalid password" });
}
} catch (error) {
return done(error);
}
}));
passport.serializeUser(function (user, done) {
console.log("serializing user in session ", user.id);
done(null, user.id);
});
passport.deserializeUser(async function (id, done) {
try {
const user = await User.findByPk(id);
done(null, user);
} catch (error) {
done(error, null);
}
});
app.use(function (request, response, next) {
response.locals.messages = request.flash();
next();
});
app.get("/", (request, response) => {
if (request.isAuthenticated()) {
return response.redirect("/page");
}
else {
return response.render("index.ejs", {
})
}
});
//route to signup page
app.get("/signup", async (request, response) => {
response.render('signup.ejs', {
})
})
//route for new user signup "user"
app.post("/user", async (request, response) => {
const hashedPwd = await bcrypt.hash(request.body.password, saltRounds);
console.log(hashedPwd);
try {
const user = await User.create({
name: request.body.name,
email: request.body.email,
password: hashedPwd,
role: request.body.role,
})
console.log("new user added succesfully ")
request.login(user, (error) => {
if (error) {
console.log(error);
console.log("An error occurred during login through user");
}
response.redirect(`/${user.role}`)
}
);
} catch (error) {
console.log("Error while crerating a new User");
console.log(error)
response.status(500).json(error);
}
})
app.get("/login", async (request, response) => {
console.log("login failed")
response.render('login.ejs', {
message: request.flash('error')
});
});
app.post("/session", passport.authenticate("local", {
successRedirect: "/page",
failureRedirect: "/login",
failureFlash: true
}));
app.get("/page", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
response.redirect(`/${request.user.role}`);
})
app.get("/Educator", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
console.log(`Educator logged in successfully `)
console.log(request.user.name)
const createdCourses = await Course.findAll({ where: { created: request.user.id } })
try {
await response.render('educator.ejs', {
name: request.user.name,
id: request.user.id,
createdCourses: createdCourses,
});
} catch (error) {
console.log("Error while rendering the Educator Homepage")
console.log(error);
}
});
// Routing--realted--password-change
app.get('/password', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
response.render("password.ejs")
} catch (error) {
console.log('Error in rendering password-change page')
}
})
app.post("/password", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
if (request.body.newPassword === request.body.confirmPassword) {
const hashedPwd = await bcrypt.hash(request.body.newPassword, saltRounds);
const updatedUser = await User.update({ password: hashedPwd }, { where: { id: request.user.id } });
if (updatedUser) {
console.log("Password updated successfully");
response.redirect("/pwdSucces");
} else {
console.log("Failed to update password");
response.redirect("/password-change");
}
} else {
console.log("New password and confirm password do not match");
response.redirect("/password");
}
} catch (error) {
console.error("Error updating password:", error);
response.redirect("/password");
}
});
// Success pages
app.get("/pwdSucces", async (request, response) => {
await response.render('pwdSucces.ejs')
})
//Route-for-creating-course
app.post("/course", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
const course = await Course.create({
name: request.body.courseName,
description: request.body.courseDescription,
created: request.user.id
});
console.log("New course added!");
response.redirect(`/course/${course.id}`);
} catch (error) {
console.log("New course not added!");
console.log(error);
return response.status(422).json(error);
}
});
app.get("/course/:courseId", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
const course = await Course.findByPk(request.params.courseId)
const chapters = await Chapter.findAll({ where: { courseId: request.params.courseId } })
response.render('course.ejs', {
courseName: course.name,
courseId: course.id,
courseDescription: course.description,
chapters: chapters,
})
} catch (error) {
console.log("Error while rendering Course Index page")
}
})
// routing-related-to-creation-of-chapters
app.post('/chapter/', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
const chapter = await Chapter.create({
name: request.body.chapterName,
courseId: request.body.courseId,
});
console.log("New chapter added!");
response.redirect(`/chapter/${chapter.id}`);
} catch (error) {
console.log("New chapter not added!");
console.log(error);
return response.status(422).json(error);
}
});
app.get('/chapter/:chapterId', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const pages = await Page.findAll({ where: { chapterId: request.params.chapterId } })
try {
const chapter = await Chapter.findByPk(request.params.chapterId)
response.render('chapter.ejs', {
chapterName: chapter.name,
chapterId: chapter.id,
pages: pages,
courseId: chapter.courseId,
})
} catch (error) {
console.log("Error while rendering Chapter Index page")
}
})
// routing--related-to-creation-of-page
app.post("/page", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
const page = await Page.create({
name: request.body.pageName,
chapterId: request.body.chapterId,
content: request.body.pageContent,
});
console.log("New page added!");
response.redirect(`/chapter/${request.body.chapterId}`);
} catch (error) {
console.log("New page not added!");
console.log(error);
return response.status(422).json(error);
}
});
app.get("/editPage/:pageId", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
const page = await Page.findOne({ where: { id: request.params.pageId } });
await response.render("editPage.ejs", {
page: page
})
} catch (error) {
console.log("Error in getting the details of a particular page");
}
})
app.post("/savePageChanges", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const { pageId, editedContent } = request.body;
try {
console.log('Received Page ID:', pageId);
await Page.update(
{ content: editedContent },
{ where: { id: pageId } })
} catch (error) {
console.log("Error in editing the page")
}
})
//==============================Student routes ==================================
app.get("/Student", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
console.log("student logged in successfully");
const availcourses = await Course.findAll();
const enrollments = await Enrollment.findAll({ where: { userId: request.user.id } })
const enrolledCoursesIds = enrollments.map((enrollment) => enrollment.courseId)
const enrolledCourses = await Course.findAll({ where: { id: enrolledCoursesIds } })
try {
await response.render('student.ejs', {
studentName: request.user.name,
studentId: request.user.id,
availcourses: availcourses,
enrolledCourses: enrolledCourses,
});
} catch (error) {
console.log("Error while rendering the Student Homepage")
console.log(error);
}
});
app.get('/viewCourse/:courseId', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
const chapters = await Chapter.findAll({ where: { courseId: request.params.courseId } })
const course = await Course.findByPk(request.params.courseId)
response.render('viewCourse.ejs', {
course: course,
chapters: chapters,
});
} catch (error) {
console.log(error)
}
})
app.get('/enrollCourse/:courseId', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const course = await Course.findByPk(request.params.courseId);
const enrollmentStatus = await Enrollment.findOne({ where: { userId: request.user.id, courseId: course.id } })
if (enrollmentStatus) {
response.render('alreadyEnrolled.ejs', {
course: course
})
} else {
response.render('enrollCourse.ejs', {
course: course
})
}
})
app.post('/enrollCourse/:courseId', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const enrollment = await Enrollment.create({
userId: request.user.id,
courseId: request.params.courseId,
})
console.log("New Enrollment created succesfully")
response.redirect(`/scourse/${request.params.courseId}`)
})
app.get('/scourse/:courseId', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const chapters = await Chapter.findAll({ where: { courseId: request.params.courseId } })
const course = await Course.findByPk(request.params.courseId);
const completedPages = await Completed.findAll({ where: { userId: request.user.id, courseId: request.params.courseId } })
const CompletedpagesCnt = completedPages.length;
const findNumberOfPages = async (chapter) => {
const pages = await Page.findAll({ where: { chapterId: chapter.id } });
return pages.length;
};
// Use Promise.all to asynchronously get the number of pages for each chapter
const chaptersWithPageCountPromises = chapters.map(async (chapter) => {
const pageCount = await findNumberOfPages(chapter);
return { pageCount };
});
// Wait for all promises to resolve
const chaptersWithPageCount = await Promise.all(chaptersWithPageCountPromises);
const totalPageCount = chaptersWithPageCount.reduce((sum, chapter) => sum + chapter.pageCount, 0);
console.log(totalPageCount)
const percentage = (CompletedpagesCnt / totalPageCount) * 100 || 0.0
response.render('scourse.ejs', {
course: course,
chapters: chapters,
percentage: percentage.toFixed(1),
})
})
app.get('/schapter/:chapterId', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const pages = await Page.findAll({ where: { chapterId: request.params.chapterId } })
const chapter = await Chapter.findByPk(request.params.chapterId);
const course = await Course.findByPk(chapter.courseId)
const completedPages = await Completed.findAll({ where: { userId: request.user.id, courseId: course.id, chapterId: chapter.id } })
response.render('schapter.ejs', {
course: course,
chapter: chapter,
pages: pages,
completedPages: completedPages,
})
})
app.get("/spage/:pageId", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const page = await Page.findByPk(request.params.pageId)
const chapter = await Chapter.findByPk(page.chapterId)
const course = await Course.findByPk(chapter.courseId)
const enrollment = await Enrollment.findOne({ where: { userId: request.user.id, courseId: course.id } })
const CompletedStatus = await Completed.findOne({ where: { userId: request.user.id, courseId: course.id, chapterId: chapter.id, pageId: page.id } })
await response.render("spage.ejs", {
page: page,
chapter: chapter,
course: course,
CompletedStatus: CompletedStatus,
})
})
//================Routes-related-to-completion-of-page================
app.post("/completePage/:pageId", connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
const page = await Page.findByPk(request.params.pageId)
const chapter = await Chapter.findByPk(page.chapterId)
const course = await Course.findByPk(chapter.courseId)
const enrollment = await Enrollment.findOne({ where: { userId: request.user.id, courseId: course.id } })
const CompletedStatus = await Completed.findOne({ where: { userId: request.user.id, courseId: course.id, chapterId: chapter.id, pageId: page.id } })
if (CompletedStatus) {
request.flash('info', 'Page already marked as Compeleted.');
console.log("Page already marked as Compeleted")
} else {
const completePage = await Completed.create({
userId: request.user.id,
courseId: course.id,
chapterId: chapter.id,
pageId: page.id,
enrollmentId: enrollment.id,
pageName: page.name,
chapterName: chapter.name,
courseName: course.name,
})
console.log("Page marked as Compelted")
request.flash('info', 'Page marked as Compeleted.');
}
response.redirect(`/spage/${page.id}`)
})
//========================View Report===============================
//Routing--related-to-view-report-to-educator
app.get('/courseEnrollments/:courseId', connectEnsureLogin.ensureLoggedIn(), async (request, response) => {
try {
const totalEnrollments = await Enrollment.findAll();
const totalEnrollmentsCnt = totalEnrollments.length;
const enrollments = await Enrollment.findAll({ where: { courseId: request.params.courseId } })
const enrollmentUserIds = enrollments.map((enrollment) => enrollment.userId)
const enrollmentCnt = enrollments.length;
const createdBy = request.user.name;
const course = await Course.findByPk(request.params.courseId)
const enrolledstudents = await User.findAll({ where: { id: enrollmentUserIds } })
const percentage = (enrollmentCnt / totalEnrollmentsCnt) * 100 || 0
await response.render('courseEnrollments.ejs', {
course: course,
enrolledstudents: enrolledstudents,
createdBy: createdBy,
enrollmentCnt: enrollmentCnt,
enrollments: enrollments,
percentage: percentage.toFixed(1),
})
} catch (error) {
console.log(error)
}
});
//======================Sign Out===================
app.get("/signout", (request, response, next) => {
request.logout((err) => {
if (err) { return next(err); }
console.log("Sign out Successfully")
response.redirect('/signOut-success');
});
});
app.get("/signOut-success", async (request, response) => {
await response.render("signOut.ejs")
})
module.exports = app;