At this point there are two ways we can move forward on our order service.
We can either start to take a look at how we're going to publish some events around orders and listen
or subscribe to events coming in as well.
The other direction we can move is to focus first on all of our different root handlers.
We're going to first implement these root handlers and then we're going to come back to all the event
related stuff.
The reason for that is that we still have not really done a deep dive on how we designed different events
how we communicate them across different services and how we effectively think about the flows of a
event through our application.
So I really want to put together all these different handlers first and once we have all that completed
will then have to really complete services the ticket service and the order service that needs have
a really solid exchange of data through the eventing system.
So at that point we can have a really deep dive and understand how to get these things to work together
effectively.
Having said that we still have this comment inside of our new root handler and I would definitely not
want to forget that we need to come back here and eventually publish an event.
So as a kind of reminder for that I'm going to go back over to the test file that we were just working
on and at the very bottom I'm going to add in a new test right here and I'm going to market as a test
that we need to come back and write so I can write out.
It's got to do emits an order related event if I now see this file with that dot to do on there and
go back over to my terminal you'll notice that that test is now showing up with this little special
purple font and we are also being told there's one to do.
So this is just to remind us that we need to come back to the new root handler and add in that root
or extremely order created event.
So that looks good.
So we will come back to all the event related stuff.
Once these root handlers are put together the next round handler we're gonna put it together is the
index root handler so with this index right.
HANDLER We want to find all the different orders related to the user who is making the request and just
return them.
That is pretty much it.
So let's give this a shot to get started inside my roots directory.
I'm gonna find the index t s file.
We've already got some stuff inside of here.
All we really have to do is add in a couple of different imports wire up middleware to add an add in
some code to fetch all the related orders and send them back this right handler handlers can be pretty
simple in nature so let's just get to it right away.
The first thing we want to do here is to make sure that a user is logged in if they want to access this
right handler.
Orders are tied to very specific users and we only want a user to see the orders that they have created.
So it definitely makes sense to say that you should only be able to access this handler if you are authenticated.
So that mind at the very top we'll get started by importing require off from our common library and
then add that as a middleware.
Like so
then if we get inside the body of this request handler we're definitely going to want to run some kind
of query over our order collection and find all the different orders that belong to this user to run
that query.
We're definitely going to have to import our order model at the top of the file so I will import order
from up one directory models order
then inside of here right above the rest sent let's do a orders and we'll run our query so to run the
query we're going to write out order find.
And then inside of an object we're gonna put in the criteria of what orders we want to search for.
You'll recall that all of our different orders store a user I.D. property and that user I.D. is the
idea of the user who owns this order so we're going to do a query and try to find all the orders where
the user I.D. is equal to the idea of the user who just made our request.
And remember we are setting all that current user stuff and making sure that is available on the request
to do the require auth and the current user middleware.
So we should be able to do recurrent user exclamation dot i.e. that will get us all our orders.
Now for each of the orders that we fetch we probably want to also include the ticket that the order
is for.
So in other words we should probably make sure we somehow fetch the ticket and get its title in its
price and so on and send those back along with it.
We're gonna do so using that populate system in mongoose.
You recall that we took a look at how to fetch a particular order and also simultaneously load up the
ticket associated with them.
All we have to do is chain onto the end of this query a dot populate ticket that's it
so once we get that list of orders pretty simple we'll just send it back.
So resort sent and I'll send back the orders and I should be at.
All right so let's say this.
Once again we probably need to make sure that we write out a test to make sure that this works as expected.
Again we don't have any code inside of our project right now around creating a ticket.
So the only way that we can really simulate or test this stuff is through a automated test.
So we're going to write out a test to create maybe one or two orders and assign tickets to both them
and then make a request and just make sure that we get the list of orders tied to this user and the
associated tickets.
So let's put that test together in just a moment.