WEBVTT

00:03.670 --> 00:05.020
Hated everyone they shared.

00:05.020 --> 00:07.960
And in this video, we are going to create a couple of admin routes.

00:07.960 --> 00:11.830
In this precise video, we're going to create just one, which is update one product.

00:11.830 --> 00:14.980
Now, obviously, admin will be allowed to update the product itself.

00:15.190 --> 00:16.930
So let's go ahead and work on with that.

00:16.960 --> 00:22.120
Now the only thing that you have to take care is if admin sends the photo as an update part.

00:22.120 --> 00:27.280
Now if admin sends the photos as the updation part, then you have to delete the existing photos and

00:27.280 --> 00:31.540
have to upload the photos and send them into the database entry wherever you are storing that.

00:31.540 --> 00:32.950
And that's that's pretty much it.

00:32.980 --> 00:37.240
Just all the JSON updates shouldn't bother you much because we have studied so much on that part.

00:37.240 --> 00:40.480
So let's go ahead and rather actually go ahead and copy this.

00:40.480 --> 00:45.220
So let's go ahead and work with that to remove this e and we have this.

00:45.220 --> 00:48.940
Let's minimize this and let's open this one.

00:51.060 --> 00:56.040
Now we are keeping all the admin products all together, but again, not really necessary.

00:56.040 --> 01:00.090
So we're going to say admin update one product.

01:01.230 --> 01:03.330
So how we're going to go ahead and do this one.

01:03.330 --> 01:08.070
So first is we need to find a product and we'll take care of the response later on.

01:08.070 --> 01:10.200
First, let's go ahead and work on with this one.

01:10.200 --> 01:11.940
So this is not going to be products.

01:11.940 --> 01:15.330
It's going to be a product because only one product is getting updated.

01:15.360 --> 01:17.370
How user will be sending me the response?

01:17.370 --> 01:18.750
Simply not the response.

01:18.750 --> 01:19.980
The idea of the product.

01:20.220 --> 01:24.510
Usually it's in the params, but if it has a query or something you know how to handle that shouldn't

01:24.510 --> 01:25.230
be a big deal.

01:25.350 --> 01:27.450
So request dot params dot id.

01:27.480 --> 01:32.160
Now hopefully this has given me all the details that I want, but again, this can actually create an

01:32.160 --> 01:32.460
issue.

01:32.460 --> 01:37.320
So I'm going to go ahead and open this one, copy this line again, saving some time, because we have

01:37.320 --> 01:39.330
already studied this thing quite in depth.

01:39.900 --> 01:40.680
So there we go.

01:40.680 --> 01:45.240
Now, if I don't find a product, I will go ahead and simply create an issue that, hey, I couldn't

01:45.240 --> 01:46.830
find the product with this ID.

01:47.070 --> 01:52.500
Now moving on, I will first look that if we are receiving any files or not, so we're going to go ahead

01:52.500 --> 01:56.550
and check with the if that if the request has files or not.

01:56.550 --> 01:59.760
Now if the request has file, then we have to do a couple of things.

01:59.760 --> 02:11.160
First, destroy the existing images and then upload and save the images.

02:11.160 --> 02:14.220
So these are the two tasks in front of us that we have to do.

02:14.520 --> 02:17.130
Okay, so how we're going to destroy the images?

02:17.130 --> 02:22.590
I can access the existing images by this product because this product has product photos and I can actually

02:22.590 --> 02:27.270
find the length of the area so that how many times I have to run the loop and then simply use the cloud

02:27.480 --> 02:28.560
to destroy the images.

02:28.560 --> 02:30.960
So let's go ahead and see that how we can work on with that.

02:30.960 --> 02:34.920
So let's go ahead and simply run a simple for loop for doing that.

02:34.920 --> 02:37.350
I'm going to go ahead and take help of these four.

02:37.380 --> 02:38.130
There we go.

02:38.250 --> 02:39.360
Index is fine.

02:39.360 --> 02:45.060
The index is going to run till the length of of photos, not photos, products.

02:46.850 --> 02:52.190
Product, not products, product dot photos, dot length.

02:52.310 --> 02:53.630
Now this is all good.

02:53.630 --> 02:58.700
So this will keep on running the loop till we have the products, the length of the photos itself.

02:58.730 --> 03:00.410
Now, let's go ahead and remove this one.

03:00.410 --> 03:02.450
Now we want to destroy the photo here.

03:02.450 --> 03:06.950
We can destroy the photo by accessing the product photos dot ID.

03:06.980 --> 03:08.180
Let's go ahead and do that.

03:08.180 --> 03:11.960
So how we actually delete that, we have to first access the cloud net.

03:11.960 --> 03:17.060
E So let's go ahead and say cloud entry and we will get some result as well.

03:17.060 --> 03:23.150
So we're going to go ahead and say const rest a result for this one since we won't be using it.

03:23.150 --> 03:27.860
But still, let's go ahead and hold that maybe later on you want to get some information from that or

03:27.860 --> 03:30.650
something or you can just use the cloud native feature of that.

03:30.770 --> 03:35.120
We're going to go ahead and say V two dot uploader, dot destroy.

03:35.750 --> 03:36.320
There we go.

03:36.320 --> 03:40.160
Now this destroy obviously takes a parameter that how you're going to destroy the images.

03:40.160 --> 03:41.420
Give me that ID.

03:41.450 --> 03:48.890
I can give that ID by simply saying, hey, product, we are having a dot photos, dot photos and since

03:48.890 --> 03:55.160
we are looping through that inside the photos itself, let's go ahead and have this square bracket and

03:55.160 --> 03:56.990
this will be governed by index.

03:56.990 --> 04:03.080
Now, each of that photo's first element, second element and third element will having a dot ID.

04:03.110 --> 04:03.950
So there we go.

04:03.950 --> 04:08.720
So hopefully now this will be able to access the ID and we'll be able to delete the photos.

04:09.350 --> 04:13.550
Once this is all done, this loop is all done, we can actually get a response or something.

04:13.550 --> 04:16.370
Now we need to upload and save the new photos that we got.

04:16.370 --> 04:18.260
Since the requested file is there.

04:18.260 --> 04:20.330
We can actually access all of that up here.

04:21.170 --> 04:24.770
So first let's go ahead and call this one as images.

04:24.770 --> 04:29.420
Edit In fact, we can design this images at the very top so that we can access everywhere.

04:29.420 --> 04:32.990
So we're going to go ahead and say images array just like the previous one.

04:32.990 --> 04:34.280
We are going to do exactly same.

04:34.280 --> 04:36.950
We are going to push all the results into this images area.

04:37.100 --> 04:38.750
So we don't need to create it up here.

04:38.780 --> 04:40.400
Let's go ahead and work on with that.

04:40.430 --> 04:44.180
Again, we are going to need a loop because there are a lot of photos that are coming in.

04:44.180 --> 04:44.930
So let's go ahead.

04:44.930 --> 04:49.730
In fact, what we can do is instead of doing like this, we can actually copy this because we have already

04:49.730 --> 04:50.210
done that.

04:50.210 --> 04:51.500
This is exactly the same.

04:51.710 --> 05:00.140
So we are having this images array and we are having this request files actually from here request files.

05:00.140 --> 05:03.830
We are uploading that into the photos and they will go and everything is coming in.

05:03.830 --> 05:07.430
So let's go ahead and copy this and get back up here.

05:09.000 --> 05:14.160
And inside this one, we can actually go ahead and paste everything.

05:14.430 --> 05:18.120
Now we are already checking this request file, so we don't need to check this up here.

05:18.120 --> 05:21.870
So I'm going to go ahead and remove this one and the last parenthesis.

05:21.870 --> 05:23.730
There we go and save that.

05:24.300 --> 05:28.080
And we are calling this as image array, image array.

05:28.080 --> 05:29.400
So this should be same.

05:29.400 --> 05:35.040
Let's go ahead and copy this and let's throw that all the result into images array this time.

05:35.040 --> 05:36.900
So again, not really a big deal.

05:36.930 --> 05:40.860
Let's quickly analyze what is the code that we are brought in because it's always good to have this

05:40.860 --> 05:41.310
one.

05:41.310 --> 05:46.230
So we are saying that, hey, if the requested files exist, let's go ahead and run a simple loop.

05:46.230 --> 05:52.170
This will be saying requested file, photos, length, it will run the loop and we are getting all the

05:52.170 --> 05:52.920
files.

05:53.220 --> 05:58.080
So we are saying, hey, hold the result into cloud ready and cloud uploader will upload the photos

05:58.080 --> 06:04.080
from request file dot photos index of it and then we are going to say dot temp file path and we'll be

06:04.080 --> 06:04.920
uploading that product.

06:04.950 --> 06:06.690
Now here I would like to put up a comment.

06:06.720 --> 06:08.340
There's this folder name.

06:08.430 --> 06:10.080
This folder name can be trouble.

06:10.080 --> 06:17.340
So folder name can actually go into dot E and B that would make sense in this case or a longer run of

06:17.340 --> 06:22.050
the product itself, because this folder, even if you change it like products to product, then it's

06:22.050 --> 06:22.890
going to create an issue.

06:22.890 --> 06:25.680
So this should really come from the E and B file.

06:25.830 --> 06:29.490
And then we are pushing this all the values into this one and that is all good.

06:29.490 --> 06:31.590
So now we have access to this image is Eddy.

06:31.650 --> 06:32.730
So that's fantastic.

06:32.730 --> 06:36.960
Now all we got to do is update this stuff here.

06:36.960 --> 06:38.430
So let let's go up here.

06:38.550 --> 06:41.160
I do have access of this images area at the top.

06:41.160 --> 06:42.750
I'm going to go ahead and minimize this.

06:42.840 --> 06:45.180
So now I have this access of images area.

06:45.180 --> 06:47.220
All I have to do is add that to the body itself.

06:47.220 --> 06:53.160
So I'm going to say request, dot, body, dot, whatever the photos you are bringing to me is going

06:53.160 --> 06:56.250
to be updated by this image is array.

06:57.380 --> 06:57.830
Images.

06:57.830 --> 07:01.760
Edit So this images array is now properly designed the way we want it to be.

07:01.760 --> 07:05.570
ETA Which to object object with two properties ID and the security URL.

07:05.570 --> 07:06.560
So that is all good.

07:06.560 --> 07:07.610
That is all nice.

07:08.690 --> 07:12.590
Now what we need to do further down the road now the image is parted, handled.

07:12.620 --> 07:15.080
All we need to do is update the information.

07:15.080 --> 07:21.700
So we're going to go ahead and use this product and we are going to go ahead and say, hey, await and

07:21.740 --> 07:29.840
we are going to use the same model product dot find by ID and update.

07:30.050 --> 07:34.790
So let's go ahead and see how we're going to give that request dot params dot ID.

07:34.790 --> 07:39.620
So this is the ID of the one and now we can say request dot body.

07:40.370 --> 07:43.850
This is the entirety of the body that you have sent it to me to update.

07:43.850 --> 07:48.560
So what are the values you are giving me to update, name, price, stock, whatever you want to update,

07:48.560 --> 07:52.580
you can just go ahead and send me up there and further down the road I have to provide you a couple

07:52.580 --> 07:53.210
of objects.

07:53.210 --> 07:55.070
So first new is true.

07:55.100 --> 08:01.730
That will make sure that I get the response as whatever the new values are, not the old values.

08:01.940 --> 08:07.550
And we are going to go ahead and say, Hey, I want to run the validators as well run validators so

08:07.550 --> 08:10.610
that everything is being taken care by my models itself.

08:10.640 --> 08:18.380
Now, optionally, we would like to go ahead and use, use, find and modify all that.

08:18.380 --> 08:23.450
We don't need to do it now, but we would still like to turn the flag on now by default in the modern

08:23.450 --> 08:27.830
mongoose, this is already off, but still just to make sure that it is not being used.

08:28.070 --> 08:29.150
Now this is all good.

08:29.150 --> 08:33.710
Now all we got to do is send the success through and let's also send this product that we have recently

08:33.710 --> 08:34.370
designed.

08:35.150 --> 08:40.430
So hopefully this is all good and should be working absolutely fine.

08:41.060 --> 08:46.280
Okay, so now we can actually minimize this one and hopefully things are okay and we will still test

08:46.280 --> 08:47.720
it out and we'll check this out.

08:47.750 --> 08:49.970
All we need is a parameters ID.

08:50.000 --> 08:53.390
Let's go ahead and see that how this update one product is going to work on.

08:53.390 --> 08:54.350
So copy that.

08:54.380 --> 08:59.390
Go into the model itself and let's get this one here which is admin update one product.

08:59.390 --> 09:02.330
So this is going and becoming a part of the admin itself.

09:03.080 --> 09:08.630
So we can actually go ahead and copy this route, copy that and paste it up here.

09:09.900 --> 09:11.550
Move that a little bit.

09:12.270 --> 09:13.080
There we go.

09:14.130 --> 09:15.060
Should be all good.

09:15.090 --> 09:20.330
Now, what we can do is we can define that slash admin, slash product and slash.

09:20.340 --> 09:22.920
Then we can go ahead and provide an ID here.

09:23.010 --> 09:25.860
And he is going to be logged in custom role as admin.

09:25.860 --> 09:29.930
Let's go ahead and grab this one and paste it up here.

09:29.940 --> 09:35.340
Now we have a route on which we can update the product by providing slash admin, slash product, slash

09:35.340 --> 09:37.890
ID and this event will take care of that.

09:37.890 --> 09:39.990
So hopefully things are actually making sense.

09:39.990 --> 09:44.640
The most important thing is not to just write code but actually understand what is going to happen,

09:44.640 --> 09:47.910
how I'm going to handle that, how am I going to provide the response for that?

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