All right.
So now that we have all the functions here for our order controller, basically just dummy responses,
we're going to start to work on these in the first one I want to do is to create a new order.
So let's get rid of this and we're going to have to get a lot of stuff from the body because that's
what's going to come in through the front end.
So first off, we're going to get the order items, which will be an array of orders, shipping address,
payment method.
We want the items price tax, price, shipping price and total price.
So we're going to get that stuff from the body of the Http request.
Then we want to check to see if there is an order items array, but it's empty and if it is, then we
want to respond with a 400.
So let's say res dot status and we're going to do a 400 and let's say dot Json.
Actually, no, we don't need to do that because we have our error handler.
So just we'll set the status and then we'll throw a new error and say no order items.
Okay.
Else So if there are order items, then we want to proceed.
So we're going to create a new actually I'll go ahead and put this in here.
But order items, we don't want to just put that directly in because if we look at our model, our order
model, we have in order items, this stuff is going to come through right through the front end.
But this isn't the product, which is just the ID of the item for each item.
So we have to add this onto order item.
So what we can do is set this to an object, right?
Actually, no, we don't want to set it to an object.
We want to map through this.
So order items and then we're going to map through that.
And let's say for each order, we'll call it X and we want to return an object from this.
So we're going to use parentheses and then our curly braces and we're going to spread across whatever
that is in that order, which will be this stuff right here, these four fields.
We need to add this product.
So let's say product.
And all that's going to be is the object ID, which we can get from X dot underscore ID.
Okay.
And then we're going to set the ID field, underscore ID to undefined because we don't need that in
here.
Right?
We just have the product, which will be the ID, There's no underscore ID field in here, so we're
setting that to undefined.
Okay.
So just a little bit of preparation for the order items before we actually submit to the database.
Now, this will create the order and just put it in this variable.
It's not going to get saved yet.
So we want to do that.
Actually, it already got added here.
So yeah, we want to just await on order dot save, put the the order into this variable and then respond
with a 201 and pass in that new created order.
And that should do it for that route.
Now let's do a few of these other ones.
So to get my orders, that's actually going to be pretty easy so we can get rid of that and we'll say
const orders and we're going to set that to await and then order find.
And we just want to match the user to whatever user is logged in, okay?
And then we're just going to respond.
So res dot status and respond with a 200 and then dot Jason and pass in the orders.
Okay.
And then the other one I want to do is get order by ID.
We're not going to do these update ones just yet, so we're not going to do the admin stuff, but for
getting the order by ID, let's do that.
So we'll say const order and we want to await on order find by ID pass in the id which is in the URL.
So this here we can get with that.
And then I also want to add the user, the user name and email to the order and that's not stored in
the order collection.
So what we can do is we can say dot populate and we're going to say we want to populate from the user
collection and we want to populate the name and email fields.
Okay.
So again, this is going to find the order by ID, everything that's in the orders collection for that
particular order.
And then it's also going to populate the user's name and email.
So then we'll check for the order.
And if we have an order, then we're going to respond with that order.
Else.
Uh, else.
Then let's set a status.
So raise dot status and we're going to do a 404.
Okay.
And then we'll throw an error of order not found.
And that's it.
So we'll go ahead and save that.
And now what we'll do is on the front end, we'll consume these these end points and we'll be able to
create a new order.