We've got this nice query written out right here that will determine whether or not a given ticket is
already reserved.
And as I mentioned at the end of last video there's very likely going to be some other scenarios where
we need to decide whether or not a given ticket has been reserved.
So we're going to extract this logic into a separate file.
In particular we're going to extract it into the ticket model file.
It's going to go back over to models ticket to yes we're going to add a new method onto the ticket model.
This is going to allow us to decide whether or not a ticket has actually been reserved.
Let me show you how this is going to work.
I'm going to go down to the interface of ticket doc right here and on the ticket talk interface.
We're going to add in some new method that's going to exist on a individual ticket document and say
that we're going to add in a new method called is reserved.
If this thing ever gets called we're going to return a promise that is going to resolve with a boolean
so once we implement this is reserved method I'm going to flip back over to our right handler really
quickly once we implement that method we will be able to write out some code like is reserved is a wait
ticket dot is reserved.
That is the goal.
That's what we're going to try to eventually add in.
So we're going to build a ticket ticket called Method and that's going to return to us whether or not
this has been already assigned to some other valid order inside of rap so inside of that is reserved
method that we can write in just a second.
We're going to essentially just run this exact query right here that's gonna make it a lot easier to
figure out whether or not a ticket has been reserved and really shorten down the code we have inside
of this root handler.
So that's the idea.
So let's go back over to the ticket model file.
We're gonna go down towards the bottom and right after we define that build method on statics the statics
object is how we add a new method directly to the ticket model itself.
Remember this is the ticket model.
If the object that allows us get to get access to the overall collection if you want to add a new method
to a document then we're going to add in a method or a property to ticket schema methods that is reserved.
We're gonna set the sequel to a keyword function supercritical here.
Please make sure you write out a function keyword instead of an era.
Function so by putting in ticket schema methods is reserved.
We can now run what you saw just over here a moment ago.
We can do something like this.
This is how we add a method directly to the ticket document itself.
So why did we add in a function keyword.
Well Mongoose has you seen several times is kind of stuck in the ways of old so to speak in the old
ways of writing javascript I suppose.
So in order to actually get information about the ticket document that we're operating on when we call
this method right here we're going to access this so inside this function this is equal to the ticket
document that we just called is reserved on in the context of our new route handler.
We're just looking at a moment ago when we write out something like this right here the value of this
inside of that function is going to be equal to that ticket document that we just found inside the database.
A moment ago the reason we have to use a function keyword inside of here is because if we use an arrow
function it's going to mess around with the value of this inside of the function.
So that's what we have to use a keyword function.
We're going to also mark this function as being async so we can use the async await syntax than inside
of here.
We're gonna write out essentially the same query that we were looking at a moment ago inside of our
root handler.
Let's just go and copy paste thing to save a little bit of time don't find the query that we just wrote
here it is right here.
I'm going to cut that whole thing go back over to our ticket model file and then paste it inside of
your
it's now inside this function.
The ticket document that we're trying to look for is going to be equal to this inside this function.
So we're gonna replace ticket with this like so.
We also need to make sure we import the order model and the order status you name at the top as well
so go to the top.
I'm going to import order from same directory order and now we could get order status from our common
module inside of here.
But there's another thing we could do very easily in the context of this orders service right here.
There are several different files that are going to need to access the order status.
It's a little bit confusing to have the definition of exactly what an order is coming from either the
order bottle file and then in some cases our common module instead you'll make a lot more sense if we
could do something like order status.
Like so you know just make a little bit more sense because now the implication here is that we have
one file that has a definition of everything order relayed inside of application.
So rather than having the separate import same right here we're going to try to do this.
So we're going to try to get order status in the order file.
So in order to write this right here that means we have to go back over to our order model file.
We already imported order status over here.
Now we're just going to re export that thing like so.
So now we go back over to the ticket model file that import is now working.
So again the idea here is just to have one source for everything order related okay.
Let's get back on track here.
So now those errors to be gone so inside this is reserve function.
We should now have our existing order that again that thing is going to be either some order document
or null.
If we do not actually find a value from is reserved we don't really care necessarily about the ordered
that this ticket is associated with the name of is reserved kind of just implies that we're going to
return a boolean value of either true.
Yes this thing is reserved or false.
No it is not reserved and as a matter of fact that's even what we wrote out inside of our method signature
right fort as well.
We said it the promise was going to reserve a similar result with a billion rather than just returning
the existing order.
We're going to return not not existing order that's essentially going to take existing order.
If that thing is equal to no it will be flip to true by the first exclamation and then flip back to
FALSE with the second one and then if existing order is defined we're gonna flip at first a false and
then flip it to true.
So we're just doing that to the side or seeming to return a boolean true Yes it is reserved or false.
No it is not.
So that should be it is going to save this file we can now go back over to our new root handler
and now we can write out something right underneath that comment where we just had a really long query
we could say something like is reserved is a weight ticket dot is reserved and then on the if statement
will say if is reserved.
Well I don't know about you but this is a lot easier to read than what we had before.
It's not really as clear what it means to be reserved.
So this comment being right here does it really make a lot of sense.
We might want to move that comment over to the model file.
I'm going to delete it.
You can certainly move it over if you wish.
But this right here a lot easier to understand what is going on.
We've got a very well-known method that's going to tell us with a simple boolean.
Yep.
It's reserved or no it's not.
So this is a definite good and prevent still a little bit to take care of inside of this handler.
Let's continue in just a moment.