In this video we're going to tackle the show request handler.
This request handler is going to get information about a very specific order.
Once again we're going to make sure that only authenticated users can access this through our handler.
And we're also going to make sure that users can only access their own orders.
So one user can not look at another user's orders in total.
This right handler is going to look very similar to the index one that we just put together.
So let's just dive right in and get to it back inside to my editor I'll find my roots directory and
we've already created the show to yes file inside there.
So your show.
Yes at the very top we're going to add in a couple of different imports right away without a doubt.
We need the require off middleware from our common module.
I'm also going to get the order model from up to directories or see me just one directory models order
let's apply that require off middleware.
So right above the or right before the actual it request handling function will add and require of then
inside the root handler itself.
We're going to take the I.D. or the order I.D. off the request parameters and we'll just try to fetch
some order out of our database using that order I.D. sort through inside of your order is going to be
a wait order.
Fined by I.T. No.
That is how we look up a very particular record based on its I.T. and the idea that we want to try to
look up is going to be coming out of the request per Ram's object.
So it will be rec Korans not order I.D. We're really making a big assumption here.
Just assuming that the user is going to provide a accurate or what looks like a mongo DV idea right
here and we already had a discussion a little bit ago about doing some validation to make sure that
a valid I.D. is provided.
So we could add in a validation step to make sure that that parameter right there is looking like a
real Mongo DV I.D..
I'll leave that up to you if you want to add that in so when I going to fetch the order and when we
fetch the order we probably want to also simultaneously retrieve the associated ticket as well.
So we're going to once again use that populate system in Mongoose so to say that we want to find this
order and also simultaneously fetch the associated ticket.
We're going to chain onto the very end dot populate ticket like so now this should give us our order
and the associated ticket
when we run this query is entirely possible that we will not find any order whatsoever.
So a user might provide an invalid I.D. they might provide an I.D. for some order that has not yet been
created.
Who knows.
But first we need to do is to make sure that we actually found in order we'll say if we did not find
an order then we probably want to throw a not found error until a user.
Sorry but we were not able to actually find anything so if we get inside of here we will throw a new
not found error let's make sure we import that error from our common module so not found error.
Next up if the user does not own this order then we probably want to throw in error as well.
So again one user cannot look at another user's orders so we'll say if the order got user I.D..
So this thing's user I.D. if the person who owns this order is not equal to the idea of the person making
the request.
Well we need to throw a not authorized error or something like that.
So say if the order is user I.D. is not equal to recurrent user and don't forget the exclamation.
Because we want to tell typescript Don't worry we already made sure that there is a current user property
and then dot I.D. then we will throw a new not authorized error and once again we need to make sure
we import that from our common module
so if we get past both those little checks right there then that means say we found the I.D. or something.
We found the order and we actually want send this back to the user.
So at the very bottom we can replace that empty object with order and send back the entire thing to
the user.
That should be it.
Let's save this.
As usual we don't have really any mechanism right now to effectively test this is we don't have the
ability to really create tickets to associate with a given order.
So once again we're gonna take a quick pause.
Come back the next video and write out a quick test.
And don't worry.
The test for this is going to go way faster than the index one we just put together.
Let's take a pause right here.
Get started on this in just a moment.