1 00:00:00,270 --> 00:00:05,250 In this video you'll learn how to set up an HDTV end point for deleting a resource. 2 00:00:05,340 --> 00:00:10,530 We will eventually need to one for deleting users and one for deleting tasks. 3 00:00:10,620 --> 00:00:15,530 We'll focus on deleting users together and deleting tasks will be your challenge. 4 00:00:15,540 --> 00:00:20,870 So far we have four different routes for working with our users down below. 5 00:00:20,970 --> 00:00:27,210 Right after the fourth one we're going to add a another express method to call right here. 6 00:00:27,300 --> 00:00:35,010 EXPRESS provides us with a delete method allowing us to set up an H TTP end point that uses the delete 7 00:00:35,330 --> 00:00:36,990 H TTP method. 8 00:00:36,990 --> 00:00:41,610 So far we've used post to create get to read. 9 00:00:41,670 --> 00:00:43,680 We've used a patch to update. 10 00:00:43,680 --> 00:00:46,470 And here we used delete to delete. 11 00:00:46,470 --> 00:00:52,920 Now you are all for deleting is going to require their I.D. similar to what we did for reading an individual 12 00:00:52,920 --> 00:00:54,840 user or updating one. 13 00:00:54,840 --> 00:00:59,160 So forward slash users forward slash colon I.D.. 14 00:00:59,160 --> 00:01:01,260 And this is all the input we need. 15 00:01:01,260 --> 00:01:05,070 There isn't going to be anything on the request body that we need. 16 00:01:05,070 --> 00:01:09,260 Just having the I.D. is enough to delete the user right here. 17 00:01:09,390 --> 00:01:16,020 We'll set up our async function with our 2 parameters we have the request and the response objects both 18 00:01:16,020 --> 00:01:18,240 of which we'll be using down below. 19 00:01:18,360 --> 00:01:21,270 Now actually deleting a resource isn't too bad. 20 00:01:21,300 --> 00:01:28,210 Typically the less input we're taking in the less complexity we have for a given root handler. 21 00:01:28,290 --> 00:01:33,270 In this case the only thing we're accepting is the I.D. so there's not a lot of work to do. 22 00:01:33,300 --> 00:01:36,900 I will be kicking things off though with a try catch block. 23 00:01:36,960 --> 00:01:38,990 Let's go ahead and start there. 24 00:01:39,090 --> 00:01:44,480 Once we have this in place we can focus on actually attempting to delete the user. 25 00:01:44,670 --> 00:01:49,980 Now as we saw in the Mongoose documentation there are a few different ways we can get that done. 26 00:01:50,490 --> 00:01:52,740 I'll head back over to Google Chrome. 27 00:01:52,740 --> 00:01:56,250 Right here we have delete many we have delete one. 28 00:01:56,340 --> 00:02:01,570 I also have a find by I.D. and delete and find one and delete. 29 00:02:01,570 --> 00:02:08,010 We're gonna go ahead and use find by I.D. and delete right over here inside of index dot J. 30 00:02:08,010 --> 00:02:10,290 Yes let's go ahead and knock that out. 31 00:02:10,290 --> 00:02:13,170 We are going to create a contest for user. 32 00:02:13,170 --> 00:02:18,990 This is going to store the user that we end up deleting if any and we're going to await the promise 33 00:02:19,020 --> 00:02:25,290 that comes back from user dot find by I.D. and delete. 34 00:02:25,290 --> 00:02:26,220 Perfect. 35 00:02:26,280 --> 00:02:31,830 Now once we have that method call in place the only thing we need to provide is the first argument the 36 00:02:31,830 --> 00:02:39,840 I.D. of the user to remove request dot parameter dot I.D. and we're done now down below. 37 00:02:39,870 --> 00:02:45,730 Let's go ahead and see if there was actually a user to delete if there was no user. 38 00:02:45,750 --> 00:02:48,980 We'll go ahead and send a 4 0 4 back right here. 39 00:02:49,170 --> 00:02:53,290 Return response dot status for 0 4. 40 00:02:53,310 --> 00:02:56,580 Then we'll go ahead and use send it to send that back. 41 00:02:56,610 --> 00:03:02,790 Next up if there was a user that got deleted we'll stick with the default 200 status code and we'll 42 00:03:02,790 --> 00:03:04,500 just send the user data back. 43 00:03:04,560 --> 00:03:12,060 So response dot send user if someone's deleting a user we want to send the deleted user as the response 44 00:03:12,120 --> 00:03:13,180 body. 45 00:03:13,260 --> 00:03:21,060 Next up if something went wrong we'll go ahead and send back a 500 response dot status 500 using send 46 00:03:21,060 --> 00:03:22,540 to send that back. 47 00:03:22,590 --> 00:03:24,270 And this is honestly it. 48 00:03:24,870 --> 00:03:28,490 Now that we have this in place we can go ahead and test things out. 49 00:03:28,560 --> 00:03:34,580 Actually deleting users by their I.D. I'm gonna head over to postmen to get this done now. 50 00:03:34,590 --> 00:03:40,730 As always we will need to set up a new request in task app so I'll start there. 51 00:03:40,740 --> 00:03:47,820 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 52 00:03:47,820 --> 00:03:49,500 a user to remove. 53 00:03:49,500 --> 00:03:51,480 Now I have that read user's request. 54 00:03:51,480 --> 00:03:53,120 We've worked with before. 55 00:03:53,160 --> 00:03:54,530 Here I have three. 56 00:03:54,600 --> 00:04:00,810 I'll go ahead and delete the second one with the name Andrew so I'll copy that I.D. to the clipboard 57 00:04:01,200 --> 00:04:04,800 head over to the delete user request and start to manipulate it. 58 00:04:04,800 --> 00:04:11,340 The first thing I'll do is change the method from get over to delete and then from there we'll set the 59 00:04:11,340 --> 00:04:20,530 U.R.L. that's gonna be local host on port three thousand forward slash users forward slash D user I.D.. 60 00:04:20,530 --> 00:04:23,160 Now there's no additional information required. 61 00:04:23,220 --> 00:04:27,240 We don't need any parameters any headers or any body data. 62 00:04:27,240 --> 00:04:29,720 This is enough to get things to work. 63 00:04:29,730 --> 00:04:33,750 I'm gonna go ahead and save the request and fire it off now. 64 00:04:33,780 --> 00:04:39,870 When I do what do I get I get my two hundred status code which is fantastic and down below I can see 65 00:04:39,870 --> 00:04:41,600 the information for the user. 66 00:04:41,610 --> 00:04:48,330 I'm deleting and this is the exact one I saw just a few moments ago with the name of Andrew the other 67 00:04:48,330 --> 00:04:51,140 users had either Andrew Meade or Jess. 68 00:04:51,180 --> 00:04:55,080 In my case now we fired this off once and it worked. 69 00:04:55,080 --> 00:05:01,600 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 70 00:05:01,600 --> 00:05:05,190 exact same request I'll click send one more time. 71 00:05:05,290 --> 00:05:11,470 And right here we're getting a 4 0 4 which is correct it should not have found a user with this I.D. 72 00:05:11,500 --> 00:05:15,740 since it was removed with the previous request we had made. 73 00:05:15,850 --> 00:05:16,920 So there we go. 74 00:05:16,960 --> 00:05:20,170 You now know how to delete users by their I.D.. 75 00:05:20,320 --> 00:05:25,320 And now it's gonna be your challenge to do the same thing for tasks down below. 76 00:05:25,360 --> 00:05:31,150 We have our four end points for working with tasks and we need a fifth one that's going to go right 77 00:05:31,150 --> 00:05:31,770 here. 78 00:05:31,780 --> 00:05:35,110 Let's go ahead and start with the challenge comments right here. 79 00:05:35,110 --> 00:05:38,510 Your goal is to allow for the removal of tasks. 80 00:05:38,530 --> 00:05:43,850 So you want to set up the end point handler then attempt to delete the task by its I.D.. 81 00:05:44,080 --> 00:05:46,870 And once again we have those three potential outcomes. 82 00:05:46,870 --> 00:05:50,160 Success task not found or error. 83 00:05:50,170 --> 00:05:53,920 And we want to send a response back for all of those last up. 84 00:05:53,920 --> 00:06:00,970 Test your work so create the new request in post and attempt a few different requests to make sure everything 85 00:06:00,970 --> 00:06:02,800 is working as expected. 86 00:06:02,800 --> 00:06:03,360 All right. 87 00:06:03,370 --> 00:06:04,240 Pause the video. 88 00:06:04,240 --> 00:06:07,720 Knock that out and when you're done come back and click play. 89 00:06:11,660 --> 00:06:12,500 How'd that go. 90 00:06:12,500 --> 00:06:16,320 Let's go ahead and kick things off by setting up the end point handler. 91 00:06:16,340 --> 00:06:17,390 Right here. 92 00:06:17,390 --> 00:06:20,340 I'll be using app dot delete once again. 93 00:06:20,420 --> 00:06:27,980 Now in this case we're trying to delete a task by its I.D. So forward slash tasks forward slash I.D.. 94 00:06:28,310 --> 00:06:28,880 Excuse me. 95 00:06:28,880 --> 00:06:31,010 Forward slash colon I.D.. 96 00:06:31,250 --> 00:06:33,380 Then we'll set up the callback function. 97 00:06:33,380 --> 00:06:40,460 It's an async function with request and response and we'll set up our try catch block to make sure that 98 00:06:40,460 --> 00:06:43,090 everything works as expected. 99 00:06:43,100 --> 00:06:48,560 Now once we have that block in place the first thing we want to do is attempt to delete the task by 100 00:06:48,560 --> 00:06:49,490 its I.D.. 101 00:06:49,490 --> 00:06:55,790 So right here I will create a task variable where we'll store the deleted task if any. 102 00:06:55,790 --> 00:07:04,870 And I will await a call to task dot find by I.D. and delete like we used before. 103 00:07:04,880 --> 00:07:12,330 And in this case request dot parameter dot I.D. contains the idea of the task we're trying to remove. 104 00:07:12,350 --> 00:07:16,850 Now let's go ahead and set up the three different ways we'll end up responding. 105 00:07:16,880 --> 00:07:18,950 We have the 4 0 4 response. 106 00:07:18,950 --> 00:07:28,460 If there was no task to delete right here response dot status for 0 4 then dot send sending back nothing 107 00:07:28,970 --> 00:07:36,680 down below that if things went well we'll actually have the task to send back response dot send task 108 00:07:37,190 --> 00:07:44,050 last up if an error did occur we'll send back that 500 response dot status setting it equal to five 109 00:07:44,050 --> 00:07:50,840 hundred and then using send to send things back to the requester now that we have this in place we're 110 00:07:50,870 --> 00:07:55,830 all done and we can actually test things out to make sure it's working as expected. 111 00:07:56,030 --> 00:08:03,110 I'm gonna go ahead and remove the challenge comments save the file or the final time in this one and 112 00:08:03,110 --> 00:08:09,410 add a new request to the task app collection right here at the request. 113 00:08:09,410 --> 00:08:15,920 I'll call this one delete task and once we have the name in place we're gonna go through the process 114 00:08:15,920 --> 00:08:24,020 of setting it up if I click on it we have to switch the actual H TTP method over to delete then we can 115 00:08:24,020 --> 00:08:32,110 focus on setting up that you are out right here for me that's gonna be local host colon three thousand 116 00:08:32,540 --> 00:08:39,950 forward slash tasks forward slash some task I.D. Now if you don't have one which I don't we can always 117 00:08:39,950 --> 00:08:46,640 use the reed at tasks operation to get back an I.D. of all of the tasks we have access to. 118 00:08:47,090 --> 00:08:54,700 I'll go ahead and fire that off right here I have to I'll go ahead and delete this one learn the Mongoose 119 00:08:54,790 --> 00:09:01,690 library I'll head back over to the delete operation and paste that in you are Belfield now once again 120 00:09:01,720 --> 00:09:08,260 there's no need to provide any additional information I'm gonna go ahead and save this one send it off 121 00:09:08,290 --> 00:09:15,370 and right here I can see I got it two hundred and I have the deleted task showing up if I fire it off 122 00:09:15,430 --> 00:09:20,470 again I would expect a 4 0 4 and that is exactly what I'm getting. 123 00:09:20,530 --> 00:09:21,700 So there we go. 124 00:09:21,700 --> 00:09:29,050 We now have end points for managing day for CRUD operations for our resources we can create them read 125 00:09:29,050 --> 00:09:31,630 them update them and delete them. 126 00:09:31,840 --> 00:09:37,080 Allowing someone using this API to manage the data as they would need to. 127 00:09:37,090 --> 00:09:39,370 That's where we're going to stop for this video. 128 00:09:39,370 --> 00:09:45,220 There is just one video left for this section where you're going to learn how to refactor your application 129 00:09:45,430 --> 00:09:49,680 so you don't have every single route defined in these same file. 130 00:09:49,690 --> 00:09:51,010 I'm excited to get to that. 131 00:09:51,040 --> 00:09:52,240 Let's jump right in.