-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreviews-router.js
157 lines (146 loc) · 5.25 KB
/
reviews-router.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
const mongoose = require("mongoose");
const ObjectId= require('mongoose').Types.ObjectId;
const Movie = require("./models/MovieModel");
const User = require("./models/UserModel");
const Person = require("./models/PersonModel");
const Review = require("./models/ReviewModel");
const Notification = require("./models/NotificationModel");
const express = require('express');
let router = express.Router();
router.post("/", createReview, pushReviewIDtoUser, pushReviewIDtoMovie, getUser, createNotificationObject, pushNotificationIDtoFollowers);
//////////////////////////CREATES A NEW REVIEW RESOURCE////////////////////////////////////
//creates a review document based off of the Review schema
function createReview(req, res, next){
if(req.session.loggedin){
//creates a Review document based off provided information
let newReview = new Review();
newReview._id = mongoose.Types.ObjectId();
newReview.username = req.session.userID;
newReview.movieId = mongoose.Types.ObjectId(req.body.movieID);
newReview.rating = req.body.rating;
if(req.body.hasOwnProperty("briefsummary")){
newReview.briefsummary = req.body.briefsummary;
}
if(req.body.hasOwnProperty("review")){
newReview.review = req.body.review;
}
//saves the document
newReview.save(function(err, user) {
if (err){
if(err.name === 'ValidationError'){
res.sendStatus(400); //Bad request, the data send by the client failed to get verified and added.
}
else{
console.log(err);
res.sendStatus(500);
//the server had an error saving this document that did not involve validation
}
}
else{
req.reviewObject = newReview;
next();
}
});
}
else{
res.sendStatus(401);
//Authentication is required and has failed or has not yet been provided.
}
}
//pushes the newly made review's ID to the logged in user's list of reviews
function pushReviewIDtoUser(req, res, next){
User.findByIdAndUpdate(req.session.userID,
{$push: {"reviews": req.reviewObject._id}},
{ "new": true, "upsert": true },
function(err, result){
if(err){
console.log(err);
res.sendStatus(500);
//the logged in user's ID in session has already been verified, and the review ID was just created
//so this is a server error
}
else{
next();
}
});
}
//appends the newly made review's ID to the movie it was created for
function pushReviewIDtoMovie(req, res, next){
Movie.findByIdAndUpdate(req.reviewObject.movieId,
{$push: {"reviews": req.reviewObject._id}},
{ "new": true, "upsert": true },
function(err, result){
if(err){
console.log(err);
res.sendStatus(500);
//the movie ID came straight from the URL where the review was made, and the review ID was just created
//so this is a server error
}
else{
//calculates the average rating of the movie with the newly added review
if(!result.rating){
result.rating = req.body.rating;
result.save()
}
else{
result.rating = result.calcAvRating(req.body.rating);
result.save()
}
next();
}
});
}
//gets the user object of the user in session (logged-in user)
function getUser(req, res, next){
User.findById(req.session.userID, function(err, result){
if(err){
res.setHeader('content-type', 'application/json');
res.status(500).send(req.reviewObject);
//the logged in user's ID in session has already been verified, so this is a server error
}
else{
req.user = result;
next();
}
});
}
//creates a Notification object so that the followers of the logged-in user can get an alert that a review has been made
function createNotificationObject(req, res, next){
//creates a new Notification document
let newNotification = new Notification();
newNotification._id = mongoose.Types.ObjectId();
newNotification.user = req.session.userID;
newNotification.movieId = req.reviewObject.movieId;
newNotification.nType = 0;
//saves the document
newNotification.save(function(err, user) {
if (err) {
console.log(err);
res.setHeader('content-type', 'application/json');
res.status(500).send(req.reviewObject);
//everything up until this point should've been verified, so this is a server error.
}
else{
req.notification = newNotification;
next();
}
});
}
//pushes the Notification object ID to the notifications array of each of the followers on the logged-in user
function pushNotificationIDtoFollowers(req, res, next){
User.updateMany({'_id': {$in: req.user.followers}}, { $push: { "notifications": req.notification._id }}, function(err, results){
if(err){
console.log(err);
res.setHeader('content-type', 'application/json');
res.status(500).send(req.reviewObject);
//these ids should've already been verified by the server, so if they can't be added then the server has a problem.
}
else{
res.setHeader('content-type', 'application/json');
res.status(201).send(req.reviewObject);
//sends back the newly made review object
}
});
}
//Export the router so it can be mounted in the main app
module.exports = router;