We've got our data layer put together which means we can now go back over to our new rabbit handler
the one we were working on just a little bit ago and start to put this implementation together.
So inside of my new root handler at the very top I'm going to import those two models that we just created.
So I'm going to get tickets from up one directory models ticket and order from up one directory at models
order.
Now we've got the ability to potentially fetch a ticket out of our database and potentially save a order
as well.
So now let's go down to our right handler.
The first thing we're going to do inside of here is write out a couple of comments and really think
about what logic we need to implement inside of here.
So this is really going to require us to deeply understand what this application is all about.
So as a quick reminder the whole idea here is that we want to build to create an order associate with
a ticket that has already been created inside the database.
And we also want to make sure that order will expire after 15 minutes.
So we make sure need to make sure we set that expires at flag as well.
So this mind let's write out a couple of different comments just to guide ourselves.
The first step to do inside of here is find the ticket.
The user is trying to order in the database so when we try to find the ticket that the user trying to
order.
If we fail to find it for some reason then potentially the ticket has already been purchased maybe the
ticket has been deleted.
Maybe the user for some reason crafted a malicious request to try to buy a ticket that doesn't exist
for some crazy reason.
So if we fail to find the ticket that the user is trying to purchase we're going to want to definitely
return some kind of air immediately after that.
We need to make sure that this ticket is not already reserved so the entire idea here with reserving
a ticket for 15 minutes is that we're kind of planning around the idea of our application getting a
lot of traffic and a lot of people trying to buy the same tickets at the same point in time.
So in just one second of time in other words in the instant of time we can kind of imagine that we might
get 10 different requests of a bunch of users all trying to reserve the exact same ticket so we need
to somehow make sure that this ticket is not already been reserved by someone else.
Just finding that ticket inside the database is not enough.
Once we have done these two steps right here we're then going to calculate an expiration date for this
order so as I mentioned we're going to assume that the order has 15 minutes to pay.
After that the ticket will essentially go unlocked and the order will enter a canceled state that 15
minutes is entirely arbitrary of course we can make it 15 seconds we can make it 15 days.
It really doesn't matter.
We're going to make it very easy to change variable but we do have to make sure that something time
we calculate when that expiration date occurs.
So we're gonna do that inside of here.
Then after that assuming we get past all these checks we calculate that expiration date then we will
finally go ahead and build the order and save it to the database and then immediately after that we
definitely are going to want to tell the rest of our application that an order has just been created.
We definitely need to think through what other services need to know.
That order has been created.
So what do a little bit of research or thinking on that front but chance all right now we can definitely
assume that.
Well we need to tell the rest of our application that order was created.
We'll say publish an event saying that an order was created and remember publishing and events or creating
a new event.
We don't have any infrastructure right now to say that a order was created.
The only kind of events we have are list out inside of our common module specifically inside of that
events directory.
So right now the only two events we have our ticket created and to get updated.
So for this last step right here we will definitely once again have to go back over to our common module
and create these new types of events something and say hey the order was created and then after we create
that event of course we'll also have to define a publisher inside of our orders project.
So there really is a lot of stuff going on inside of here.
Let's get started right away.
So we're going to go through this first step right here.
We're going to try to find the ticket that the user is trying to order inside of our database on a line
right above it.
I'm going to take a look at the incoming request body and I'm going to pull off the I.D. the ticket
that the user is trying to order.
So I'll do a ticket I.D. I mean from wrecked up body
so let's go ahead and then try to find that ticket inside the database.
This is going to be a very standard Mongo DV or Mongoose query.
So do a ticket is a weight ticket find by I.D. and we'll pass in the I.D..
So now when we do this find my I.D. We're going to either get our ticket or not a ticket.
So this might possibly be a value of null if we were not able to find an I.D. with the one that the
user just provided.
So we need to make sure that we handled a case in which no ticket was found.
So say if there is not a ticket then let's throw a new not found error.
Remember this is one of those custom errors we put together if we throw this air then our area handling
middleware it will automatically capture it turn a response into a for a four and send it back to the
user we do need to make sure that we import that type into our file however.
Let's go back up to the top very quickly find our common module and import not found error.
Now I don't want this video to run too long as well as we normally do.
So let's now take a pause right here.
We'll come back and start to implement everything else inside here in just a moment.