WEBVTT

00:03.730 --> 00:05.290
Hey there everyone that they share.

00:05.290 --> 00:10.600
And the previous video we saw that how we can have a simple method which can help us to add a review

00:10.600 --> 00:14.260
and technically it is also acting as updating the review as well.

00:14.260 --> 00:19.630
So add an update are happening all in the same case and we don't need to give any put request or anything

00:19.630 --> 00:21.310
if somebody sends us just the post data.

00:21.310 --> 00:23.050
We are managing everything on here.

00:23.170 --> 00:27.550
Now, further down the road, I would like to have a delete method as well, so let's go ahead and have

00:27.550 --> 00:28.600
a copy of this one.

00:28.600 --> 00:37.300
So I'll just copy this and I'll say give me a paste of that and let's go ahead and shrink this one and

00:37.300 --> 00:37.870
add this one.

00:37.870 --> 00:41.470
So this one is going to be delete a review or delete review in simple.

00:41.770 --> 00:44.460
So this one is delete review.

00:44.470 --> 00:44.680
Okay.

00:44.710 --> 00:46.450
Let's see how we're going to work with that.

00:46.450 --> 00:51.550
Now again, of course, you are not going to send me this much of the information you would either be

00:51.550 --> 00:56.920
sending me just the product ID now how we're going to product, how we expect the product idea to come

00:56.920 --> 01:01.480
up to us if it comes in the requested body, that is also fine.

01:01.480 --> 01:06.610
But you are going to notice in such cases, especially for the delete, it is kind of a recommended

01:06.610 --> 01:09.580
that you come up or you just get that into the query.

01:09.820 --> 01:14.680
So we're going to go ahead and say request or query DOT product ID, okay, we have extracted that and

01:14.680 --> 01:16.720
now we have access to this product ID again.

01:16.720 --> 01:20.920
Now again, if you want to access that in the params, totally a new query totally on you.

01:20.920 --> 01:23.530
You just have to convey this information to the front end guy.

01:23.800 --> 01:28.210
We don't need to construct an object like this because that's not necessary.

01:28.210 --> 01:28.990
So there we go.

01:29.260 --> 01:35.860
Now, obviously, we need to find a product based on this product ID, however you are receiving doesn't

01:35.860 --> 01:36.730
really matter.

01:36.880 --> 01:37.240
Okay.

01:37.240 --> 01:41.590
Once I have gotten access to this one, now I need to update the reviews itself.

01:41.590 --> 01:43.510
So let's go ahead and use a simple method.

01:43.570 --> 01:47.040
I'm going to go ahead and say, hey, I will construct this reviews area.

01:47.050 --> 01:52.300
The goal or what I'm trying to do here is I have this access to this reviews entire array.

01:52.300 --> 01:56.680
I want to use a filter method and filter out the review which is being sent by that user.

01:56.680 --> 01:57.910
That's all I'm trying to do.

01:58.030 --> 01:59.770
So I'll be using the same method here.

01:59.770 --> 02:04.150
So I'll be saying that, hey, reviews, let's go ahead and update that just like this.

02:04.270 --> 02:11.500
And let's go ahead and say, hey, product dot reviews and let's use a filter method on that filter.

02:11.530 --> 02:12.220
There we go.

02:12.520 --> 02:17.980
Now filter actually goes and gives you again the same option that let's go ahead and call this one as

02:17.980 --> 02:21.940
simple rev this time and this is going to work exactly same.

02:21.940 --> 02:25.630
So we can actually go ahead and use this exact same line just like that.

02:25.930 --> 02:30.370
Now Rev works, this filter actually works a little bit different than the other methods.

02:30.550 --> 02:35.890
It actually allows to pass on all the values, but wherever the value or the condition matches out,

02:35.890 --> 02:38.140
it doesn't allow you to pass that thing.

02:38.140 --> 02:44.080
So the object we are looping through all the values, the object on which the review matches.

02:44.080 --> 02:45.670
That means this is the review by the user.

02:45.670 --> 02:48.490
It will not allow it to pass that.

02:48.490 --> 02:55.090
So now we have a reviews, a new fresh error, which is not having the values which we are supposed

02:55.090 --> 02:55.600
to delete.

02:55.600 --> 02:58.060
So that's exactly what we want to save in the database.

02:58.390 --> 03:02.290
We don't want this entirety of the stuff and F and LS and all of that.

03:02.290 --> 03:03.790
So let's go ahead and remove that part.

03:03.790 --> 03:04.810
We don't need that.

03:05.080 --> 03:05.520
Okay.

03:06.070 --> 03:07.600
Now I have this reviews.

03:07.600 --> 03:11.830
I obviously have to adjust these ratings and stuff, which I just deleted.

03:11.830 --> 03:15.730
And the only part that got deleted in that is this number of reviews.

03:15.730 --> 03:17.500
So let's copy that and update that.

03:17.500 --> 03:23.530
So let's go ahead and say, hey, now we have this number of reviews and that will be calculated from

03:23.530 --> 03:28.900
this fresh reviews that we got after filtering and we can actually go ahead and calculator length on

03:28.900 --> 03:31.960
that, if I can write that properly.

03:31.960 --> 03:32.740
There we go.

03:33.100 --> 03:33.350
Okay.

03:33.370 --> 03:39.550
So this is all updated number of reviews and we got this rating as well.

03:39.550 --> 03:41.590
So rating is accumulation fine.

03:41.590 --> 03:43.120
So this is again recalculated.

03:43.120 --> 03:44.770
That is absolutely fine.

03:44.800 --> 03:46.420
I'm happy with that part.

03:46.450 --> 03:48.970
Now all I need to do is insert a product dot save.

03:48.970 --> 03:54.730
I have to update the data because remember we are having a lot of reviews and all those a number of

03:54.730 --> 03:55.900
ratings and all these data.

03:55.900 --> 03:57.940
So let's go ahead and work on with that.

03:58.180 --> 04:00.280
So I'll remove this part.

04:00.760 --> 04:01.420
Let's go ahead.

04:01.420 --> 04:06.340
And instead of save, it would be better to say that update the product.

04:08.740 --> 04:09.820
Product.

04:09.820 --> 04:10.480
There we go.

04:10.900 --> 04:11.160
Okay.

04:11.170 --> 04:15.370
So how we're going to you update that again, this is a database operation and we're going to go ahead

04:15.370 --> 04:18.490
and use simple product model itself.

04:18.820 --> 04:23.830
And it has a property of find by ID and update.

04:23.830 --> 04:24.550
There we go.

04:24.910 --> 04:29.110
Now, first, we need to provide the ID itself and we are holding that ID in available.

04:29.120 --> 04:31.210
So there we go, product ID.

04:31.210 --> 04:33.100
Let's go ahead and copy that.

04:34.850 --> 04:35.600
Copy that.

04:35.600 --> 04:37.250
And I'll give you this product ID.

04:37.280 --> 04:37.910
There we go.

04:37.940 --> 04:42.290
Now, I need to provide you the data that you should be updating, and that data is actually inside

04:42.290 --> 04:43.610
an object itself.

04:43.640 --> 04:46.550
So what are the data that you should be updating first?

04:46.550 --> 04:51.860
You should be updating the reviews, so update that, then you should be updating the ratings because

04:51.860 --> 04:54.860
I have recalculated that ratings.

04:55.160 --> 04:57.350
Ratings and what else?

04:57.380 --> 04:58.640
A number of reviews also.

04:58.640 --> 04:59.990
So go ahead and update that.

04:59.990 --> 05:02.780
So these are the three things that needs to be updated there.

05:03.740 --> 05:05.960
So this is all happy.

05:06.380 --> 05:09.500
Now, also, we need to pass on the previous options as well.

05:09.500 --> 05:12.570
So this is all there and there we go.

05:12.590 --> 05:14.330
Now let's provide the options as well.

05:14.330 --> 05:16.690
So do we want the new value to be returned?

05:16.700 --> 05:17.960
Yes, definitely.

05:18.260 --> 05:20.600
We want run validators as well.

05:20.690 --> 05:22.370
Run validators.

05:22.790 --> 05:23.270
Yep.

05:23.270 --> 05:24.620
I want to do that.

05:25.640 --> 05:30.920
And also optionally again, use, find and modify.

05:32.290 --> 05:32.790
There we go.

05:32.800 --> 05:34.780
I want to turn off this flag as well.

05:35.050 --> 05:36.070
Let's save this.

05:36.070 --> 05:37.630
So we are having product by it.

05:37.660 --> 05:43.090
We are providing what product to update, what with, what data to update and optional, some of the

05:43.090 --> 05:44.200
options going in.

05:44.260 --> 05:47.770
Now, again, the response is pretty obvious and pretty good as well.

05:47.990 --> 05:48.230
Okay.

05:48.250 --> 05:52.510
So this is the basic method of how we are actually getting that and these are the only things we need

05:52.510 --> 05:53.230
to worry about.

05:53.230 --> 05:56.200
And basically we need to give us a method itself.

05:56.230 --> 06:00.700
Now, optionally, we are going to design one more method because here is the situation for that.

06:00.790 --> 06:06.280
Although whenever we are sending an individual product response, we are sending all the data through

06:06.280 --> 06:10.950
it, we are sending up what all the reviews are, what all the ratings and everything is.

06:10.960 --> 06:15.070
And technically, we shouldn't be sending the repetitive or iterative data when somebody in the front

06:15.070 --> 06:16.150
end can grab it, actually.

06:16.150 --> 06:20.650
But maybe, maybe for some reason in the front end, Guy has asked that, Hey, can you give me a simple

06:20.650 --> 06:24.610
method in which if I provide you product ID, you just return me all the reviews.

06:24.610 --> 06:27.610
It would be easier to display all the reviews in a separate component.

06:27.610 --> 06:30.580
So we are going to design a method for him just for that.

06:30.580 --> 06:35.980
And this is just to explain the stuff that yes, sometimes user or front end guy may ask you that I

06:35.980 --> 06:37.840
don't want the entirety of the information.

06:37.840 --> 06:40.120
Just give me some selected data on some of the route.

06:40.120 --> 06:42.580
And yes, we can always go ahead and do that.

06:42.670 --> 06:48.040
So we're going to say and say that get only reviews.

06:50.340 --> 06:53.100
For one product.

06:53.100 --> 06:56.520
So yeah, this is a really long one, but this is how we are going to do it.

06:56.700 --> 07:01.320
We're going to say that this is going to be wrapping around Big Promise and we're going to say request

07:01.320 --> 07:04.590
a response and the next just like that.

07:04.590 --> 07:05.520
There we go.

07:06.600 --> 07:09.450
And seems like there is a little bit of the issue.

07:09.630 --> 07:13.110
Yep, there is a little bit of issue too fast.

07:13.110 --> 07:13.980
There we go.

07:14.370 --> 07:18.180
And I need to wrap everything around the big promise.

07:18.510 --> 07:19.020
There we go.

07:19.020 --> 07:23.580
That's why I usually copy and paste this stuff and this needs to be going a sync.

07:25.900 --> 07:33.130
And after the async, this needs to be wrapped around like that and then I can fire my callback.

07:33.850 --> 07:38.770
OC looks good now, so we're going to go ahead and say that, hey, you need to send me somehow the

07:38.770 --> 07:41.080
ID of that product, of that one product.

07:41.080 --> 07:44.440
So let's just say we're going to hold that into a product.

07:44.920 --> 07:49.180
And how that is going to come up is up to you and how you talk to the front end guy.

07:49.210 --> 07:51.100
Really, we don't really matter that much.

07:51.100 --> 07:56.680
We have mastered that art already, so we're going to say, fine, buy ID and go ahead and just change

07:56.680 --> 07:57.670
it in case things changes.

07:57.670 --> 08:00.550
I'm going to say that, hey, let's go ahead and grab it into the query.

08:00.550 --> 08:05.560
So request dot, query, dot ID or you can call this one as product ID however you like to go ahead

08:05.560 --> 08:06.370
and use that.

08:06.640 --> 08:09.400
Now we have this product, we are holding that.

08:09.400 --> 08:12.160
So let's go ahead and send just only the reviews there.

08:12.160 --> 08:13.090
So it's really simple.

08:13.090 --> 08:20.620
We are going to say Rez dot status and we are going to go ahead and say 200 and let's send it JSON response.

08:20.620 --> 08:21.370
There we go.

08:21.370 --> 08:25.420
In the JSON response, we are going to say success is true in this case.

08:25.570 --> 08:33.760
And the reviews, which is an error itself, is going to come up from the product DOT reviews.

08:33.760 --> 08:34.420
There we go.

08:34.450 --> 08:40.420
Make sure it is a TSS reviews and that's all what we got to do now in case somebody in the frontend

08:40.420 --> 08:45.340
guy comes up and say, Hey, I need a specific model, I'm designing a special component in which just

08:45.340 --> 08:46.750
the brand names are coming in.

08:46.750 --> 08:50.890
So hey, can you send me just specifically the brand name or just the category up there?

08:50.890 --> 08:52.510
You can go ahead and design those method.

08:52.510 --> 08:54.760
And yes, these requests are very, very common.

08:54.760 --> 08:56.290
They come up quite a lot.

08:56.320 --> 08:57.100
In the next video.

08:57.100 --> 09:01.810
We're going to go ahead and design the routes for that because we haven't yet implemented them in the

09:01.810 --> 09:02.080
route.

09:02.080 --> 09:03.910
Let's go ahead and catch up in the next video.
