1 00:00:00,180 --> 00:00:06,830 As promised in this video it's going to be up to you to set up an HDTV endpoint for updating a task 2 00:00:06,840 --> 00:00:11,430 by its I.D. similar to what we've done here for users. 3 00:00:11,430 --> 00:00:16,410 That's going to come down below and they'll start us off with some challenge comments describing what 4 00:00:16,410 --> 00:00:17,900 I'd like you to do. 5 00:00:17,910 --> 00:00:21,220 So the goal is to allow for task updates. 6 00:00:21,240 --> 00:00:27,180 We want people to be able to change the description of a task as what they need to do changes. 7 00:00:27,180 --> 00:00:34,110 And obviously we want them to be able to change that completed value to mark a task as either complete 8 00:00:34,170 --> 00:00:35,700 or incomplete. 9 00:00:35,730 --> 00:00:38,790 So step one is to set up the root handler. 10 00:00:38,790 --> 00:00:44,340 Once you have that structure in place you're going to see if there are any unknown updates like we did 11 00:00:44,370 --> 00:00:45,180 up above. 12 00:00:45,180 --> 00:00:51,900 For example if they're trying to update email for a task we should probably let them know that that's 13 00:00:51,900 --> 00:00:57,930 not something they're going to be able to change since the task model doesn't track an email. 14 00:00:57,930 --> 00:01:04,560 Next up you want to attempt to update the task using find by I.D. and update you want to handle the 15 00:01:04,590 --> 00:01:10,740 case where no task is found with that I.D. You want to make sure to handle the validation errors and 16 00:01:10,740 --> 00:01:13,650 finally handle success cases. 17 00:01:13,650 --> 00:01:15,430 Last up test your work. 18 00:01:15,480 --> 00:01:21,750 Fire off the given a request a few different times with both valid and invalid data and make sure you 19 00:01:21,750 --> 00:01:24,570 get the response back that you are expecting. 20 00:01:24,600 --> 00:01:27,360 You can go ahead and knock that or out right here. 21 00:01:27,420 --> 00:01:32,910 Take some time to get that done pause the video and when you're done come back and click play 22 00:01:36,830 --> 00:01:37,580 how that go. 23 00:01:37,610 --> 00:01:40,520 Let's kick things off by setting up the route itself. 24 00:01:40,520 --> 00:01:46,370 We'll be using app dot patch once again and then you are real structure will be the same as what we 25 00:01:46,370 --> 00:01:49,730 had up above for reading an individual task. 26 00:01:49,730 --> 00:01:53,270 We want to update the task by its I.D.. 27 00:01:53,270 --> 00:02:00,920 So for me that would be forward slash tasks forward slash colon I.D. then we set up that async function 28 00:02:01,160 --> 00:02:07,520 where we get access to the request and response parameters down below we can focus on what we actually 29 00:02:07,520 --> 00:02:08,780 want to do. 30 00:02:08,840 --> 00:02:13,660 Now the first thing we're going to do is make sure that we're not allowing those unknown updates. 31 00:02:13,700 --> 00:02:21,380 So like we did up above with users we want to make sure to add similar code to validate which updates 32 00:02:21,380 --> 00:02:23,380 are being performed down below. 33 00:02:23,390 --> 00:02:25,370 Let's go ahead and knock that out. 34 00:02:25,370 --> 00:02:27,810 First up I need to know what's being updated. 35 00:02:27,830 --> 00:02:32,910 I'll create the updates array for that and using object keys. 36 00:02:32,990 --> 00:02:38,910 I will convert request dot body from an object to an array of properties. 37 00:02:38,990 --> 00:02:42,260 Next up which properties are you allowed to update. 38 00:02:42,260 --> 00:02:48,490 We could always pull open the task model if we've forgotten we have description and we have completed. 39 00:02:48,710 --> 00:02:56,000 So back over in index dot J S I'll create a new constant I'll call this allowed updates I'll set it 40 00:02:56,000 --> 00:02:59,010 equal to an array of strings. 41 00:02:59,060 --> 00:03:00,190 The first being. 42 00:03:00,190 --> 00:03:01,370 Description. 43 00:03:01,370 --> 00:03:03,710 And the second being completed. 44 00:03:03,710 --> 00:03:04,640 Excellent. 45 00:03:04,640 --> 00:03:10,690 Now as we add more properties or remove them from any given model we can adjust the code here as well. 46 00:03:10,730 --> 00:03:14,690 The next thing we'll go ahead and do is figure out if this is valid or not. 47 00:03:14,690 --> 00:03:17,780 So const is valid operation. 48 00:03:18,170 --> 00:03:25,190 We'll go ahead and once again use updates dot every to figure that out for each individual update is 49 00:03:25,190 --> 00:03:27,350 it included in the array. 50 00:03:27,350 --> 00:03:36,430 So right here we will use allowed updates dot includes and we'll check if the individual update is listed 51 00:03:36,430 --> 00:03:37,870 inside of there. 52 00:03:37,870 --> 00:03:42,460 The last thing we need to do a four step number two is send back the error if it exists. 53 00:03:42,520 --> 00:03:50,340 So right here if it is not a valid operation what do we want to do. 54 00:03:50,370 --> 00:03:52,800 We want to go ahead and send back that 400. 55 00:03:52,830 --> 00:03:54,190 So I'll return. 56 00:03:54,360 --> 00:03:59,160 I'll be using response dot sadness to set the 400 status code. 57 00:03:59,160 --> 00:04:05,430 And like we did for users we can always send an error message back right here sending back an error 58 00:04:05,460 --> 00:04:09,360 property with the text of invalid updates. 59 00:04:09,360 --> 00:04:10,590 Excellent. 60 00:04:10,590 --> 00:04:16,770 Now once we have that in place we can move on to Step number three and actually attempt to update things. 61 00:04:16,860 --> 00:04:19,390 I will use try catch for that. 62 00:04:19,500 --> 00:04:25,320 Making sure that we handle those errors correctly while still being able to take advantage of async 63 00:04:25,380 --> 00:04:26,200 await. 64 00:04:26,340 --> 00:04:33,390 Now right here what we need to do is create a variable for the task so const task equals we'll be using 65 00:04:33,440 --> 00:04:33,860 a wait. 66 00:04:33,870 --> 00:04:41,760 Once again this time though with task dot find by I.D. and update as opposed to user dot find by I.D. 67 00:04:41,790 --> 00:04:48,510 And update now once we have that call in place we have to provide those three arguments we had before. 68 00:04:48,510 --> 00:04:55,500 The first is the idea of the thing we're trying to update for Quest dot parameter dot I.D. contains 69 00:04:55,500 --> 00:04:57,140 that value for us. 70 00:04:57,270 --> 00:05:00,530 It comes from the U R L parameter right here. 71 00:05:00,630 --> 00:05:05,340 Next up the updates we're trying to apply that is request dot body. 72 00:05:05,340 --> 00:05:13,260 And finally those two options we had talked about setting new equal to true and setting run validators 73 00:05:13,290 --> 00:05:15,690 equal to true as well. 74 00:05:15,690 --> 00:05:22,410 Now once we have that call in place we can go ahead and see if we actually found a task to update if 75 00:05:23,250 --> 00:05:24,700 there is no task. 76 00:05:24,720 --> 00:05:27,840 We're gonna go ahead and send back a 4 0 4. 77 00:05:27,840 --> 00:05:33,030 So right here we will use return to stop the function execution. 78 00:05:33,030 --> 00:05:38,780 I'll use response dot status to set up the forum for and send sending nothing back. 79 00:05:38,790 --> 00:05:41,010 But the status response. 80 00:05:41,010 --> 00:05:48,260 Next up if things went well we'll just send the task back response dot send task last up. 81 00:05:48,270 --> 00:05:53,220 If there was some sort of validation error down below we want to make sure to send something back for 82 00:05:53,220 --> 00:05:57,090 that as well right here response dot status. 83 00:05:57,090 --> 00:06:00,890 Setting up a four hundred and sending back the error. 84 00:06:00,900 --> 00:06:02,040 Excellent. 85 00:06:02,040 --> 00:06:06,410 Now that we have this in place we're gonna go ahead and actually test things out. 86 00:06:06,450 --> 00:06:13,350 So we've handled those three cases that we listed under attempt to update the task and the final step 87 00:06:13,350 --> 00:06:15,190 is to test our work. 88 00:06:15,330 --> 00:06:20,820 So I'll remove the challenge comments and bring the code we just wrote back into view. 89 00:06:20,820 --> 00:06:26,450 I'm going to make sure to save the script before heading over to postmen now over in postman. 90 00:06:26,450 --> 00:06:30,200 We will set up the new request and then run it a few different ways. 91 00:06:30,360 --> 00:06:33,530 Over here we have our task app collection. 92 00:06:33,570 --> 00:06:41,670 I'll go ahead and add a new request to that and I'll call this update task now as always the first thing 93 00:06:41,670 --> 00:06:48,630 we need to do is click on that and update the method and the U.R.L. in this case we want to switch over 94 00:06:48,630 --> 00:06:55,280 to patch once again which is the method that we set up to support right here and then back over and 95 00:06:55,350 --> 00:06:55,860 man. 96 00:06:55,860 --> 00:07:02,160 We can go ahead and configure the you are out now to update a task we do need the I.D. of a task that's 97 00:07:02,160 --> 00:07:03,980 actually in our database. 98 00:07:04,010 --> 00:07:10,440 I can always run read tasks to get one right here I'll pull all of the tasks from the database and I'll 99 00:07:10,440 --> 00:07:12,900 just pick the I.D. for one of them. 100 00:07:12,900 --> 00:07:18,900 I'll go ahead and grab this one right here and we'll try to change it from false over to true. 101 00:07:18,900 --> 00:07:23,560 I'll head back over to that request I created and set you are out. 102 00:07:23,560 --> 00:07:28,370 That'll be local host on port three thousand forward slash users. 103 00:07:28,410 --> 00:07:35,780 Excuse me the forward slash tasks forward slash the I.D. that we just copied from the other request. 104 00:07:35,790 --> 00:07:37,920 We also need to provide our updates. 105 00:07:37,920 --> 00:07:45,120 So over here we do have our body will switch over to raw and then we'll switch from text to Jason once 106 00:07:45,120 --> 00:07:48,570 again and we'll go ahead and change that completed value. 107 00:07:48,600 --> 00:07:54,710 So right here I will be setting completed equal to the Boolean true. 108 00:07:54,750 --> 00:07:59,940 Now when I fire this off I would expect a two hundred response which is exactly what I'm getting and 109 00:07:59,940 --> 00:08:03,300 down below I can see the task has changed. 110 00:08:03,300 --> 00:08:08,640 I went from learn the amount goose library having the completed value of false to true. 111 00:08:08,730 --> 00:08:12,210 And now we can go ahead and update other things if we wanted to. 112 00:08:12,240 --> 00:08:17,660 Now we could also try to update something that doesn't exist like completed with 2D. 113 00:08:17,670 --> 00:08:19,450 Let's say we made a typo. 114 00:08:19,530 --> 00:08:25,690 Down below we can see we do get a bad request and we are seeing invalid updates showing up. 115 00:08:26,190 --> 00:08:26,910 So there we go. 116 00:08:26,910 --> 00:08:31,900 We have c r and you for the crud operations. 117 00:08:31,950 --> 00:08:38,220 The last thing we need to be able to do with our H TTP endpoints is D for delete. 118 00:08:38,220 --> 00:08:42,780 I want to be able to delete users and delete their tasks. 119 00:08:42,810 --> 00:08:46,520 We're going to start that process in the next video with users. 120 00:08:46,560 --> 00:08:48,780 So let's go ahead and jump right at.