Now that we've got this order status put together let's figure out what's going on with this ticket
Doc reference right here.
Remember we want to replicate all the different tickets that are being created inside of our ticket
service over to the order service as well.
Then on each of our different order documents we're going to provide a reference over to the ticket
that this order is trying to allow a user to purchase so in just a moment we're going to create another
model inside of our models directory.
And this model is going to create this idea of a ticket inside of our order service.
We're going to access these tickets and associate them with the orders through the population system
or the reference system in mongoose.
Let me very quickly give you an example of how that code or how that Mongoose stuff is going to work.
So there's really three different scenarios we need to worry about here.
I wrote out the three different scenarios in code rather than the diagram because diagram is kind of
hard to understand when it comes to just strictly code and how we actually write the code out to the
three things we need to understand how to do is how to associate an existing order and a ticket together
to associate an existing ticket with a brand new order that is about to be created and then finally
how to fetch an order from our database and the associated ticket that is whether or the ticket that
is associated with it.
So first case right here how do we associate a existing order and ticket together.
Well we're going to pull a ticket in order out of the database.
We're then going to take that ticket and set it on the ticket property of the order and then save the
order to the database.
That's pretty much it.
That's how this ref system works with Mongoose if you want to associate an existing ticket with a new
order.
So this really very much is what we are about to do inside of our new route handler.
We're going to pull the ticket out of the database.
We're then going to try to build in order and for the ticket property we're going to assign the ticket
that we just pulled out of the database.
We would then save the order to the database like so.
And then finally whenever we want to figure out what order is associated with what given ticket we'll
do a query for an order.
And on that query we're going to also add in this little populate method to it to a call populate provided
the name of the record or the key on the order that we want to populate.
In this case it is ticket as we are setting that ticket property right there.
Then when Mongoose goes and finds this order inside the database it will also fetch the associate with
ticket with it as well.
And we can then access that ticket off of the order by writing out order dot ticket.
So for example if we wanted to know the price of the ticket would you order ticket price if you wanted
to know the title the ticket order that title that ticket.
That's pretty much it so we get all this stuff working we've already set up the order itself correctly.
We just have to also add in a new model or a ticket as well.
That's pretty much it.
So let's pause right here.
We're going gonna come back the next video create a model for our ticket.