WEBVTT

00:03.910 --> 00:05.320
Hey, did everyone this year.

00:05.320 --> 00:08.120
And now let's worry about the reviews itself.

00:08.140 --> 00:09.990
Now, reviews are really a simple thing.

00:10.000 --> 00:13.310
It's just like adding a separate page for updating the stock just.

00:13.330 --> 00:14.050
Just like that.

00:14.050 --> 00:16.270
So in this case, we haven't put down any reviews.

00:16.270 --> 00:18.370
Now we're going to do that now reviews.

00:18.370 --> 00:20.650
Just trying to imagine that you'll be having a website.

00:20.650 --> 00:22.390
There will be a lot of cards on that.

00:22.390 --> 00:25.450
So you won't be allowing user to add the reviews just from that page.

00:25.450 --> 00:30.550
You'll be allowing user to add reviews from when somebody clicks on that card and visits the individual

00:30.550 --> 00:34.210
product itself, then we'll allow there or probably somewhere else.

00:34.210 --> 00:37.990
So this is usually the ideal situation where all the details of the product are there.

00:37.990 --> 00:40.270
Just below there the reviews are going to be there.

00:40.450 --> 00:42.670
So if I can go ahead and click on that.

00:42.670 --> 00:47.140
Definitely in that page, I do have an axis of that idea of the product.

00:47.140 --> 00:49.180
Now, how you're going to send the idea of the product.

00:49.180 --> 00:49.900
It's up to you.

00:49.900 --> 00:51.100
You are designing the application.

00:51.100 --> 00:55.210
You can request that in the URL, you can request that in a query.

00:55.210 --> 00:57.760
You can request that in the body however you like to go.

00:57.940 --> 01:02.110
Now, in case you are going to request that in the root itself, then obviously the root is going to

01:02.110 --> 01:03.340
become something like this.

01:03.460 --> 01:07.420
Let me just show you that as a brief example, probably just here.

01:07.570 --> 01:11.740
So if you are requesting that here, so probably something going to look like this, that this is going

01:11.740 --> 01:16.060
to be slash product, then we need to have the product ID itself.

01:16.060 --> 01:24.040
So let's just call this one as simply product product ID and then we are going to have a slash and then

01:24.040 --> 01:25.660
probably more information that you have.

01:25.660 --> 01:31.020
So the point here is that I want to say is you can request the product ID in the URL and the body in

01:31.030 --> 01:32.470
itself and the query as well.

01:32.470 --> 01:33.580
So it depends on you.

01:33.610 --> 01:37.360
Now I will be requesting the this product ID in the body itself.

01:37.360 --> 01:38.170
Again, no big deal.

01:38.170 --> 01:40.600
User can actually go ahead and pass on me this one.

01:40.960 --> 01:42.850
I'll actually copy this one up here.

01:42.850 --> 01:45.040
So let's go ahead and make a copy of this one.

01:45.340 --> 01:49.360
I'll say let's close this one and expand this one and move up here.

01:49.480 --> 01:52.180
So this one is going to be simply add a review.

01:52.180 --> 01:55.780
So let's go ahead and simply say add review.

01:55.780 --> 01:56.350
That's it.

01:56.350 --> 01:57.250
That's pretty much it.

01:57.280 --> 01:59.320
Now let's go ahead and work on with that.

01:59.410 --> 02:02.650
A couple of information that I would like to jog your memory on this one.

02:02.650 --> 02:04.600
So first is the middleware.

02:04.600 --> 02:09.640
So if I go ahead and open this middleware, if you remember that once we are having this request of

02:09.670 --> 02:16.120
user, remember we are finding we are using fine by ID and based on the ID, we are finding a user.

02:16.600 --> 02:19.300
And most important thing is this user is a big object.

02:19.330 --> 02:24.760
Now entirety of the information that is stored in my database about the user, his name, email, his

02:24.760 --> 02:25.810
ID, everything is there.

02:25.810 --> 02:29.650
So just to jog it one more time, if I go ahead and check out the users.

02:29.650 --> 02:34.600
So all this information including is underscore ID has a dot name, dot email, password.

02:35.050 --> 02:40.690
Everything is available to me in the request dot user and that's all the information I need so I can

02:40.690 --> 02:41.980
go ahead and add that view.

02:42.640 --> 02:48.730
Just one last thing before we add that we need to also verify and check that whether this user has already

02:48.730 --> 02:53.230
done a review in the past or not, if he has already added a review in the past, we just want to update

02:53.230 --> 02:54.730
that review and that's pretty much it.

02:54.730 --> 02:56.530
That's all that's all what we're going to do.

02:56.710 --> 02:57.220
Okay.

02:57.880 --> 03:00.970
So first, let's extract all the information from the body.

03:00.970 --> 03:04.060
So all the information will be coming up in request or body.

03:04.060 --> 03:05.500
Let's go ahead and extract that.

03:05.500 --> 03:09.730
So we're going to go ahead and say, hey, I need some information that will be coming up from there.

03:09.940 --> 03:14.050
And precisely the one thing that I'm looking for is the rating that's going to be coming in.

03:14.050 --> 03:16.300
The rating is going to be between one and five.

03:16.300 --> 03:21.280
My front end guy is responsible for restricting that between one and five.

03:21.310 --> 03:23.680
We will be requesting a comment as well.

03:23.680 --> 03:27.670
So we're going to go ahead and say, hey, bring me the common and we'll also get a product ID.

03:27.670 --> 03:30.190
So we're going to go ahead and say, hey, give me a product ID.

03:30.220 --> 03:33.430
So all these information will be coming up from the body itself.

03:33.430 --> 03:39.880
Now coming on to the model itself of the product, not here, the product model.

03:39.880 --> 03:40.600
There we go.

03:40.630 --> 03:45.960
Now inside this product model, we are adding the review so user we can extract then from our request

03:46.080 --> 03:46.870
the user.

03:46.870 --> 03:48.070
So this is an ID.

03:48.070 --> 03:53.260
So all I have to say is remember in order to access this ID, I'll have to say request dot user dot

03:53.260 --> 03:54.280
underscore ID.

03:54.310 --> 03:56.290
Similarly, I can access the name as well.

03:56.290 --> 04:00.820
No big issue and then rating and the comment is coming up already in the body.

04:00.820 --> 04:01.930
So no big deal there.

04:02.590 --> 04:04.630
Now let's go ahead and construct that.

04:04.630 --> 04:10.390
So we're going to go ahead and create a simple review that is going to be pushed inside the ETA reviews.

04:10.390 --> 04:12.460
So let's go ahead and call this one as a review.

04:12.700 --> 04:15.130
Now, this review is going to be an object that will be pushed.

04:15.130 --> 04:21.790
So again, we have a property of user and this user is going to look like from request dot, user,

04:21.790 --> 04:26.470
dot and again, make sure it is underscore ID because that's how the values are stored in the database.

04:26.470 --> 04:32.650
We just saw that inside our guy for the mongoose, not mongoose, the MongoDB.

04:33.460 --> 04:36.310
So we got the user similarly we'll getting the name as well.

04:36.310 --> 04:42.580
So request dot user dot name in case you want to add email up to you how you are designing your application.

04:42.790 --> 04:45.820
We will be saying that, hey, I will be having a rating as well.

04:45.820 --> 04:49.960
The rating we're going to wrap it around between the number because that's what we are expecting.

04:49.960 --> 04:56.530
And I expect that whoever is sending me it says job to send me between one and a five number.

04:56.710 --> 04:59.230
And then further down the road we'll have the comments.

04:59.230 --> 05:01.840
So let's go ahead and say comment is going to be simply comment.

05:02.000 --> 05:06.440
And since we are using JavaScript modern version, we can just go ahead and remove this and this will

05:06.440 --> 05:08.030
still take care of the things.

05:08.300 --> 05:12.460
Now the next thing is just let's go ahead and push this into the database.

05:12.470 --> 05:17.900
Now we can actually go ahead and push it into database pretty easily, but we have to first check that

05:17.900 --> 05:18.800
in what product?

05:18.800 --> 05:21.500
I have to push this because this is right now just hanging.

05:21.500 --> 05:25.010
So I need to find a product in which I can add this information.

05:25.010 --> 05:27.410
Let's go ahead and find that particular product.

05:27.410 --> 05:29.810
So we're going to go ahead and say, hey, product.

05:30.890 --> 05:35.540
And since this is a database operation await and we're going to go ahead and say, hey, I want to find

05:35.540 --> 05:42.980
a product and I'm going to use a find by ID and I'll just provide this exact same product ID that you

05:42.980 --> 05:44.450
are sending me in the body.

05:44.570 --> 05:45.410
So there we go.

05:45.410 --> 05:47.300
I found I found that product.

05:47.300 --> 05:54.560
Hopefully now we need to check whether this user has already made a review or not.

05:54.560 --> 06:01.070
And since each of these array is injecting this user ID, only one user is allowed to send one review

06:01.070 --> 06:01.490
only.

06:01.490 --> 06:02.570
Let's go ahead and check that.

06:02.570 --> 06:03.230
It's pretty easy.

06:03.230 --> 06:05.900
We can go ahead and use a method on the error itself.

06:06.140 --> 06:08.060
So we're going to go ahead and see

06:10.280 --> 06:13.070
already reviewed.

06:13.070 --> 06:15.440
So this is going to be a boolean, true and false value.

06:15.470 --> 06:17.120
How we're going to go ahead and get that.

06:17.240 --> 06:19.460
We're going to go ahead and load this product.

06:19.460 --> 06:25.700
And inside this product we are going to look for the reviews, error reviews, make sure you look for

06:25.700 --> 06:26.600
the model here.

06:26.720 --> 06:29.330
And this is an error reviews.

06:30.310 --> 06:34.690
Now inside these reviews, we can use all the methods that we got, so we're going to go ahead and simply

06:34.690 --> 06:36.070
look for a find here.

06:36.100 --> 06:40.900
Now, this fine method gets gives us a true and false value based on whether the condition is being

06:40.900 --> 06:41.800
matched or not.

06:41.830 --> 06:44.800
So I'm going to go ahead and access one property into this review.

06:44.800 --> 06:47.860
So I'm going to go ahead and loop through each of these values.

06:47.860 --> 06:50.980
So I'm going to go ahead and say, Hey, individual review.

06:51.400 --> 06:54.460
Now this is how we are going to go ahead and work on with that.

06:54.580 --> 06:59.000
So this is going to be a simple method that we're going to call and we're going to go ahead and say,

06:59.000 --> 07:08.170
hey, on these each item of the review, we are having a property of DOT user and this property is going

07:08.170 --> 07:12.970
to be equal to whatever the request is coming to me.

07:13.210 --> 07:16.210
I'll say that, hey, there is a user coming up there as well.

07:16.210 --> 07:21.160
So request dot user dot underscore ID.

07:21.460 --> 07:24.760
So if this matches, that means this guy has already reviewed.

07:24.760 --> 07:29.620
But the problem is this ID, this underscore ID is a based on object.

07:29.620 --> 07:32.200
So this is based on kind of a value.

07:32.200 --> 07:34.330
So I need to convert that into a string.

07:34.330 --> 07:38.350
And this is a really important thing to a string and make sure you fire this up.

07:38.590 --> 07:41.260
This is kind of a one on one thing inside this one.

07:41.260 --> 07:46.000
So we'll be saying to a string, okay, this again, repeating this stuff because this can be a little

07:46.000 --> 07:46.900
bit confusing.

07:46.900 --> 07:49.210
I'm marking this Boolean field.

07:49.210 --> 07:54.570
I'm saying, hey, go into the reviews array and use the property dot fine, which is a classic error

07:54.580 --> 08:00.190
method which allows you to just run some condition on each and every object we're calling each object

08:00.190 --> 08:01.980
as review or rev.

08:01.990 --> 08:05.200
Now inside this ref, go ahead and check the user property.

08:05.200 --> 08:10.930
If this user property matches with whatever the user underscore ID, I'm giving it to you.

08:10.930 --> 08:11.860
That means it's a true.

08:11.860 --> 08:15.700
That means this user has already b has done the review.

08:16.960 --> 08:19.040
Now again, please don't get confused here.

08:19.060 --> 08:22.930
This rev is actually repeating over each of the object.

08:22.930 --> 08:27.220
This request is coming to me from the front end itself.

08:27.220 --> 08:29.080
So again, really simple one.

08:29.210 --> 08:29.520
Okay.

08:29.530 --> 08:32.590
Moving further down the road now I have the access of this.

08:32.590 --> 08:35.160
I can actually run that simple if an LS condition.

08:35.170 --> 08:38.290
So let's go ahead and simply work on this.

08:38.290 --> 08:43.450
If an LS if already reviewed now if he has already reviewed, then we are going to work on that.

08:43.450 --> 08:46.450
If not, then simply we can go ahead and run the LS.

08:46.450 --> 08:50.190
If he hasn't already reviewed, then it is the simplest thing of all to do.

08:50.200 --> 08:52.210
We're going to go ahead and run this product.

08:52.210 --> 08:56.080
We're going to say, Hey, we want to have an access of this reviews array.

08:56.080 --> 09:00.790
So we're going to go ahead and say, Hey, I have this sort of views and this can have a simple push

09:00.790 --> 09:05.170
property and go ahead and push up our already designed object, which is review.

09:05.320 --> 09:06.940
So we are going to push that.

09:06.940 --> 09:09.610
Now one more thing we have to worry about in this case.

09:09.610 --> 09:10.690
So this is our reviews.

09:10.690 --> 09:13.360
But remember, we have this number of reviews as well.

09:13.480 --> 09:16.120
So this number of reviews needs to be updated as well.

09:16.120 --> 09:17.830
So let's go ahead and work with that.

09:17.830 --> 09:23.500
So once this is being done, so we're going to go ahead and say product dot.

09:24.310 --> 09:28.360
Let me go ahead and copy this actually number of reviews.

09:28.360 --> 09:29.200
Copy that.

09:30.950 --> 09:38.600
Product number of reviews is going to be it is going to be calculated from product, dot reviews, dot

09:38.600 --> 09:39.380
length.

09:39.620 --> 09:44.720
Now obviously I know and I'm fully aware that there is one more property we need to check on, which

09:44.720 --> 09:45.590
is the ratings.

09:45.590 --> 09:51.260
So rating is basically an average of all the users review that they have given divided by total number

09:51.260 --> 09:52.440
of reviews that I have.

09:52.460 --> 09:54.650
I will take care of that in a minute.

09:54.650 --> 10:00.560
But first, let's check out the condition that if a user hasn't already reviewed, we have pushed the

10:00.560 --> 10:01.580
values inside this.

10:01.580 --> 10:05.680
But if a user has already reviewed, then we might want to update the value.

10:05.690 --> 10:07.190
So let's go ahead and update that.

10:07.190 --> 10:08.990
So I'm going to go ahead and access again.

10:08.990 --> 10:12.770
Same thing we're going to say product dot reviews.

10:15.110 --> 10:15.590
A review.

10:15.590 --> 10:16.990
So I want to look through this array.

10:17.000 --> 10:21.020
So I'm going to go ahead and use the classic for each command.

10:21.050 --> 10:25.050
No, it doesn't suggest me anything in the classic for each.

10:25.070 --> 10:27.830
I'll access to each of the review and I'll.

10:27.830 --> 10:30.310
I would like to run a simple method on that.

10:30.320 --> 10:37.670
What I'll say I want to match that particular element inside the arrays, which is having this review.

10:37.670 --> 10:41.090
So I'm going to go ahead and use a simple event ls condition up here.

10:41.090 --> 10:44.630
And again, actually I can go ahead and use the same condition here.

10:45.020 --> 10:51.200
I'm going to go ahead and copy this and I will paste the condition here instead of rev.

10:51.200 --> 10:52.640
This time we are calling it as review.

10:52.640 --> 10:54.860
So let's go ahead and copy this and update that.

10:55.130 --> 11:00.410
So now we are finding the users, whatever the review is, I hope this is pretty clear.

11:00.470 --> 11:02.240
In that case, we need to just update.

11:02.240 --> 11:04.340
So individual element, which is review.

11:04.340 --> 11:11.590
In this case, your comment comment is going to be updated by whatever the comment we have.

11:11.600 --> 11:13.850
Remember, we are extracting the comment up here.

11:14.540 --> 11:15.920
So this is being updated.

11:15.920 --> 11:17.390
Your rating should also be updated.

11:17.390 --> 11:22.190
So we're going to go ahead and say review DOT rating, are we calling it as rating?

11:23.270 --> 11:24.920
We are calling it as rating.

11:24.920 --> 11:25.370
Yep.

11:26.180 --> 11:28.850
So rating is going to be updated as rating.

11:29.300 --> 11:29.480
Okay.

11:29.630 --> 11:30.670
So this is all good.

11:30.680 --> 11:35.000
Now, we have found that particular element inside the array and we have updated that.

11:35.060 --> 11:39.440
Now there is one more thing still remaining that no matter how you have pushed the value inside the

11:39.440 --> 11:44.480
array, there is one more property we are calculating, which is this average ratings.

11:44.480 --> 11:45.710
So there is an SX there.

11:45.710 --> 11:47.270
This is an average rating.

11:47.390 --> 11:54.260
Let's go up here and say now we need to adjust ratings.

11:55.580 --> 11:58.760
Okay, so how we are going to adjust rating first, we need to access that.

11:58.760 --> 12:06.550
So product DOT ratings now this is going to be updated by an average of how many reviews we have.

12:06.560 --> 12:08.120
So let's go ahead and access that.

12:08.120 --> 12:17.080
So let's go ahead and say, hey, product dot reviews and each of these reviews inside this array,

12:17.090 --> 12:21.350
we can actually run a simple reduce method again, reduces a classic JavaScript method.

12:21.350 --> 12:22.790
Let's go ahead and work on with that.

12:22.790 --> 12:26.150
It gives you an accumulator and the item as well.

12:26.210 --> 12:31.670
So inside this further down the road, we can actually go ahead and say, hey, on each of this item,

12:31.670 --> 12:39.140
there is a property of rating that needs to be accumulated and we are going to start with zero again.

12:39.140 --> 12:42.650
You can read the box a little bit more onto that, but this is how we do it.

12:42.650 --> 12:46.790
And this whole thing is going to be divided by how many total reviews we have.

12:46.790 --> 12:53.990
And we can find that simply by saying product, dot reviews, dot length.

12:54.200 --> 12:54.860
There we go.

12:55.010 --> 12:56.120
So again, really simple.

12:56.120 --> 13:00.620
We have just loop through the array, use the reduced method to find the property and we are just calculating

13:00.620 --> 13:03.920
the average and we are updating this rating up here.

13:04.040 --> 13:06.740
Now only thing that's remaining is now to save this one.

13:06.740 --> 13:08.450
So we're going to go ahead and save it.

13:08.450 --> 13:11.000
So how we can save that again, we have done this many times.

13:11.000 --> 13:12.020
Shouldn't be a big deal.

13:12.350 --> 13:15.710
We're going to go ahead and say, hey, product, go ahead and save this one.

13:15.710 --> 13:22.340
But this time, since we are manipulating a lot of properties, it is really nice to have this validate

13:22.340 --> 13:24.350
before save flag.

13:24.470 --> 13:28.550
So no, it's not going to suggest me validate before save.

13:28.550 --> 13:32.000
Let's go ahead and turn off this flag so that we don't face any issues.

13:32.960 --> 13:35.060
So this is all done and this is all good.

13:35.060 --> 13:37.620
Now, further down the road, we are going to just send a response.

13:37.620 --> 13:42.860
So we're going to say restart status and the status is going to be 200, obviously.

13:43.010 --> 13:47.330
And then we're going to go ahead and say, Hey, Jason, response and we are going to go ahead and say

13:47.330 --> 13:49.040
success is going to be true.

13:49.040 --> 13:50.810
That's all the response we are sending.

13:51.080 --> 13:55.760
So hopefully things are okay and very understandable as well.

13:55.760 --> 13:57.830
Let's go ahead and catch up in the next video.
