Hey, did everyone this year.
And now let's worry about the reviews itself.
Now, reviews are really a simple thing.
It's just like adding a separate page for updating the stock just.
Just like that.
So in this case, we haven't put down any reviews.
Now we're going to do that now reviews.
Just trying to imagine that you'll be having a website.
There will be a lot of cards on that.
So you won't be allowing user to add the reviews just from that page.
You'll be allowing user to add reviews from when somebody clicks on that card and visits the individual
product itself, then we'll allow there or probably somewhere else.
So this is usually the ideal situation where all the details of the product are there.
Just below there the reviews are going to be there.
So if I can go ahead and click on that.
Definitely in that page, I do have an axis of that idea of the product.
Now, how you're going to send the idea of the product.
It's up to you.
You are designing the application.
You can request that in the URL, you can request that in a query.
You can request that in the body however you like to go.
Now, in case you are going to request that in the root itself, then obviously the root is going to
become something like this.
Let me just show you that as a brief example, probably just here.
So if you are requesting that here, so probably something going to look like this, that this is going
to be slash product, then we need to have the product ID itself.
So let's just call this one as simply product product ID and then we are going to have a slash and then
probably more information that you have.
So the point here is that I want to say is you can request the product ID in the URL and the body in
itself and the query as well.
So it depends on you.
Now I will be requesting the this product ID in the body itself.
Again, no big deal.
User can actually go ahead and pass on me this one.
I'll actually copy this one up here.
So let's go ahead and make a copy of this one.
I'll say let's close this one and expand this one and move up here.
So this one is going to be simply add a review.
So let's go ahead and simply say add review.
That's it.
That's pretty much it.
Now let's go ahead and work on with that.
A couple of information that I would like to jog your memory on this one.
So first is the middleware.
So if I go ahead and open this middleware, if you remember that once we are having this request of
user, remember we are finding we are using fine by ID and based on the ID, we are finding a user.
And most important thing is this user is a big object.
Now entirety of the information that is stored in my database about the user, his name, email, his
ID, everything is there.
So just to jog it one more time, if I go ahead and check out the users.
So all this information including is underscore ID has a dot name, dot email, password.
Everything is available to me in the request dot user and that's all the information I need so I can
go ahead and add that view.
Just one last thing before we add that we need to also verify and check that whether this user has already
done a review in the past or not, if he has already added a review in the past, we just want to update
that review and that's pretty much it.
That's all that's all what we're going to do.
Okay.
So first, let's extract all the information from the body.
So all the information will be coming up in request or body.
Let's go ahead and extract that.
So we're going to go ahead and say, hey, I need some information that will be coming up from there.
And precisely the one thing that I'm looking for is the rating that's going to be coming in.
The rating is going to be between one and five.
My front end guy is responsible for restricting that between one and five.
We will be requesting a comment as well.
So we're going to go ahead and say, hey, bring me the common and we'll also get a product ID.
So we're going to go ahead and say, hey, give me a product ID.
So all these information will be coming up from the body itself.
Now coming on to the model itself of the product, not here, the product model.
There we go.
Now inside this product model, we are adding the review so user we can extract then from our request
the user.
So this is an ID.
So all I have to say is remember in order to access this ID, I'll have to say request dot user dot
underscore ID.
Similarly, I can access the name as well.
No big issue and then rating and the comment is coming up already in the body.
So no big deal there.
Now let's go ahead and construct that.
So we're going to go ahead and create a simple review that is going to be pushed inside the ETA reviews.
So let's go ahead and call this one as a review.
Now, this review is going to be an object that will be pushed.
So again, we have a property of user and this user is going to look like from request dot, user,
dot and again, make sure it is underscore ID because that's how the values are stored in the database.
We just saw that inside our guy for the mongoose, not mongoose, the MongoDB.
So we got the user similarly we'll getting the name as well.
So request dot user dot name in case you want to add email up to you how you are designing your application.
We will be saying that, hey, I will be having a rating as well.
The rating we're going to wrap it around between the number because that's what we are expecting.
And I expect that whoever is sending me it says job to send me between one and a five number.
And then further down the road we'll have the comments.
So let's go ahead and say comment is going to be simply comment.
And since we are using JavaScript modern version, we can just go ahead and remove this and this will
still take care of the things.
Now the next thing is just let's go ahead and push this into the database.
Now we can actually go ahead and push it into database pretty easily, but we have to first check that
in what product?
I have to push this because this is right now just hanging.
So I need to find a product in which I can add this information.
Let's go ahead and find that particular product.
So we're going to go ahead and say, hey, product.
And since this is a database operation await and we're going to go ahead and say, hey, I want to find
a product and I'm going to use a find by ID and I'll just provide this exact same product ID that you
are sending me in the body.
So there we go.
I found I found that product.
Hopefully now we need to check whether this user has already made a review or not.
And since each of these array is injecting this user ID, only one user is allowed to send one review
only.
Let's go ahead and check that.
It's pretty easy.
We can go ahead and use a method on the error itself.
So we're going to go ahead and see
already reviewed.
So this is going to be a boolean, true and false value.
How we're going to go ahead and get that.
We're going to go ahead and load this product.
And inside this product we are going to look for the reviews, error reviews, make sure you look for
the model here.
And this is an error reviews.
Now inside these reviews, we can use all the methods that we got, so we're going to go ahead and simply
look for a find here.
Now, this fine method gets gives us a true and false value based on whether the condition is being
matched or not.
So I'm going to go ahead and access one property into this review.
So I'm going to go ahead and loop through each of these values.
So I'm going to go ahead and say, Hey, individual review.
Now this is how we are going to go ahead and work on with that.
So this is going to be a simple method that we're going to call and we're going to go ahead and say,
hey, on these each item of the review, we are having a property of DOT user and this property is going
to be equal to whatever the request is coming to me.
I'll say that, hey, there is a user coming up there as well.
So request dot user dot underscore ID.
So if this matches, that means this guy has already reviewed.
But the problem is this ID, this underscore ID is a based on object.
So this is based on kind of a value.
So I need to convert that into a string.
And this is a really important thing to a string and make sure you fire this up.
This is kind of a one on one thing inside this one.
So we'll be saying to a string, okay, this again, repeating this stuff because this can be a little
bit confusing.
I'm marking this Boolean field.
I'm saying, hey, go into the reviews array and use the property dot fine, which is a classic error
method which allows you to just run some condition on each and every object we're calling each object
as review or rev.
Now inside this ref, go ahead and check the user property.
If this user property matches with whatever the user underscore ID, I'm giving it to you.
That means it's a true.
That means this user has already b has done the review.
Now again, please don't get confused here.
This rev is actually repeating over each of the object.
This request is coming to me from the front end itself.
So again, really simple one.
Okay.
Moving further down the road now I have the access of this.
I can actually run that simple if an LS condition.
So let's go ahead and simply work on this.
If an LS if already reviewed now if he has already reviewed, then we are going to work on that.
If not, then simply we can go ahead and run the LS.
If he hasn't already reviewed, then it is the simplest thing of all to do.
We're going to go ahead and run this product.
We're going to say, Hey, we want to have an access of this reviews array.
So we're going to go ahead and say, Hey, I have this sort of views and this can have a simple push
property and go ahead and push up our already designed object, which is review.
So we are going to push that.
Now one more thing we have to worry about in this case.
So this is our reviews.
But remember, we have this number of reviews as well.
So this number of reviews needs to be updated as well.
So let's go ahead and work with that.
So once this is being done, so we're going to go ahead and say product dot.
Let me go ahead and copy this actually number of reviews.
Copy that.
Product number of reviews is going to be it is going to be calculated from product, dot reviews, dot
length.
Now obviously I know and I'm fully aware that there is one more property we need to check on, which
is the ratings.
So rating is basically an average of all the users review that they have given divided by total number
of reviews that I have.
I will take care of that in a minute.
But first, let's check out the condition that if a user hasn't already reviewed, we have pushed the
values inside this.
But if a user has already reviewed, then we might want to update the value.
So let's go ahead and update that.
So I'm going to go ahead and access again.
Same thing we're going to say product dot reviews.
A review.
So I want to look through this array.
So I'm going to go ahead and use the classic for each command.
No, it doesn't suggest me anything in the classic for each.
I'll access to each of the review and I'll.
I would like to run a simple method on that.
What I'll say I want to match that particular element inside the arrays, which is having this review.
So I'm going to go ahead and use a simple event ls condition up here.
And again, actually I can go ahead and use the same condition here.
I'm going to go ahead and copy this and I will paste the condition here instead of rev.
This time we are calling it as review.
So let's go ahead and copy this and update that.
So now we are finding the users, whatever the review is, I hope this is pretty clear.
In that case, we need to just update.
So individual element, which is review.
In this case, your comment comment is going to be updated by whatever the comment we have.
Remember, we are extracting the comment up here.
So this is being updated.
Your rating should also be updated.
So we're going to go ahead and say review DOT rating, are we calling it as rating?
We are calling it as rating.
Yep.
So rating is going to be updated as rating.
Okay.
So this is all good.
Now, we have found that particular element inside the array and we have updated that.
Now there is one more thing still remaining that no matter how you have pushed the value inside the
array, there is one more property we are calculating, which is this average ratings.
So there is an SX there.
This is an average rating.
Let's go up here and say now we need to adjust ratings.
Okay, so how we are going to adjust rating first, we need to access that.
So product DOT ratings now this is going to be updated by an average of how many reviews we have.
So let's go ahead and access that.
So let's go ahead and say, hey, product dot reviews and each of these reviews inside this array,
we can actually run a simple reduce method again, reduces a classic JavaScript method.
Let's go ahead and work on with that.
It gives you an accumulator and the item as well.
So inside this further down the road, we can actually go ahead and say, hey, on each of this item,
there is a property of rating that needs to be accumulated and we are going to start with zero again.
You can read the box a little bit more onto that, but this is how we do it.
And this whole thing is going to be divided by how many total reviews we have.
And we can find that simply by saying product, dot reviews, dot length.
There we go.
So again, really simple.
We have just loop through the array, use the reduced method to find the property and we are just calculating
the average and we are updating this rating up here.
Now only thing that's remaining is now to save this one.
So we're going to go ahead and save it.
So how we can save that again, we have done this many times.
Shouldn't be a big deal.
We're going to go ahead and say, hey, product, go ahead and save this one.
But this time, since we are manipulating a lot of properties, it is really nice to have this validate
before save flag.
So no, it's not going to suggest me validate before save.
Let's go ahead and turn off this flag so that we don't face any issues.
So this is all done and this is all good.
Now, further down the road, we are going to just send a response.
So we're going to say restart status and the status is going to be 200, obviously.
And then we're going to go ahead and say, Hey, Jason, response and we are going to go ahead and say
success is going to be true.
That's all the response we are sending.
So hopefully things are okay and very understandable as well.
Let's go ahead and catch up in the next video.