Hey there, everyone, that they.
And in this video, we're going to work through how to delete a user.
Now, in the update, in part, we have taken a kind of a side step.
We haven't provide the user or the admin the the kind of adaptation of the photo itself.
I think it should not be provided user only should be able to update their photos itself.
It's a kind of a privacy thing, but again, you can provide that to the user, no big deal.
But in the deletion part, we cannot actually skip that part because once we are deleting the user,
obviously their photos should also be deleted from the cloud itself.
So this will actually give you a little bit more detail on how you can do it.
If you can delete it, surely you can update that we have performed all of this operations, so have
a little bit confidence in yourself and I'm pretty sure you can do it.
So let's go ahead and create a root for this one, not root actually a controller first.
So let's go ahead and you can shrink down a little bit just like there, and let's go ahead and make
a copy of this one.
Okay.
Do we need a copy of something else now?
We'll write the code entirely, so no problem.
I was thinking if we have written similar kind of code in some other controller, we can actually borrow
that.
But let's go ahead and write this entirely.
Okay?
So let's just clean this up and see and figure out how we are going to delete that for deleting.
We are using the same route.
We saw that up here that we are using slash admin slash user slash ID.
So I'll be able to grab the idea of the user from there.
So let's first do that.
So request dot params dot id.
Okay, I have this user id.
I need to find a user from the database itself.
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
into a variable.
And since the database is in another continent, you get it.
Right now I have this user being stored, so I need to extract some information from the user itself.
More importantly, I have to first check whether this user exists or not, whether there is something
wrong into the params or something like that.
So if there is no user, then go ahead and work like this.
So we're going to go ahead and return this method right here.
So no need to execute further.
We are going to go ahead and say, hey, next, I would like to raise a new custom error with my constructor
that I have designed and it says No such user found, found.
And this is a401 error classic and that's it.
Okay, what about if I'm able to grab the user?
The first thing that I'll do is I would love to delete the photo because if I delete the user first,
I would have no reference of the photo.
So that's that.
Okay, so let's grab the reference of the image.
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
dot ID.
Now I can use the cloud to delete this photo based on this ID.
So let's go ahead and use the destroy method of cloud entry.
So I will say cloud deanery, cloud cloud ordinary and we are going to use the V two version of it,
dot uploader.
It has a method of destroy just what we use in the Updation case, and it takes just one parameter which
is image ID.
Now this operation is going to take some time.
You can hold that into a reference or just can use in a way that, hey, please go ahead and perform
this operation and take as much time as it takes.
And once this is done, then we are going to go ahead and remove this user.
So all we got to do is user dot and we can use the remove method.
And there are a couple of other methods available on this one.
We are going to use the remove one and again, you can go ahead and use other methods in case you want
to use that.
In the mongoose I found that remove is kind of okay, it works and this is a database operation.
So that is also going to take some time and then let's go ahead and just send the response resident
status and we're going to go ahead and say it is 200 and we're going to go ahead and say, Hey, Jason,
and we're going to go ahead and simply send a success of true.
So sounds like a job done.
Derica, let's go ahead and copy it.
Says Admin Delete one.
So let's go ahead and change this one as delete one with a DX capital that would make sense admin delete
one user details not admin delete one user.
Okay, so that is fine.
Let's go ahead and copy this and let's go ahead and move on to Roots.
And this time we are going to go ahead and say that, hey, remove this colon and we have another root
which is going to be simply come on and delete.
So this time we have this delete root and on this delete root I expect that you should be logged in
even if you are admin.
And I will also say that you should have some of the custom roles, precisely the admin.
And after that just use this controller in case a delete request comes up on.
This particular route.
And obviously I need to change this one up here.
So there we go.
Okay.
So whoever the team is in charge of designing the entire model and controller and routes for this entire
thing for the user.
This is all done.
So I'll call this one as Jordan.
And remember we have written so much of the controller just in the login and yes, things can be as
complex as it sounds.
So again, the beauty about is I was just updating my user routes and the middleware as well as the
controllers.
I never touch the GS.
My senior engineer was responsible for handling all these routes and that said, that's all I care about.
Now, obviously there are another team might be working on another controller, which is a product controller
and the product routes as well.
So obviously this next team is our self only, so we need to work on that, but surely we are going
to do that in the next section.