-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
1505 lines (1399 loc) · 54.3 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
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
const log = console.log;
// starting the express server
const express = require("express");
const app = express();
// body-parser: middleware for parsing HTTP JSON body into a usable object
const bodyParser = require("body-parser");
app.use(bodyParser.json());
// express-session for managing user sessions
const session = require("express-session");
app.use(bodyParser.urlencoded({extended: true}));
// fileUpload for uploading file to courses
const fileUpload = require("express-fileupload");
app.use(
fileUpload({
limits: {fileSize: 500 * 1024 * 1024}
})
);
const fs = require("fs");
const fsPromises = fs.promises;
// datetime for converting date to string
const datetime = require("date-and-time");
// building database
const {ObjectID} = require("mongodb");
const {mongoose} = require("./db/mongoose");
mongoose.set("useFindAndModify", false);
const {RegularUser} = require("./models/RegularUser");
const {Course} = require("./models/Course");
const {BillBoard} = require("./models/BillBoard");
const {File} = require("./models/File");
//console.log("Welcome to server.js");
// Create a session cookie
app.use(
session({
secret: "csc309", // using this for now, may have a security concern
resave: false,
saveUninitialized: false,
cookie: {
expires: 6000000,
httpOnly: true
}
})
);
/*
user authentication routes
*/
// A route to login and create a session
app.post("/RegularUser/login", (req, res) => {
const username = req.body.username;
const password = req.body.password;
//console.log("username: " + username);
//console.log("password: " + password);
RegularUser.findByUsernamePassword(username, password)
.then(user => {
//console.log("found user");
req.session.username = user.username;
req.session.currentUserID = user._id;
//console.log("ready to send response1");
//console.log("ready to send response2");
res.send({currentUser: user.username});
})
.catch(error => {
res.status(400).send();
});
});
// A route for admin to access user information
app.post("/RegularUser/access", (req, res) => {
const userid = req.body.userid;
RegularUser.findById(userid)
.then(user => {
req.session.username = user.username;
req.session.currentUserID = user._id;
res.send({currentUser: user.username});
})
.catch(error => {
res.status(400).send();
});
});
// A route for admin to delete a user
app.post("/RegularUser/remove", (req, res) => {
const userid = req.body.userid;
RegularUser.findById(userid)
.then(user => {
if (!user) {
res.status(404).send();
} else {
const teachingList = user.coursesTeaching;
const takingList = user.coursesTaking;
// find all courses the user to be removed was teaching
teachingList.forEach(courseObjectID => {
Course.findById(courseObjectID).then(course => {
// remove the user from the course's list
course.users.shift();
// if there's another user in course, that user will be the new course admin
if (course.users[0] !== undefined) {
RegularUser.findById(course.users[0]).then(newUser => {
// remove the new admin from course taking
newUser.coursesTaking.pull(course);
// put the new admin into the course he/she will instruct
newUser.coursesTeaching.push(course);
newUser.save();
});
}
// otherwise, there will be no course admin until next user join the course
course.save();
});
});
// deal with the course admin to be removed
// remove it from all the courses he/she was taking
// to prevent unnecessary problem when switching course admin
takingList.forEach(courseObjectID => {
Course.findById(courseObjectID).then(course => {
user.coursesTaking.pull(course);
course.users.pull(user);
course.save();
user.save();
});
});
}
})
.catch(error => res.status(500).send());
RegularUser.findByIdAndDelete(userid)
.then(result => {
res.send(result);
})
.catch(error => {
res.status(400).send();
});
});
// delete a course in a user's profile, taking or teaching
app.delete("/RegularUser/:userID/removeCourse/:courseName", (req, res) => {
const userID = req.params.userID;
RegularUser.findById(userID).then((user) => {
if (!user) {
res.status(404).send();
} else {
const courseName = req.params.courseName;
Course.findByCourseName(courseName)
.then((course) => {
if (course.users[0] === userID) {
// delete admin's course
course.users.shift();
// case 1: there's another user in the course
// he/she will become the new course admin
if (course.users[0] !== undefined) {
RegularUser.findById(course.users[0]).then(newUser => {
// remove the new admin from course taking
newUser.coursesTaking.pull(course._id);
// put the new admin into the course he/she will instruct
newUser.coursesTeaching.push(course._id);
newUser.save();
});
}
// case 2: there's only the admin user
// there will be no admin for that course until the next user enroll the course
// and he/she will become the new course admin
// remove the course from CourseTeaching list of the user
user.coursesTeaching.pull(course._id);
} else {
// user is not the admin in the course to be deleted
// delete the user from the course' users list
course.users.pull(userID);
// delete the course from the user's CourseTaking list
user.coursesTaking.pull(course._id);
}
if (user.coursesLiked.includes(course._id)) {
user.coursesLiked.pull(course._id);
course.likes--;
}
course.save();
user.save();
return res.send();
})
}
})
});
app.post("/RegularUser/signup", (req, res) => {
const username = req.body.username;
//console.log("Create a new user");
//console.log("username: " + username);
RegularUser.findOne({username: username})
.then(result => {
if (!result) {
const new_RegularUser = new RegularUser({
username: req.body.username,
password: req.body.password,
GPA: req.body.GPA,
gender: req.body.gender,
levelOfEducation: req.body.levelOfEducation,
fieldOfStudy: req.body.fieldOfStudy,
coursesTeaching: [],
coursesTaking: []
});
new_RegularUser.save().then(
user => {
req.session.username = user.username;
res.send({currentUser: user.username});
//res.send(result);
},
error => {
res.status(400).send(error);
}
);
} else {
//console.log("Username already exists");
res.status(400).send();
}
})
.catch(error => {
res.status(400).send();
});
});
// A route to logout a user
app.get("/RegularUser/logout", (req, res) => {
// Remove the session
req.session.destroy(error => {
if (error) {
res.status(500).send(error);
} else {
res.send();
}
});
});
// A route to check if a use is logged in on the session cookie
app.get("/RegularUser/check-session", (req, res) => {
if (req.session.username) {
res.send({
currentUser: req.session.username,
currentUserID: req.session.currentUserID
});
} else {
res.status(401).send();
}
});
// A rounte to get all regular users
app.get("/AllRegularUser", (req, res) => {
RegularUser.find()
.then(result => {
if (!result) {
res.status(404).send();
} else {
res.send(result);
}
})
.catch(error => {
res.status(500).send();
});
});
// API rountes start here
app.get("/RegularUser/username/password", (req, res) => {
//console.log("Access User");
const username = req.body.username;
const password = req.body.password;
//console.log("username: " + username);
//console.log("password: " + password);
RegularUser.findByUsernamePassword(username, password)
.then(user => {
res.send(user);
})
.catch(error => {
res.status(400).send();
});
});
app.post("/RegularUser", (req, res) => {
//console.log("post a new regular user");
const new_RegularUser = new RegularUser({
username: req.body.username,
password: req.body.password,
GPA: req.body.GPA,
gender: req.body.gender,
levelOfEducation: req.body.levelOfEducation,
fieldOfStudy: req.body.fieldOfStudy,
coursesTeaching: [],
coursesTaking: []
});
new_RegularUser.save().then(
result => {
res.send(result);
},
error => {
res.status(400).send(error);
}
);
});
// regular users can get courses thru this request, so that their course list will be displayed
// on their dashboard
app.get("/courses", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
res.status(403).send();
return;
}
RegularUser.findById(currentUserID)
.then(user => {
if (!user) {
res.status(404).send();
} else {
let count = 0;
const list = [];
const rawList = user.coursesTeaching.concat(user.coursesTaking);
if (rawList.length === 0) {
res.send({courses: list});
}
//console.log(rawList);
rawList.forEach(courseObjectID =>
Course.findById(courseObjectID).then(course => {
// users[0] is always the course admin, who is also a regular user
RegularUser.findById(course.users[0]).then(admin => {
const thisCourse = {};
thisCourse.name = course.name;
thisCourse.info = course.description;
// admin: for displaying the admin on the dashboard
thisCourse.admin = admin.username;
thisCourse.liked = user.coursesLiked.includes(course._id);
list.push(thisCourse);
count++;
if (count === rawList.length) {
res.send({courses: list});
}
});
})
);
//console.log(list);
}
})
.catch(error => res.status(500).send());
});
// users can create a course thru this request. since she/he creates the course,
// she/he is the first user in the "users" array. Therefore, users[0] is the
// course admin.
app.post("/courses", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
res.status(403).send();
return;
}
const courseName = req.body.name.toUpperCase();
console.log(courseName);
Course.findOne({name: courseName})
.then(existedCourse => {
if (existedCourse) {
return res.status(400).send(); // duplicated course name
}
const course = new Course({
name: courseName,
description: req.body.description,
users: [currentUserID]
});
// Save the course
course.save().then(
result => {
RegularUser.findById(currentUserID).then(user => {
user.coursesTeaching.push(course._id);
user.save();
res.send(result);
});
},
error => {
res.status(400).send(error); // 400 for bad request
}
);
})
.catch(error => {
console.log(error);
return res.status(500).send(); // server error
});
});
// a user can get the course tuple from the DB thru this request, which typically includes
// the course admin, chatroom, messages, and announcements in this course
app.get("/getCourses/:courseName", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
return res.status(403).send(); // user not logged in
}
const courseName = req.params.courseName;
Course.findByCourseName(courseName)
.then(course => {
if (!course) {
log("invalid course name");
res.status(404).send(); // could not find this resource
} else {
const theCourse = {
// admin: for checking whether the current user has permission to post announcements
// also, it would be used for displaying the admin's profile
admin: course.users[0],
announcements: course.announcements,
chatroom: []
};
if (course.chatroom.length === 0) {
res.send({course: theCourse});
}
let count = 0;
const chatroom = [];
course.chatroom.forEach((msg) => {
RegularUser.findById(msg.user_id).then(
(user) => {
// if the user does not exist, it is very likely she/he has been deleted
// after sending this message
const updatedMsg = {
user_id: user ? msg.user_id : 0,
date: datetime.format(msg.date, "h:mm:s on MMM D"),
message: msg.message,
username: user ? user.username : "User has been removed"
};
chatroom.push(updatedMsg);
count++;
//log(msg);
if (count === course.chatroom.length) {
theCourse.chatroom = chatroom;
res.send({course: theCourse});
}
},
(error) => {
return res.send(400).send(error);
}
);
});
}
})
.catch(error => {
console.log(error);
res.status(500).send(); // server error
});
});
app.get("/getRankings", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
return res.status(403).send(); // user not logged in
}
Course.find()
.then(courses => {
courses.sort((a, b) => {
if (a.likes > b.likes) {
return -1;
} else if (a.likes < b.likes) {
return 1;
} else {
return 0;
}
});
RegularUser.find()
.then(users => {
// exclude admin from the list
users.splice(0, 1);
users.sort((a, b) => {
if (a.coursesTeaching > b.coursesTeaching) {
return -1;
} else if (a.coursesTeaching < b.coursesTeaching) {
return 1;
} else {
return 0;
}
});
return res.send({
likesRankings: courses.slice(0, 8),
usersRankings: users.splice(0, 8)
});
})
.catch(error => {
console.log(error);
return res.status(500).send();
});
})
.catch(error => {
console.log(error);
return res.status(500).send();
});
});
// get resources of some course given a course name
app.get("/courses/:courseName/getResources", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
return res.status(400).send(); // user not logged in
}
const courseName = req.params.courseName;
RegularUser.findById(currentUserID)
.then(user => {
if (!user) {
res.status(404).send(); // could not find this user
}
Course.findByCourseName(courseName)
.then(course => {
if (!course) {
log("invalid course name");
return res.status(404).send(); // could not find this resource
}
if (!course.users.includes(currentUserID)) {
return res.status(403).send(); // unauthorised user
}
const resourcesList = [];
// if this courses does not have any resources, simply send an empty array
if (course.resources.length === 0) {
res.send({
// for checking whether the current user has permission to upload
admin: course.users[0],
resources: resourcesList
});
}
let count = 0;
course.resources.forEach((fileObjectID) => {
File.findById(fileObjectID).then((fileDBEntry) => {
const newResource = {};
newResource.file_id = fileObjectID;
newResource.name = fileDBEntry.name;
newResource.link = `/download/${fileObjectID}`;
newResource.type = fileDBEntry.type;
newResource.size = fileDBEntry.size;
newResource.date = datetime.format(fileDBEntry.date, "M/D/Y h:mm A");
newResource.favoured = user.filesFavoured.includes(fileObjectID);
resourcesList.push(newResource);
count++;
if (count === course.resources.length) {
return res.send({
// for checking whether the current user has permission to upload
admin: course.users[0],
resources: resourcesList
});
}
}).catch(error => {
console.log(error);
res.status(500).send(); // server error
})
});
})
.catch(error => {
console.log(error);
res.status(500).send(); // server error
});
}).catch(error => {
console.log(error);
res.status(500).send(); // server error
});
});
// function for a user to join a course given a course name
app.patch("/courses/:courseName", (req, res) => {
const userID = req.session.currentUserID;
if (!userID) {
return res.status(400).send(); // user not logged in
}
const courseName = req.params.courseName;
let theCourse = null;
Course.findByCourseName(courseName)
.then(course => {
if (!course) {
log("invalid course name");
return res.status(404).send(); // could not find this resource
} else {
log(userID);
course.users.addToSet(userID);
course.save().then(
() => {
RegularUser.findById(userID)
.then(user => {
if (
user.coursesTeaching.includes(course._id) ||
user.coursesTaking.includes(course._id)
) {
return res.status(400).send();
}
if (course.users.length === 1) {
user.coursesTeaching.addToSet(course._id);
} else {
user.coursesTaking.addToSet(course._id);
}
user.save().then(
() => {
return res.send({
message: "Successfully enrolled!"
});
},
error => {
log("bad request");
return res.status(400).send(error); // 400 for bad request
}
);
})
.catch(error => {
console.log(error);
return res.status(500).send(); // server error
});
},
error => {
log("bad request");
return res.status(400).send(error); // 400 for bad request
}
);
}
})
.catch(error => {
console.log(error);
return res.status(500).send(); // server error
});
});
// add an announcement to the course
// 1. get the course
// 2. check whether the current user is the admin of the course
// 3. push the announcement into course.announcements
// 4. delete the first announcement whenever there are more than 3 announcements
app.post("/courses/:courseName/announcement", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
res.status(400).send();
return;
}
let theCourse = null;
const courseName = req.params.courseName;
Course.findByCourseName(courseName)
.then(course => {
if (!course) {
log("invalid course name");
res.status(404).send(); // could not find this resource
} else {
// check whether the current user is the admin of the course,
// given course.users[0] is always the course admin
if (course.users[0] != currentUserID) {
return res.status(403).send();
} else {
const newAnnouncement = {
title: req.body.title,
content: req.body.content
};
course.announcements.push(newAnnouncement);
//see if there are more than 3 announcements
if (course.announcements.length > 3) {
//delete the oldest announcement
course.announcements.shift();
}
//save the course
course.save().then(
result => {
return res.send({
message: "announcement sent successfully"
});
},
error => {
return res.status(400).send(error);
}
);
}
}
})
.catch(error => {
console.log(error);
return res.status(500).send(); // server error
});
});
app.delete("/courses/:courseName/:announcement", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
res.status(400).send();
return;
}
let theCourse = null;
const courseName = req.params.courseName;
console.log(courseName);
const _id = req.params.announcement;
Course.findByCourseName(courseName)
.then(course => {
if (!course) {
log("invalid course name");
res.status(404).send(); // could not find this resource
} else {
//find the announcements
const announcement = course.announcements.id(_id);
if (!announcement) {
log("invalid announcement id");
res.status(404).send(); // could not find this resource
} else {
// check whether the current user is the admin of the course,
// given course.users[0] is always the course admin
if (course.users[0] != currentUserID) {
return res.status(403).send();
} else {
//find the macthed announcement (by id)
// const theAnnouncement = course.announcements.id(_id);
course.announcements.pull(announcement);
//save the course
course.save().then(
result => {
return res.send({
message: "announcement deleted successfully"
});
},
error => {
return res.status(400).send(error);
}
);
}
}
//hi i am working here
}
})
.catch(error => {
console.log(error);
return res.status(500).send(); // server error
});
});
/* Bill Board API Route */
// return all bill board content
app.get("/BillBoard/content", (req, res) => {
const userid = req.session.currentUserID;
if (userid !== undefined) {
BillBoard.find()
.then(result => {
if (!result) {
res.status(404).send();
} else {
res.send(result);
}
})
.catch(error => {
res.status(500).send();
});
} else {
console.log("Unauthorized access to BillBoard");
res.send(401).send();
}
});
// add a new bill board content
/* request body:
{
"username": ,
"date": ,
"message": ,
"image":
}
*/
app.post("/BillBoard/new", (req, res) => {
const userid = req.session.currentUserID;
//const userid = req.body.userid;
// const username = req.body.username;
// const date = req.body.date;
// const message = req.body.message;
// const image = req.body.image;
// console.log("Create a new billboard message");
// console.log("User id: " + userid);
// console.log("username: " + username);
// console.log("date: " + date);
// console.log("message: " + message);
// console.log("image: " + image);
if (userid !== undefined) {
RegularUser.findById(userid)
.then(user => {
if (!user) {
res.status(404).send();
} else {
//console.log("Found user, add billlboard content");
const new_BillBoard_content = new BillBoard({
userid: req.session.currentUserID,
username: req.body.username,
date: req.body.date,
message: req.body.message,
image: req.body.image
});
//console.log("Creating new content");
console.log(new_BillBoard_content);
new_BillBoard_content.save().then(
result => {
//console.log("Saving new content");
res.send(result);
},
error => {
res.send(400).send();
}
);
}
})
.catch(error => {
res.status(400).send();
});
} else {
//console.log("Unauthorized access to BillBoard");
res.send(401).send();
}
});
app.post("/BillBoard/delete", (req, res) => {
const comment_id = req.body._id;
//console.log(comment_id);
BillBoard.findByIdAndDelete(comment_id)
.then(result => {
res.send(result);
})
.catch(error => {
res.status(400).send();
});
});
/* Profile API routes*/
// return regular user profile of the current user
app.get("/RegularUser/profile", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
//console.log("Unauthorized access to user profile");
res.status(401).send();
}
RegularUser.findById(currentUserID)
.then(user => {
if (!user) {
//console.log("Regular user does not exist");
res.status(404).send();
} else {
res.send(user);
}
})
.catch(error => {
res.status(500).send();
});
});
// return regular user profile of the current user
app.get("/RegularUser/getProfileById/:user_id", (req, res) => {
const currentUserID = req.session.currentUserID;
if (!currentUserID) {
//console.log("Unauthorized access to user profile");
return res.status(401).send();
}
const user_id = req.params.user_id;
RegularUser.findById(user_id)
.then(user => {
if (!user) {
//console.log("Regular user does not exist");
return res.status(404).send();
}
//console.log(user);
const userProfile = {
username: user.username,
gender: user.gender,
GPA: user.GPA,
levelOfEducation: user.levelOfEducation,
fieldOfStudy: user.fieldOfStudy,
coursesTaking: [],
coursesTeaching: []
};
if (
user.coursesTaking.length === 0 &&
user.coursesTeaching.length === 0
) {
return res.send(userProfile);
}
if (user.coursesTaking.length === 0) {
let count = 0;
user.coursesTeaching.forEach(courseTeaching_id => {
Course.findById(courseTeaching_id)
.then(courseTeaching => {
userProfile.coursesTeaching.push(courseTeaching.name);
count++;
if (count === user.coursesTeaching.length) {
return res.send(userProfile);
}
})
.catch(error => {
return res.status(500).send();
});
});
}
if (user.coursesTeaching.length === 0) {
let count = 0;
user.coursesTaking.forEach(courseTaking_id => {
Course.findById(courseTaking_id)
.then(courseTaking => {
userProfile.coursesTaking.push(courseTaking.name);
count++;
if (count === user.coursesTaking.length) {
return res.send(userProfile);
}
})
.catch(error => {
return res.status(500).send();
});
});
}
let coursesTakingCount = 0;
user.coursesTaking.forEach(courseTaking_id => {
Course.findById(courseTaking_id)
.then(courseTaking => {
userProfile.coursesTaking.push(courseTaking.name);
coursesTakingCount++;
if (coursesTakingCount === user.coursesTaking.length) {
let coursesTeachingCount = 0;
user.coursesTeaching.forEach(courseTeaching_id => {
Course.findById(courseTeaching_id)
.then(courseTeaching => {
userProfile.coursesTeaching.push(courseTeaching.name);
coursesTeachingCount++;
if (coursesTeachingCount === user.coursesTeaching.length) {
return res.send(userProfile);
}
})
.catch(error => {
return res.status(500).send();
});
});
}
})
.catch(error => {
return res.status(500).send();
});
});
})
.catch(error => {
return res.status(500).send();
});
});
// return regular user course taking
app.get("/RegularUser/profile/coursesTaking", (req, res) => {
const currentUserID = req.session.currentUserID;
if (currentUserID) {
RegularUser.findById(currentUserID)
.then(user => {
if (!user) {
console.log("Regular user does not exist");
res.status(404).send();
} else {
let count = 0;
const list = [];
const rawList = user.coursesTaking;
rawList.forEach(courseObjectID =>
Course.findById(courseObjectID).then(course => {
// course.users[0] is always the course admin
RegularUser.findById(course.users[0]).then(admin => {
const thisCourse = {};
thisCourse.id = courseObjectID;
thisCourse.name = course.name;
thisCourse.info = course.description;
thisCourse.admin = admin.username;
thisCourse.liked = true;
list.push(thisCourse);
count++;