1 00:00:00,180 --> 00:00:00,980 As promised. 2 00:00:01,020 --> 00:00:06,420 This is a challenge video and in this one you'll be setting up two end points that allow someone to 3 00:00:06,420 --> 00:00:10,260 fetch multiple tasks or a single task by I.D.. 4 00:00:10,260 --> 00:00:16,960 Down below I have the challenge comments big picture goal the set up the task reading and points. 5 00:00:17,010 --> 00:00:24,020 The first step is to create the endpoint that allows you to fetch all tasks now later on in the class. 6 00:00:24,030 --> 00:00:31,160 When we add authentication in will limit this to being able to fetch all tasks that you actually created. 7 00:00:31,170 --> 00:00:36,550 If I as my account create a task you as another account shouldn't be able to read it. 8 00:00:36,550 --> 00:00:41,980 We're not worrying about any of that for now but I wanted to mention it's something we'll get into later. 9 00:00:42,030 --> 00:00:48,240 So you're going to set up one end point for fetching all of the tasks then another for fetching a single 10 00:00:48,240 --> 00:00:50,270 task by its I.D.. 11 00:00:50,460 --> 00:00:57,360 Similar to what we did in the last video for users then you'll use postmen to test your work set up 12 00:00:57,360 --> 00:01:03,060 new requests for both of those endpoints firing things off and make sure you're getting the correct 13 00:01:03,060 --> 00:01:06,110 results with the second end point. 14 00:01:06,120 --> 00:01:12,540 Make sure to test it with a valid I.D. and an invalid I.D. ensuring that you're getting the expected 15 00:01:12,540 --> 00:01:14,880 results in both scenarios. 16 00:01:14,880 --> 00:01:19,740 Take some time to knock that out test your work and when you're done come back and click play 17 00:01:23,810 --> 00:01:24,560 how'd that go. 18 00:01:24,590 --> 00:01:30,140 Let's go ahead and jump right in by starting with the end point that's going to fetch all of the tasks 19 00:01:30,470 --> 00:01:37,010 right here that'll be app dot get to set it up and the path is a forward slash and tasks. 20 00:01:37,010 --> 00:01:42,590 Now from here we're going to set up our callback function with request and response and the first thing 21 00:01:42,590 --> 00:01:49,360 we're gonna do is use the task model and access its find method. 22 00:01:49,370 --> 00:01:53,390 In this case we're not actually trying to limit it by any criteria. 23 00:01:53,390 --> 00:01:54,920 We want to fetch everything. 24 00:01:55,010 --> 00:02:01,880 So we're not going to put any attributes on the object from here we can set up our call to then and 25 00:02:01,880 --> 00:02:05,060 we'll get access to the tasks that were found. 26 00:02:05,300 --> 00:02:10,270 Now it's gonna be an array but it might be an empty array if you don't have any tasks. 27 00:02:10,340 --> 00:02:11,180 What are we going to do. 28 00:02:11,180 --> 00:02:18,160 We're just going to send them back using response dot send send the tasks Next up down below. 29 00:02:18,170 --> 00:02:21,530 We want to make sure to handle errors when things go wrong. 30 00:02:21,650 --> 00:02:24,230 We'll set up that catch call right here. 31 00:02:24,230 --> 00:02:30,180 We're going to provide the callback function and in this case we'll just send back a five hundred. 32 00:02:30,260 --> 00:02:33,270 So response dot status. 33 00:02:33,370 --> 00:02:34,530 Five hundred. 34 00:02:34,590 --> 00:02:37,450 Then we'll go ahead and use send. 35 00:02:37,490 --> 00:02:40,430 So that's gonna take care of the tasks route. 36 00:02:40,520 --> 00:02:44,210 Let's go ahead and test that before moving on to the next one. 37 00:02:44,210 --> 00:02:49,750 Over inside of post man I'm gonna add a new request to our collection. 38 00:02:49,790 --> 00:02:52,820 I'll call this read tasks. 39 00:02:52,880 --> 00:02:59,990 FORERO I'm going to create the new route I'll go ahead and save my old one first then I'll crack open 40 00:02:59,990 --> 00:03:03,460 read tasks and set up the U.R.L. local host. 41 00:03:03,460 --> 00:03:07,000 Port three thousand forward slash tasks. 42 00:03:07,190 --> 00:03:11,060 And if I go ahead and fire that off what do I get down below. 43 00:03:11,060 --> 00:03:16,280 I get all of the tasks I've created so far so that is it for the first route. 44 00:03:16,310 --> 00:03:19,070 Let's go ahead and knock out the second one. 45 00:03:19,070 --> 00:03:21,460 That'll be a another call to app. 46 00:03:21,460 --> 00:03:29,690 Dot get this time providing a path that also contains one of those root parameters so up above we had 47 00:03:29,690 --> 00:03:33,650 used one for this route and we'll do the same thing down below. 48 00:03:33,650 --> 00:03:41,300 So the route is forward slash tasks forward slash colon I.D. And once again getting the request and 49 00:03:41,300 --> 00:03:52,210 the response and the first thing we're gonna do is extract that I.D. const I.D. equals request dot parameter 50 00:03:52,270 --> 00:03:53,410 dot I.D.. 51 00:03:53,530 --> 00:04:03,370 And from here we can go ahead and use defined by I.D. method on task that is task dot find by I.D. providing 52 00:04:03,370 --> 00:04:09,790 the I.D. as the first and only argument then we call that right here we'll go ahead and set up that 53 00:04:09,790 --> 00:04:11,190 callback function. 54 00:04:11,380 --> 00:04:13,970 And if it did work we will get the task now. 55 00:04:14,020 --> 00:04:16,240 It could also work and we don't get a task. 56 00:04:16,240 --> 00:04:19,380 So we want to make sure to set up that conditional logic. 57 00:04:19,390 --> 00:04:26,440 Once again right here if there is no task. 58 00:04:26,520 --> 00:04:28,540 What are we going to go ahead and do. 59 00:04:28,560 --> 00:04:34,530 We will use return to stop the function execution and we'll send a 4 0 4 back response. 60 00:04:34,530 --> 00:04:36,870 Dot status for 0 4. 61 00:04:36,900 --> 00:04:38,460 Then we'll use send. 62 00:04:38,520 --> 00:04:39,690 And next up. 63 00:04:39,690 --> 00:04:42,290 This is only going to run if there was a task. 64 00:04:42,300 --> 00:04:49,330 In that case we want to send that back with the two hundred status code response dot and task. 65 00:04:49,380 --> 00:04:50,580 Excellent. 66 00:04:50,580 --> 00:04:55,080 The only thing we need to do is handle other errors that come up using catch. 67 00:04:55,200 --> 00:04:56,020 Right here. 68 00:04:56,280 --> 00:04:58,740 We're gonna go ahead and just send back the 500. 69 00:04:58,740 --> 00:05:03,480 Once again response dot status five hundred dot send. 70 00:05:03,510 --> 00:05:04,560 Perfect. 71 00:05:04,650 --> 00:05:05,890 Now we're all done. 72 00:05:05,970 --> 00:05:09,660 We can move on to the final step which is to test our work. 73 00:05:09,660 --> 00:05:12,130 So I'm going to remove the challenge comments. 74 00:05:12,210 --> 00:05:18,360 We have our two routes in place and over inside of postmen we'll test out the second one. 75 00:05:18,600 --> 00:05:23,630 That's going to require us to create a brand new request in our collection. 76 00:05:23,680 --> 00:05:29,580 I'll call this read task singular and we'll go ahead and set up the. 77 00:05:29,580 --> 00:05:30,560 You are out. 78 00:05:30,660 --> 00:05:32,480 I'll make sure to save my old one. 79 00:05:32,640 --> 00:05:35,710 Crack open the new request and start to fill this out. 80 00:05:35,730 --> 00:05:43,060 That will be local host on port three thousand forward slash tasks forward slash the I.D.. 81 00:05:43,200 --> 00:05:47,070 Now I can grab one of the task ideas I had over here. 82 00:05:47,070 --> 00:05:49,410 I'll go ahead and grab the one for the task. 83 00:05:49,470 --> 00:05:50,530 Eat lunch. 84 00:05:50,610 --> 00:05:52,050 I'm gonna take the I.D.. 85 00:05:52,170 --> 00:05:55,710 Bring it over to the other request tab and paste it right in. 86 00:05:56,250 --> 00:05:59,200 Now if I go ahead and fire this off what do I get. 87 00:05:59,250 --> 00:06:04,710 I get my two hundred and I get my task showing up with description of eat lunch. 88 00:06:04,710 --> 00:06:11,270 Now let's change this I'll change the leading five in the I.D. over to something else like a one. 89 00:06:11,310 --> 00:06:15,360 This should result in a 4 0 4 and that is exactly what we get. 90 00:06:15,420 --> 00:06:19,490 No task was found with this I.D. which is correct. 91 00:06:19,500 --> 00:06:26,370 So now that we have this in place we have the resource end points for creating resources and for reading 92 00:06:26,370 --> 00:06:26,880 them. 93 00:06:26,910 --> 00:06:28,890 That leaves just two left. 94 00:06:28,920 --> 00:06:34,800 The ability to update our resources and the ability to delete resources. 95 00:06:34,830 --> 00:06:37,590 Let's go ahead and continue on in the next lecture.