WEBVTT

00:03.880 --> 00:04.990
Hey there, everyone, that they.

00:05.320 --> 00:08.470
And in this video, we're going to work through how to delete a user.

00:08.590 --> 00:11.550
Now, in the update, in part, we have taken a kind of a side step.

00:11.560 --> 00:17.860
We haven't provide the user or the admin the the kind of adaptation of the photo itself.

00:17.860 --> 00:22.420
I think it should not be provided user only should be able to update their photos itself.

00:22.420 --> 00:26.800
It's a kind of a privacy thing, but again, you can provide that to the user, no big deal.

00:26.800 --> 00:31.300
But in the deletion part, we cannot actually skip that part because once we are deleting the user,

00:31.330 --> 00:35.980
obviously their photos should also be deleted from the cloud itself.

00:35.980 --> 00:39.130
So this will actually give you a little bit more detail on how you can do it.

00:39.130 --> 00:43.450
If you can delete it, surely you can update that we have performed all of this operations, so have

00:43.450 --> 00:47.040
a little bit confidence in yourself and I'm pretty sure you can do it.

00:47.050 --> 00:52.300
So let's go ahead and create a root for this one, not root actually a controller first.

00:52.420 --> 00:58.540
So let's go ahead and you can shrink down a little bit just like there, and let's go ahead and make

00:58.540 --> 00:59.740
a copy of this one.

01:00.220 --> 01:00.640
Okay.

01:00.670 --> 01:03.610
Do we need a copy of something else now?

01:03.610 --> 01:05.770
We'll write the code entirely, so no problem.

01:06.400 --> 01:10.690
I was thinking if we have written similar kind of code in some other controller, we can actually borrow

01:10.690 --> 01:11.290
that.

01:11.290 --> 01:13.450
But let's go ahead and write this entirely.

01:14.200 --> 01:14.490
Okay?

01:14.560 --> 01:19.870
So let's just clean this up and see and figure out how we are going to delete that for deleting.

01:19.870 --> 01:21.220
We are using the same route.

01:21.220 --> 01:25.120
We saw that up here that we are using slash admin slash user slash ID.

01:25.120 --> 01:27.430
So I'll be able to grab the idea of the user from there.

01:27.430 --> 01:28.330
So let's first do that.

01:28.330 --> 01:31.300
So request dot params dot id.

01:31.330 --> 01:33.340
Okay, I have this user id.

01:33.340 --> 01:36.250
I need to find a user from the database itself.

01:36.250 --> 01:43.450
So I'm going to go ahead and say user dot, find buy ID and this is going to take I'll store this one

01:43.450 --> 01:44.740
into a variable.

01:44.740 --> 01:48.430
And since the database is in another continent, you get it.

01:48.430 --> 01:54.460
Right now I have this user being stored, so I need to extract some information from the user itself.

01:54.970 --> 01:59.320
More importantly, I have to first check whether this user exists or not, whether there is something

01:59.320 --> 02:01.660
wrong into the params or something like that.

02:01.660 --> 02:05.680
So if there is no user, then go ahead and work like this.

02:05.740 --> 02:08.320
So we're going to go ahead and return this method right here.

02:08.320 --> 02:09.700
So no need to execute further.

02:09.700 --> 02:15.730
We are going to go ahead and say, hey, next, I would like to raise a new custom error with my constructor

02:15.730 --> 02:24.280
that I have designed and it says No such user found, found.

02:24.280 --> 02:28.690
And this is a401 error classic and that's it.

02:29.260 --> 02:32.260
Okay, what about if I'm able to grab the user?

02:32.560 --> 02:37.720
The first thing that I'll do is I would love to delete the photo because if I delete the user first,

02:37.720 --> 02:39.610
I would have no reference of the photo.

02:39.610 --> 02:40.960
So that's that.

02:41.140 --> 02:43.660
Okay, so let's grab the reference of the image.

02:43.660 --> 02:52.180
So I'm going to go ahead and say const image ID that I'll be able to grab from user dot photo for two

02:52.210 --> 02:53.230
dot ID.

02:53.440 --> 02:57.100
Now I can use the cloud to delete this photo based on this ID.

02:57.100 --> 02:59.730
So let's go ahead and use the destroy method of cloud entry.

02:59.800 --> 03:08.320
So I will say cloud deanery, cloud cloud ordinary and we are going to use the V two version of it,

03:08.320 --> 03:10.420
dot uploader.

03:10.420 --> 03:16.270
It has a method of destroy just what we use in the Updation case, and it takes just one parameter which

03:16.270 --> 03:17.470
is image ID.

03:17.560 --> 03:19.540
Now this operation is going to take some time.

03:19.540 --> 03:24.250
You can hold that into a reference or just can use in a way that, hey, please go ahead and perform

03:24.250 --> 03:26.830
this operation and take as much time as it takes.

03:27.100 --> 03:31.000
And once this is done, then we are going to go ahead and remove this user.

03:31.000 --> 03:34.780
So all we got to do is user dot and we can use the remove method.

03:34.780 --> 03:38.560
And there are a couple of other methods available on this one.

03:38.560 --> 03:44.620
We are going to use the remove one and again, you can go ahead and use other methods in case you want

03:44.620 --> 03:45.460
to use that.

03:46.150 --> 03:50.980
In the mongoose I found that remove is kind of okay, it works and this is a database operation.

03:50.980 --> 03:56.710
So that is also going to take some time and then let's go ahead and just send the response resident

03:56.710 --> 04:03.910
status and we're going to go ahead and say it is 200 and we're going to go ahead and say, Hey, Jason,

04:04.420 --> 04:08.320
and we're going to go ahead and simply send a success of true.

04:09.430 --> 04:11.980
So sounds like a job done.

04:12.010 --> 04:14.260
Derica, let's go ahead and copy it.

04:14.260 --> 04:16.720
Says Admin Delete one.

04:16.720 --> 04:23.080
So let's go ahead and change this one as delete one with a DX capital that would make sense admin delete

04:23.080 --> 04:27.880
one user details not admin delete one user.

04:28.840 --> 04:29.920
Okay, so that is fine.

04:29.920 --> 04:34.960
Let's go ahead and copy this and let's go ahead and move on to Roots.

04:35.110 --> 04:39.700
And this time we are going to go ahead and say that, hey, remove this colon and we have another root

04:39.700 --> 04:44.770
which is going to be simply come on and delete.

04:44.770 --> 04:50.350
So this time we have this delete root and on this delete root I expect that you should be logged in

04:50.350 --> 04:51.580
even if you are admin.

04:51.670 --> 04:57.070
And I will also say that you should have some of the custom roles, precisely the admin.

04:57.640 --> 05:01.750
And after that just use this controller in case a delete request comes up on.

05:01.840 --> 05:03.100
This particular route.

05:03.340 --> 05:06.490
And obviously I need to change this one up here.

05:06.490 --> 05:07.450
So there we go.

05:08.780 --> 05:09.030
Okay.

05:09.110 --> 05:16.400
So whoever the team is in charge of designing the entire model and controller and routes for this entire

05:16.400 --> 05:18.080
thing for the user.

05:18.200 --> 05:19.100
This is all done.

05:19.100 --> 05:20.810
So I'll call this one as Jordan.

05:20.810 --> 05:26.480
And remember we have written so much of the controller just in the login and yes, things can be as

05:26.480 --> 05:27.680
complex as it sounds.

05:27.680 --> 05:33.230
So again, the beauty about is I was just updating my user routes and the middleware as well as the

05:33.230 --> 05:34.070
controllers.

05:34.070 --> 05:35.830
I never touch the GS.

05:35.840 --> 05:40.640
My senior engineer was responsible for handling all these routes and that said, that's all I care about.

05:40.640 --> 05:45.350
Now, obviously there are another team might be working on another controller, which is a product controller

05:45.350 --> 05:47.090
and the product routes as well.

05:47.090 --> 05:52.640
So obviously this next team is our self only, so we need to work on that, but surely we are going

05:52.640 --> 05:54.200
to do that in the next section.
