1 00:00:01,360 --> 00:00:01,680 All right. 2 00:00:01,690 --> 00:00:05,790 You guessed it onto our next row handler in this case we'll take care of index. 3 00:00:05,800 --> 00:00:10,660 So you need a root handler where you can make a request to API such tickets that you retrieve all the 4 00:00:10,930 --> 00:00:13,450 different tickets we have in our tickets collection. 5 00:00:13,540 --> 00:00:17,470 We will eventually add on some filtering to this row handler to make sure that we can only retrieve 6 00:00:17,620 --> 00:00:19,980 say tickets that are available and still for sale. 7 00:00:20,200 --> 00:00:23,170 But right now we're going to try to retrieve all tickets. 8 00:00:23,170 --> 00:00:27,990 So once again we're going to write out tests then do the implementation as we go on. 9 00:00:28,000 --> 00:00:30,940 We're getting a little bit and a little bit faster with each of these. 10 00:00:30,940 --> 00:00:34,660 So if you're kind of tired of running out these tests and whatnot Don't worry we're gonna get through 11 00:00:34,660 --> 00:00:36,150 this stuff rather quick. 12 00:00:36,220 --> 00:00:40,330 So back inside my editor I'm going to find our test directory. 13 00:00:40,430 --> 00:00:46,260 I'll make a new file inside there of index test to us at the top. 14 00:00:46,550 --> 00:00:55,240 Let's get request from Super Test and app from up to directories at. 15 00:00:55,260 --> 00:00:57,010 So what do we need to test here. 16 00:00:57,010 --> 00:01:01,040 Well without a doubt we want anyone to be able to access this list of all tickets. 17 00:01:01,030 --> 00:01:05,600 You should not need to be authenticated there's not really any input required for this thing. 18 00:01:05,600 --> 00:01:09,620 Not really any case where a user might need to provide an I.D. or anything like that. 19 00:01:09,620 --> 00:01:15,500 So I think in total we really just need maybe one test one test to create a couple of tickets and then 20 00:01:15,620 --> 00:01:16,970 fetch them. 21 00:01:16,970 --> 00:01:20,620 So let's put that together. 22 00:01:20,710 --> 00:01:26,590 I'm going to write out in its statement and say it can fetch a list of tickets 23 00:01:29,390 --> 00:01:33,400 that inside of here we're going to use the same methodology for creating tickets as we did back inside 24 00:01:33,400 --> 00:01:34,210 of show. 25 00:01:34,210 --> 00:01:38,230 So in other words we're going to make a couple of requests to create those tickets manually 26 00:01:41,640 --> 00:01:44,180 it's going to do and await request to app 27 00:01:47,910 --> 00:02:00,200 I'll make a post request to API tickets I'm going to include the cookie not cookie but set cookie then 28 00:02:00,200 --> 00:02:09,130 global dot sign in and then I'll send in a title of whatever and a price of about 20 and then I'm going 29 00:02:09,130 --> 00:02:17,100 to repeat that not just once more so that's going to build out two separate tickets for me rather than 30 00:02:17,100 --> 00:02:22,260 having these two calls directly in a row and making this really confusing and other engineer might look 31 00:02:22,260 --> 00:02:24,800 at this and think there's actually some difference between this. 32 00:02:24,930 --> 00:02:27,570 We're just plain repeating these identical requests in a row. 33 00:02:27,600 --> 00:02:30,920 So how about instead we extract that into a little helper function. 34 00:02:31,040 --> 00:02:39,000 So how about we call it create ticket up here at the top and then inside there I will return and I'm 35 00:02:39,000 --> 00:02:40,980 going to delete the keyword await. 36 00:02:40,980 --> 00:02:45,440 So we're going to make the request and then immediate return the promise. 37 00:02:45,510 --> 00:02:52,740 So now I can call that much more easily from down inside of my test so I can call it create ticket not 38 00:02:52,770 --> 00:02:58,230 Trickett but ticket and because we get back a promise from this we do want to await it so that we can 39 00:02:58,230 --> 00:03:00,780 allow time for the request to be completed. 40 00:03:00,780 --> 00:03:04,360 So I got a chain on our place and a wait right before. 41 00:03:04,360 --> 00:03:08,000 And then how about in total let's create three tickets. 42 00:03:08,080 --> 00:03:09,740 That looks reasonable. 43 00:03:09,960 --> 00:03:11,820 Now we can do a request. 44 00:03:11,820 --> 00:03:17,130 What we're actually trying to test here we're going to do a get request to API slash tickets and we're 45 00:03:17,130 --> 00:03:24,130 really just going to expect that we get three objects in response so I'm going to make a request I'll 46 00:03:24,140 --> 00:03:25,160 capture the response 47 00:03:28,020 --> 00:03:34,370 I want to do a get request to API tickets I'm going to send that off and I'm going to expect to get 48 00:03:34,370 --> 00:03:41,140 back a status code of 200 then inside the response we're gonna have a body property in there that has 49 00:03:41,140 --> 00:03:43,840 the information was actually sent back to us. 50 00:03:43,840 --> 00:03:47,470 I'm going to assume we haven't really discussed the design of this thing just yet. 51 00:03:47,500 --> 00:03:52,060 I'm going to assume that our index routes or this kind of listing route is going to send back an array 52 00:03:52,060 --> 00:03:53,480 of objects. 53 00:03:53,530 --> 00:03:57,970 So I'm really just gonna look at the response dot body property I can expect that to be an array and 54 00:03:57,970 --> 00:04:03,830 that it has like three because there should be three tickets we are returning so I will right out expect 55 00:04:03,860 --> 00:04:15,940 response dot body dot link to equal three and that should be at as usual we can save this go back over 56 00:04:15,940 --> 00:04:17,130 to our terminal. 57 00:04:17,380 --> 00:04:24,230 I see the new test appear and not unpredictably I get a 4 0 4 OK not bad. 58 00:04:24,230 --> 00:04:26,280 Let's go and define our actual route handler. 59 00:04:26,350 --> 00:04:33,120 So inside the routes directory new file index dot t us and very similar to the other route handlers 60 00:04:33,120 --> 00:04:42,750 at the top let's import express request response from Express and I'm definitely going to need my ticket 61 00:04:42,750 --> 00:04:45,090 model to make a query as well. 62 00:04:45,320 --> 00:04:50,240 I'll go up one directory models ticket I'll then create my router 63 00:04:53,470 --> 00:05:00,480 and I'll associate a get route of API tickets I'm gonna put in an async row handler since we definitely 64 00:05:00,480 --> 00:05:12,000 have to make a database query I'll get my request and rez inside of here let's get our tickets we'll 65 00:05:12,000 --> 00:05:18,120 do in a weight ticket dot finds anthro in an empty object that empty object right there usually use 66 00:05:18,120 --> 00:05:22,260 for filtering if you want to have any kind of conditions on what records we actually find we're putting 67 00:05:22,260 --> 00:05:27,930 in an empty object right now to say just give us all the tickets inside this collection after that we 68 00:05:27,930 --> 00:05:35,050 can take those tickets and send them back and that's pretty much it Well what we've written now right 69 00:05:35,050 --> 00:05:39,910 here is not quite everything we need we do have to export this router and wire it up inside of our apt 70 00:05:39,910 --> 00:05:46,730 TSA file at the bottom I'm going to export router as index ticket router 71 00:05:50,270 --> 00:05:55,810 then back over to our apt yes file at the top we will import it right after all the other routers so 72 00:05:55,850 --> 00:06:06,970 index ticket router from routes index and then finally where that up to our app so apt use index ticket 73 00:06:07,270 --> 00:06:07,630 router 74 00:06:11,200 --> 00:06:15,640 let's make sure we save all the files we've changed and we'll go back over to our terminal C our tests 75 00:06:15,640 --> 00:06:21,880 are doing looks like we're passing very good that means that we successfully created three tickets and 76 00:06:21,880 --> 00:06:28,810 then fetched all three of them OK one root handler left all we have to do is take care of put so let's 77 00:06:28,810 --> 00:06:30,790 tackle that entire thing in the next video.