All right.
You guessed it onto our next row handler in this case we'll take care of index.
So you need a root handler where you can make a request to API such tickets that you retrieve all the
different tickets we have in our tickets collection.
We will eventually add on some filtering to this row handler to make sure that we can only retrieve
say tickets that are available and still for sale.
But right now we're going to try to retrieve all tickets.
So once again we're going to write out tests then do the implementation as we go on.
We're getting a little bit and a little bit faster with each of these.
So if you're kind of tired of running out these tests and whatnot Don't worry we're gonna get through
this stuff rather quick.
So back inside my editor I'm going to find our test directory.
I'll make a new file inside there of index test to us at the top.
Let's get request from Super Test and app from up to directories at.
So what do we need to test here.
Well without a doubt we want anyone to be able to access this list of all tickets.
You should not need to be authenticated there's not really any input required for this thing.
Not really any case where a user might need to provide an I.D. or anything like that.
So I think in total we really just need maybe one test one test to create a couple of tickets and then
fetch them.
So let's put that together.
I'm going to write out in its statement and say it can fetch a list of tickets
that inside of here we're going to use the same methodology for creating tickets as we did back inside
of show.
So in other words we're going to make a couple of requests to create those tickets manually
it's going to do and await request to app
I'll make a post request to API tickets I'm going to include the cookie not cookie but set cookie then
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
to repeat that not just once more so that's going to build out two separate tickets for me rather than
having these two calls directly in a row and making this really confusing and other engineer might look
at this and think there's actually some difference between this.
We're just plain repeating these identical requests in a row.
So how about instead we extract that into a little helper function.
So how about we call it create ticket up here at the top and then inside there I will return and I'm
going to delete the keyword await.
So we're going to make the request and then immediate return the promise.
So now I can call that much more easily from down inside of my test so I can call it create ticket not
Trickett but ticket and because we get back a promise from this we do want to await it so that we can
allow time for the request to be completed.
So I got a chain on our place and a wait right before.
And then how about in total let's create three tickets.
That looks reasonable.
Now we can do a request.
What we're actually trying to test here we're going to do a get request to API slash tickets and we're
really just going to expect that we get three objects in response so I'm going to make a request I'll
capture the response
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
back a status code of 200 then inside the response we're gonna have a body property in there that has
the information was actually sent back to us.
I'm going to assume we haven't really discussed the design of this thing just yet.
I'm going to assume that our index routes or this kind of listing route is going to send back an array
of objects.
So I'm really just gonna look at the response dot body property I can expect that to be an array and
that it has like three because there should be three tickets we are returning so I will right out expect
response dot body dot link to equal three and that should be at as usual we can save this go back over
to our terminal.
I see the new test appear and not unpredictably I get a 4 0 4 OK not bad.
Let's go and define our actual route handler.
So inside the routes directory new file index dot t us and very similar to the other route handlers
at the top let's import express request response from Express and I'm definitely going to need my ticket
model to make a query as well.
I'll go up one directory models ticket I'll then create my router
and I'll associate a get route of API tickets I'm gonna put in an async row handler since we definitely
have to make a database query I'll get my request and rez inside of here let's get our tickets we'll
do in a weight ticket dot finds anthro in an empty object that empty object right there usually use
for filtering if you want to have any kind of conditions on what records we actually find we're putting
in an empty object right now to say just give us all the tickets inside this collection after that we
can take those tickets and send them back and that's pretty much it Well what we've written now right
here is not quite everything we need we do have to export this router and wire it up inside of our apt
TSA file at the bottom I'm going to export router as index ticket router
then back over to our apt yes file at the top we will import it right after all the other routers so
index ticket router from routes index and then finally where that up to our app so apt use index ticket
router
let's make sure we save all the files we've changed and we'll go back over to our terminal C our tests
are doing looks like we're passing very good that means that we successfully created three tickets and
then fetched all three of them OK one root handler left all we have to do is take care of put so let's
tackle that entire thing in the next video.