In this video you'll learn how to set up an HDTV end point for deleting a resource.
We will eventually need to one for deleting users and one for deleting tasks.
We'll focus on deleting users together and deleting tasks will be your challenge.
So far we have four different routes for working with our users down below.
Right after the fourth one we're going to add a another express method to call right here.
EXPRESS provides us with a delete method allowing us to set up an H TTP end point that uses the delete
H TTP method.
So far we've used post to create get to read.
We've used a patch to update.
And here we used delete to delete.
Now you are all for deleting is going to require their I.D. similar to what we did for reading an individual
user or updating one.
So forward slash users forward slash colon I.D..
And this is all the input we need.
There isn't going to be anything on the request body that we need.
Just having the I.D. is enough to delete the user right here.
We'll set up our async function with our 2 parameters we have the request and the response objects both
of which we'll be using down below.
Now actually deleting a resource isn't too bad.
Typically the less input we're taking in the less complexity we have for a given root handler.
In this case the only thing we're accepting is the I.D. so there's not a lot of work to do.
I will be kicking things off though with a try catch block.
Let's go ahead and start there.
Once we have this in place we can focus on actually attempting to delete the user.
Now as we saw in the Mongoose documentation there are a few different ways we can get that done.
I'll head back over to Google Chrome.
Right here we have delete many we have delete one.
I also have a find by I.D. and delete and find one and delete.
We're gonna go ahead and use find by I.D. and delete right over here inside of index dot J.
Yes let's go ahead and knock that out.
We are going to create a contest for user.
This is going to store the user that we end up deleting if any and we're going to await the promise
that comes back from user dot find by I.D. and delete.
Perfect.
Now once we have that method call in place the only thing we need to provide is the first argument the
I.D. of the user to remove request dot parameter dot I.D. and we're done now down below.
Let's go ahead and see if there was actually a user to delete if there was no user.
We'll go ahead and send a 4 0 4 back right here.
Return response dot status for 0 4.
Then we'll go ahead and use send it to send that back.
Next up if there was a user that got deleted we'll stick with the default 200 status code and we'll
just send the user data back.
So response dot send user if someone's deleting a user we want to send the deleted user as the response
body.
Next up if something went wrong we'll go ahead and send back a 500 response dot status 500 using send
to send that back.
And this is honestly it.
Now that we have this in place we can go ahead and test things out.
Actually deleting users by their I.D. I'm gonna head over to postmen to get this done now.
As always we will need to set up a new request in task app so I'll start there.
I'll call this one delete user and once we have it in place we're gonna go ahead and grab the I.D. of
a user to remove.
Now I have that read user's request.
We've worked with before.
Here I have three.
I'll go ahead and delete the second one with the name Andrew so I'll copy that I.D. to the clipboard
head over to the delete user request and start to manipulate it.
The first thing I'll do is change the method from get over to delete and then from there we'll set the
U.R.L. that's gonna be local host on port three thousand forward slash users forward slash D user I.D..
Now there's no additional information required.
We don't need any parameters any headers or any body data.
This is enough to get things to work.
I'm gonna go ahead and save the request and fire it off now.
When I do what do I get I get my two hundred status code which is fantastic and down below I can see
the information for the user.
I'm deleting and this is the exact one I saw just a few moments ago with the name of Andrew the other
users had either Andrew Meade or Jess.
In my case now we fired this off once and it worked.
If I fire it off again it should fail because there's no longer a user with this I.D. so I'll take the
exact same request I'll click send one more time.
And right here we're getting a 4 0 4 which is correct it should not have found a user with this I.D.
since it was removed with the previous request we had made.
So there we go.
You now know how to delete users by their I.D..
And now it's gonna be your challenge to do the same thing for tasks down below.
We have our four end points for working with tasks and we need a fifth one that's going to go right
here.
Let's go ahead and start with the challenge comments right here.
Your goal is to allow for the removal of tasks.
So you want to set up the end point handler then attempt to delete the task by its I.D..
And once again we have those three potential outcomes.
Success task not found or error.
And we want to send a response back for all of those last up.
Test your work so create the new request in post and attempt a few different requests to make sure everything
is working as expected.
All right.
Pause the video.
Knock that out and when you're done come back and click play.
How'd that go.
Let's go ahead and kick things off by setting up the end point handler.
Right here.
I'll be using app dot delete once again.
Now in this case we're trying to delete a task by its I.D. So forward slash tasks forward slash I.D..
Excuse me.
Forward slash colon I.D..
Then we'll set up the callback function.
It's an async function with request and response and we'll set up our try catch block to make sure that
everything works as expected.
Now once we have that block in place the first thing we want to do is attempt to delete the task by
its I.D..
So right here I will create a task variable where we'll store the deleted task if any.
And I will await a call to task dot find by I.D. and delete like we used before.
And in this case request dot parameter dot I.D. contains the idea of the task we're trying to remove.
Now let's go ahead and set up the three different ways we'll end up responding.
We have the 4 0 4 response.
If there was no task to delete right here response dot status for 0 4 then dot send sending back nothing
down below that if things went well we'll actually have the task to send back response dot send task
last up if an error did occur we'll send back that 500 response dot status setting it equal to five
hundred and then using send to send things back to the requester now that we have this in place we're
all done and we can actually test things out to make sure it's working as expected.
I'm gonna go ahead and remove the challenge comments save the file or the final time in this one and
add a new request to the task app collection right here at the request.
I'll call this one delete task and once we have the name in place we're gonna go through the process
of setting it up if I click on it we have to switch the actual H TTP method over to delete then we can
focus on setting up that you are out right here for me that's gonna be local host colon three thousand
forward slash tasks forward slash some task I.D. Now if you don't have one which I don't we can always
use the reed at tasks operation to get back an I.D. of all of the tasks we have access to.
I'll go ahead and fire that off right here I have to I'll go ahead and delete this one learn the Mongoose
library I'll head back over to the delete operation and paste that in you are Belfield now once again
there's no need to provide any additional information I'm gonna go ahead and save this one send it off
and right here I can see I got it two hundred and I have the deleted task showing up if I fire it off
again I would expect a 4 0 4 and that is exactly what I'm getting.
So there we go.
We now have end points for managing day for CRUD operations for our resources we can create them read
them update them and delete them.
Allowing someone using this API to manage the data as they would need to.
That's where we're going to stop for this video.
There is just one video left for this section where you're going to learn how to refactor your application
so you don't have every single route defined in these same file.
I'm excited to get to that.
Let's jump right in.