Okay, So now we're going to be moving to the back end because we need to work with orders and we don't
have we don't even have an orders controller yet.
So what I want to do is the same thing we did with users where we just added all of these methods and
we added all the routes and just just responded with a string for all of them just to just to get them
there.
And then we'll start to work one by one.
So let's create a new file in the back End Routes folder and we're going to call this order Routes Dot.
JS Okay, so that will be our routes.
And then in controllers, let's create an order.
Controller.
JS And we'll start off with the controller.
So I'm going to import actually let's copy from the product controller, just the first function here
and the imports.
And because we want the async handler now, we want the order.
Order model.
So let's say models slash order model.
And for this first one here, this is going to be to create a new order.
So let's change this to create new order.
And the route is going to be a post request to API slash orders and this is going to be private.
So this is the route that we're going to hit when we go through the you know, we go through here and
we go through checkout and we place an order.
It's going to create one.
So let's change the name of this function to, let's say, create order.
And or let's call it.
We'll call it ad.
Order items.
Because it can be more than one.
So we'll say add order items and let's see, we'll get rid of this and we'll just we're just going to
respond.
So res dot send we're just going to respond with.
Add.
Order items for now.
Okay, so that's the first one.
So let's copy that.
And the next one is going to be to get the logged in users orders, because on the profile we want to
show the orders that the user made.
So we'll say get logged in.
Users orders.
And that's going to be.
Get request.
So we'll say get.
And it's going to be API slash orders, slash my orders and we'll be able to get the ID through the
token that's in the cookie.
So let's call this one, get my orders and we'll just respond for now with get my orders.
Okay, Then we're going to get we're going to have the get order by ID, So let's paste that in and
let's say this is going to get.
Order by D and that's going to be a get request.
Two API orders slash and then whatever the ID, it's going to be private.
All of these are going to be private.
And then let's change the name of the function to get order by ID.
And then we'll just have a string here.
That says Get order by D.
All right.
So the next one is going to be update order to paid because when the order is created, it's not going
to be the paid value.
If we look at the model.
You can see that we have a is paid.
So that's false by default.
But when we go through when we place an order, we're going to want to at some point mark it as paid.
So that's what this is going to do, let's say update.
Order to paid and it's going to be a.
Let's see.
It's going to be it's actually going to be a get request.
And it's going to be two API orders and then the ID, so colon ID and then slash pay.
It's going to be private.
And let's make the function name.
Call it update.
Update order to paid.
And then in here we'll just say update order to paid.
And then we also want want to update to delivered.
So basically we're going to be able to as an admin, mark it as shipped and delivered.
So let's copy that and we'll say update to delivered and it's going to be get request to API orders.
Deliver.
Private and we'll say update order to delivered.
And yeah, we'll just change this.
Delivered.
Okay.
And then the last one we're going to have is just to get all orders.
So let's say description.
Get all orders.
Now this is actually going to be admin and so is so is to update to delivered.
So we'll say private slash admin.
And then for get all orders, that's just going to be a get request to API orders.
That's also going to be admin.
Because we don't want regular users to see other people's orders.
So let's say get orders.
It should be uppercase, and we'll just.
Have this say.
Get all orders.
All right, Now we just want to export all this.
So add order that that.
Oops.
That, that and get orders.
Okay.
So that should do it for the functions.
Now let's go into order routes and now we want to let's see, how should we do this?
Let's copy.
We'll copy the user routes because there's a few things we need.
That's also in there, like the the router.
The the middleware.
We're going to want that now.
We want to bring in our controller stuff from the order controller.
And what I'm going to do is just copy these.
And put those in here because those are the ones we want to bring in.
And then let's see for the routes.
We're going to have.
See.
So for the index, the post request is going to be to add an order item.
So let's change this to add.
Order items, and we're going to want that to be just for registered users.
So we're going to add the protect middleware in front of it.
Okay.
Now the.
Um, get request is going to be to get all orders, which is an admin function.
So we already have right here protect and admin.
We just want to change the name of the function to get orders.
Okay, Now, next we're going to do actually, you know what?
Let's just get rid of the rest of these and then we'll just type out the rest that we need.
So for the.
To get my orders.
We're going to have the route B slash mine.
And yeah, we need protect middleware and then call get my orders.
Okay.
Next we're going to do for the ID If it's a get request, we want to get the order by ID.
So that should actually be.
I believe that should be for.
Yeah, that should be admin.
So let's do protect and admin middleware.
And yeah, get order by D, then we're going to have our pay.
So it's going to be the ID and then pay and it's going to be a put request and protect and then update
order to paid.
And then finally we have the deliver which is going to be protect and admin and update order to delivered.
All right.
So now we just want to check and see if these actually work.
But before we do that, we have to go to our server.js.
And just like we did with the other routes, we have to bring in the order routes.
So autoroutes change this.
To order.
And we also want to add right here.
Bring that down.
Change that to order routes and API.
Orders.
Okay, so let's try this out.
We're going to open up, Postman.
And I'm just going to add these as we go, right?
So I'm going to create a new a new collection here.
Let's see, how do we do this?
Plus new collection.
And let's call this orders.
Okay.
And then in orders, let's create a new.
Let's see.
What do we want to start with here?
Let's create a new request.
And this is going to be to create a new order.
So let's say create new order.
And it's going to be a post request and the URL is going to be our base URL.
Slash orders.
And that's it.
Just slash orders.
So let's send that and we see add order items, which is what we want.
So let's save that.
We know that works.
Thatrillionoute.
Now let's create another request.
So ad request.
So this one is going to be to get the logged in users orders.
So get logged in user orders and that's going to be a get request to let's see, it's going to be our
base URL.
Slash orders.
And it's going to be what is the root slash my orders.
Okay so it says not authorizes admin, which is right.
That's good because.
Well wait a minute.
To get my orders.
That shouldn't be an admin.
Uh, let's see.
Let me go back.
That should just be regular protect.
So get my orders.
Hmm.
You know what?
This.
I'm not sure why.
This is doing this.
So we have router dot route slash.
We have our post and then.
Oh it's mine.
That's why it's not my orders slash mine.
There we go.
All right.
So we'll save that route.
Or thatrillionequest.
Let's make a new request and let's see this one.
What is this one going to be?
Let's see.
Let's do get order by ID which.
Is that admin?
Get order by D.
Yeah, that's admin.
So in orders I'm going to create a folder.
Just like I did with with users.
And let's call this admin.
Just to differentiate what routes are actually admins.
So let's see in here.
Admin Let's add a request and say get order by ID.
It's going to be a get request to base URL.
Slash orders slash and then the ID.
Now, I don't have an ID right now, so I'm just going to put in.
Um.
Let's see.
Yeah, we'll just put a one for now.
And if I send that.
Hmm.
Unauthorized is admin.
Oh, yeah, I guess that makes sense.
We're not logged in as admin.
We are logged in, though I should have mentioned that in the beginning that you need to be logged in
for most of these.
So what I'll do is log in as admin.
So over here I'm going to go.
I'm going to log out.
And then I'm going to auth user.
And change this.
To admin.
So now if I go back to get order by ID and we look at the body that works.
So we'll save that.
Okay, so let's see.
What else do we have?
For orders, we have update order to paid which.
Which is not admin?
I don't believe so.
Let's say add request.
And that's going to be a put put request.
We'll call it update order to paid.
And it's going to be, let's see, base URL slash orders, slash the ID and then slash pay.
We'll put one for the ID, send update or it's a paid good.
We'll save that request.
Then we're going to do update to delivered.
Which is an admin function.
So let's add a request here.
We'll say update to.
Delivered.
Put request.
Base URL slash orders.
Slash id slash deliver.
Send that.
That works.
Save.
And then we have get orders, which is admin.
So let's say add request.
Say get all orders.
Get requests to the base URL.
Slash orders.
Send that and we'll save.
Okay, so now we have all the controller methods.
Our controller functions hook to the roots.
And then in Postman, we have all of our routes all set up.
Okay, so now what we'll do in the next video is make it so that we can actually create a new order.