All right, guys.
So now we're going to move on to some extra features.
And I want to start with reviews.
Now to create a review, we're going to have to have an end point in our back end that we hit, which
is going to be slash API, slash products, slash whatever the ID of the product and then slash reviews.
So let's go into back end and we'll start off with the controller.
So let's go into product controller.
And let's see, we're going to go down to the bottom here.
Actually, let's just copy.
We'll just copy this.
And let's say this is going to be two.
Create a new review.
And it's going to be a post request.
And like I said, it's going to be API products ID and then slash review.
Or let's do reviews and it's going to be private.
So you have to be logged in.
And let's call this create.
We'll say create product review.
And let's see.
We'll go ahead and get from the body what we need.
I'm just going to get rid of actually, no, we do need to get the product.
But above that, let's get.
The rating and the comment, okay, because they're going to be able to send a number rating and then
also a comment.
That's what a review contains.
And if we look in our model, so in our product model, we have a review schema.
So rating comment, the name is going to come from the user.
And then we also have the user ID, So here we're getting the product.
So we can actually just keep this the way it is.
Then we're going to check for the product and if there is one, we want to check to see if it's already
reviewed because we don't want the same person to be able to review the product twice.
In fact, we can go ahead and just put this in.
So we have a variable called already reviewed.
We're going to say product dot reviews and then dot find.
Okay, So we're not finding through the products, we're finding through the reviews, which is basically
like a like a sub collection.
And in here we're saying find passing in a callback function for each review, we're going to say if
that review user and we're going to turn that user object ID into a string and match it to the logged
in users ID as a string.
So if that turns out to be true, that means it's already reviewed.
And if it's already reviewed, we're going to send a 400 and just say product already reviewed.
Then we're going to create the review object here.
So the name again comes from the user, the logged in user's name.
The rating comes from the body.
It's going to be sent with the form.
We're just making sure it's a number and also the comment and then the user is going to be the user
ID, which comes from the request object.
Okay?
Then once we do that, we're going to take our product reviews and we're going to push onto it the new
review.
And we want to also set the Num reviews to whatever the length of the reviews because remember, products
have a num reviews field and then to get the rating, we're going to factor in the new rating by using
reduce and adding them all together.
So all the ratings together and then dividing it by the length of reviews.
So that will give us the overall rating.
Then we can save it and we'll send a 201, which means something was created and we'll just say review
added else.
If there's no review, then we get a 404 just like with everything else.
Okay.
So pretty straightforward.
Let's go ahead and now.
Export create.
I created.
It should be create.
Create product review.
Okay.
And then we need to go to our route.
So let's go to routes and we want to go to our product routes and let's first off import, create product
review, and then we're going to add let's go down here and we're going to say router dot route and
it's going to be slash colon ID slash reviews, post requests, protect middleware and call create product
review.
So that's it for the back end.
So now in the next video, we're going to jump into the front end and start to implement reviews there.