1 00:00:00,960 --> 00:00:02,260 Our tests are all complete. 2 00:00:02,280 --> 00:00:06,540 So let's go ahead and build out the actual show root handler inside my roots directory. 3 00:00:06,540 --> 00:00:12,490 I'll make a new file of show dot yes we're then going to add in everything required inside of here to 4 00:00:12,490 --> 00:00:14,360 actually build out our root handler. 5 00:00:14,360 --> 00:00:24,940 So I'm going to import Express and the request type and the response type from express without a doubt. 6 00:00:24,940 --> 00:00:28,770 I'm going to need to be able to access my ticket collection inside the database. 7 00:00:28,770 --> 00:00:33,550 So I'm also gonna get my ticket model from up one directory models. 8 00:00:33,640 --> 00:00:43,050 Ticket I'll then build up my router with Express dot router I'll then set up the handler itself though 9 00:00:43,110 --> 00:00:48,720 router dockets we want to watch for get requests to API tickets and then the I.D. of the ticket we're 10 00:00:48,720 --> 00:00:54,240 looking for we're going to be making a request to our database from inside this thing. 11 00:00:54,260 --> 00:01:04,080 So it's definitely gonna be an async route handler I'll then add in my rec as request and rez as response. 12 00:01:04,080 --> 00:01:08,160 And then finally just because we know we have to do it let's export this thing right away and wired 13 00:01:08,160 --> 00:01:16,270 up to our app at the very bottom up it in an export router as show ticket router 14 00:01:19,860 --> 00:01:22,340 I'll then go back over to my apt to yes file 15 00:01:25,370 --> 00:01:26,920 I'll add an import statement for that router 16 00:01:32,890 --> 00:01:37,440 from roots show and then again we'll wire that up to our app. 17 00:01:37,650 --> 00:01:44,690 So app dot use show ticket router. 18 00:01:44,720 --> 00:01:45,230 All right. 19 00:01:45,240 --> 00:01:50,260 Gonna save this now after we save all this stuff our tests are going to rerun and you're probably gonna 20 00:01:50,280 --> 00:01:51,830 see your tests hanging. 21 00:01:51,830 --> 00:01:56,480 The reason for that is that we've now got a root handler to receive these incoming requests to the show 22 00:01:56,480 --> 00:01:59,410 route but we're not actually responding in any way. 23 00:01:59,540 --> 00:02:01,820 Eventually the test is going to time out. 24 00:02:01,820 --> 00:02:04,850 I believe the default just is about 20 or 30 seconds. 25 00:02:04,850 --> 00:02:09,080 So actually if we flip back over after some amount of time it will tell us it looks like my default 26 00:02:09,080 --> 00:02:14,910 is actually five seconds in this case Doctor five seconds it naturally times out and both tests fail 27 00:02:16,190 --> 00:02:18,050 let's put together some implementation. 28 00:02:18,050 --> 00:02:22,160 We're going to first try to focus on getting this one right here to pass so on to make sure that if 29 00:02:22,160 --> 00:02:28,730 we cannot find the ticket let's return a 4 or 4 right away inside the root handler. 30 00:02:28,840 --> 00:02:33,910 All we have to do is use the ticket model to look into our collection and find the idea that was mentioned 31 00:02:33,940 --> 00:02:41,580 in that your URL will say counts ticket is a weight ticket dot find by I.D. And we're going to execute 32 00:02:41,580 --> 00:02:48,000 this query with the REC ramps that ad I.D. so wrecked out perhaps I.D. That is the idea that we captured 33 00:02:48,030 --> 00:02:54,730 out of the URL find my I.D. is a query helper built directly into our model as you'd guess it's just 34 00:02:54,750 --> 00:02:58,800 going to try to find a particular record coming out of this query. 35 00:02:58,820 --> 00:03:04,940 We're going to get either our ticket documents or if nothing was found it will instead be equal to no. 36 00:03:04,940 --> 00:03:10,160 So now we can take a look at tickets and if it is equal to nil or essentially if it's not defined then 37 00:03:10,160 --> 00:03:14,600 we definitely want to return a status code of 4 0 4 to the user. 38 00:03:14,600 --> 00:03:15,620 So let's handle that first. 39 00:03:15,910 --> 00:03:20,660 Well say if not ticket then we need to return the status code. 40 00:03:20,820 --> 00:03:25,530 So naturally we could do that return resorts and status blah blah all that stuff. 41 00:03:25,530 --> 00:03:30,560 But remember we've got that fancy fancy fancy airy handling system already put together. 42 00:03:30,930 --> 00:03:36,180 If we think back to our common module inside of our ears directory we had put together that custom air 43 00:03:36,210 --> 00:03:38,040 of not found air. 44 00:03:38,040 --> 00:03:42,690 Nothing was really built specifically to handle the case array and right now the case in which we make 45 00:03:42,690 --> 00:03:50,130 a request and try to find something that just does not exist so if we do not find a ticket what we should 46 00:03:50,130 --> 00:03:54,300 really be doing is throwing a not found air to do so. 47 00:03:54,340 --> 00:04:06,040 Let's import not found air from our common module and inside of your throw new not found 48 00:04:09,890 --> 00:04:13,280 then if the ticket is found let's send it back in response. 49 00:04:13,280 --> 00:04:19,370 So after the if statement we can do a resort send ticket whenever we leave off a status code it will 50 00:04:19,370 --> 00:04:26,190 default to two hundred and so that two hundred will be successfully caught by that expectation right 51 00:04:26,190 --> 00:04:27,710 there all right. 52 00:04:27,760 --> 00:04:28,880 Now I'm going to save this file. 53 00:04:28,880 --> 00:04:30,320 We're going to check our test output. 54 00:04:30,340 --> 00:04:32,950 I want you to know that we're going to see an error. 55 00:04:32,970 --> 00:04:35,360 Something's going to be failing erroneously. 56 00:04:35,560 --> 00:04:39,970 And the reason for this is well we're gonna take a look at it is an error they wanted you to see on 57 00:04:39,970 --> 00:04:44,740 your own just to see that even though yeah we could write tests sometimes they don't quite work as expected 58 00:04:44,860 --> 00:04:47,050 and sometimes they actually help us catch them. 59 00:04:47,050 --> 00:04:51,120 Issues that we would have not really found if you were just testing this inside the browser. 60 00:04:51,400 --> 00:04:59,960 Let's save this flip back over tests are running and it looks like our test to successfully create that 61 00:04:59,960 --> 00:05:03,030 ticket and then fetch it worked that one passed. 62 00:05:03,080 --> 00:05:09,610 So the good case this longer one right here that thing passed but this one failed. 63 00:05:09,710 --> 00:05:12,730 The one we were checking to make sure we got a four a four. 64 00:05:12,890 --> 00:05:14,150 So we got a message here. 65 00:05:14,150 --> 00:05:20,220 We expected to get a forum for status but instead we got back a four hundred. 66 00:05:20,780 --> 00:05:22,980 That is definitely unexpected. 67 00:05:23,030 --> 00:05:27,380 So we're going to have to do a little bit of troubleshooting a little bit of debugging to figure out 68 00:05:27,560 --> 00:05:28,640 why this is happening.