- [Instructor] All that is left to do now
to finalize the order management
is to use the order service in our routes.
So for that, I first go into server routes,
basket index.js,
so this is the basket page,
and there we already bring
in the order service and instantiated in that line,
and if I scroll down all the way to see
that here we have the route
for actually buying the contents of this basket.
So let's remove the return statement and the comments here
and also remove the next argument
and first in line 69,
I'm checking if there is a current user,
if not, something is wrong,
the user should not be on this page,
so I redirect and provide a warning.
Otherwise I will get user at the end,
user from the current user
and then I instantiate the basket for this given user
and get all the basket items in line 82.
If there are no basket items,
I throw an error,
because if that happens,
I should not be on this page anyways.
Otherwise I will now iterate through all the basket items
and for each of them,
I'm using the item service to fetch the item from Mongoose,
because I want the item name,
the SKU and the price from this Mongoose record.
So this is in line 93 and right afterwards,
I'm returning this new composed object
that just contains the information
that we need to later then write into our other tables,
what you also see is that I'm using promise all here.
I can do that because this mapping function
returns a map of promises
and with that, I can pretty much run all the statements
for all basket items in parallel.
Then in line 104,
I'm using this inTransaction method
and pass in a work function
that takes this transaction argument,
and in this work function,
I now first treat the order
and there I pass in just the user and the items,
and again, the transaction,
then again, I'm using promise all to in parallel,
clear the basket from all items,
because once I have placed the order,
the basket of course should be empty,
and in the end, I just print out a thank you message.
So that should be it for the basket and order page,
but we also have one page that lets us manage the orders
and this is in admin, orders, index.js,
and there I will first bring in the order service
so I add const OrderService = require
and they go three levels up,
services, OrderService.
and I can instantiate the order service right in my module.
Exports body.
There I add now const order = OrderService,
and I pass in config.mysql.client,
and let's not forget to also,
and we see it already
that this link complains.
Config is missing,
so I'm passing the config.
Let's see why it's crashing now.
Oh, I see I forgot the new here.
So that's it, new OrderService.
So that's also cool about Node Mong reloading everything,
because if I screw something up,
it shows pretty quickly,
and now it works again
and now I have the route that shows me the orders
and I remove the return statement and the comments
and I have to add the next argument here
for the routing function,
and let's look at what we're doing here.
So first I'm calling get all on the order service
to get all orders,
and then I call get for each item with the argument plain
which then gives me just a plain array
because before the item is an object provided by SQLizer,
so this converts this object into a plain array
and then I render admin orders,
and pass in this other array that we just created.
If I scroll further down
we also see this setshipped method.
So this is used to change the status of an item to shipped.
So we'll activate that now as well.
We can remove the next here.
So what am I doing here?
I run order.setStatus,
and pass in the order ID and I pass in the status,
and this is now shipped.
So that's all I'm doing using the order service.
So with that, everything should work now.
So let's look into the browser, I'm on the basket page
and now I hit buy now
and I get the message,
"Thank you for your business."
So this worked, so let's now look into the order management.
This would be where the shop owner then looks into
and I'll click on manage orders.
I see that I have here now, my order.
So let's create one more order for that.
I go to manage users and now I pick on chain,
and I go into the shop and add a few more items here
and then I go to my basket and click buy now again
and we are now looking to manage orders.
Now you see that we two orders in here,
the ID you see on the left,
that might be different for you,
so this is an auto increment and it really depends,
and then try things out before a little bit.
So the auto increment is already three and four,
but the rest should look rather similar.
So you have the user that did the order,
the date as we get it from the database
for the created app
and the items that were ordered
and also the quantity.
I left out the item name here,
just to make this table a little bit smaller.
Now, when going through this orders,
I can also set them to ship,
so I'll click on Set Shipped,
and you see now,
this order has the state, Shipped.
Let's do this for the others as well.
As you see now, both orders are shipped.
Let's look into MySQLWorkbench finally, real quick.
So I can now select the rows here
and then see my two orders,
and if I look into order items,
I see the items for those two orders,
with the relationship as you see here,
with the order ID that shows to which order they belong,
and with that our little shopping cart system
is complete as well.