Hey, did everyone taste here?
And in this video we're going to go through and work with the controllers for our orders.
Now, the only difficult part was the order was just the schema design.
It's really a long and lengthy schema, and that's it.
The rest of the things are pretty easy and in fact we have done so much of the work now things should
be looking absolutely easy.
So let's go into the controller, let's create a new file and I'm going to call this one as simply order
controller dogs.
Of course, we will be working on the routes and other things, but first let's finish up all of our
control and in fact, majority of the one that we have and then we're going to set up routes for them.
So the first thing is I would like to go ahead and bring the model of the order itself.
So let's go ahead and do that.
That is a pretty simple stuff, pretty ordinary stuff.
We have done it many times.
One directory back inside the models, we have the order, there we go.
We are also going to need the model of the product as well.
So let's go ahead and do that because there is going to be a time when receiving once we have received
the order, we need to change something into the product model, which is the stock.
So for that, we are going to need this.
So let's go ahead and import product and the product is going to be coming from the model, which is
product.
There we go.
Apart from this, let's go ahead and borrow some stuff from the payment controller.
Yeah.
So big promise we can borrow from here and again just saving some time and I guess that's pretty much
it.
That's all what we need in this case.
Okay, let's go ahead and export a few methods.
So let's go ahead and first say export DOT and this one is going to be create order.
So let me just walk you through again.
So these are all the routes of the order that we have to work on with.
So let me go ahead and minimize this, zoom out a little bit and there we go.
So these are all the routes that we have to work on.
So the first one is actually to create order that get single order, get the order for the log.
Then user admin should be able to get all the orders.
Admin should be able to update any particular one order admin should be able to delete one order.
So pretty simple stuff.
Nothing groundbreaking here.
So let's go ahead and call this one as simply create order.
Sounds a good name for this one.
And we're going to say this wrap around everything inside the big promise just like that.
And let's go ahead and use a sync.
And for that we are going to say request, response and the next.
And there we go.
Now we're going to work on a whole lot of things are going to come to us from the request body.
So let's go ahead and say, hey, we want to extract a lot of things that are going to come from request
dot body.
So let's go ahead and see that how many things are going to come in.
So for this, I would like to split my editor into this one and I would like to open up different file
into this one, which is going to be model and ordered on.
JS Okay, so this will help me to actually grab the things a little bit easier from here.
Let's go ahead and see how many of the items are going to come here.
So first is a shipping info, so I'm going to go ahead and say shipping info.
This will be coming up from the request body.
Yes, the entirety of the giant object.
I expect somebody to pass me from the front and then comes up is the user.
User will not come from the body itself.
We need to access the user from request to user our middleware, other items.
Yes, I expect this one.
So I can actually copy this.
Yeah, that's actually much more easier.
So order item is going to come from here.
Then we have this payment info that also should come up from the front end because somebody guy who
has requested first the payment from the back end, then once the payment is successful, he should
receive that data and then he should make a request for placing the order.
So we expect that this payment info also should come from there.
This is the kind of architecture or the flow of the application that we have planned up to X amount.
Yes, you should give it to me and then the shipping amount.
Of course, I expect this to come from the front end.
And apart from this then we have total amount also expected to come from the front end and forgot a
comma.
There we go.
And then order status has a default value so I'm not bothered about that part.
Deliver that this is also going to have a date.
So this is not going to be filled as of now.
This is also not a required field.
Once the delivery has been done, then we are going to change this field created that is automatically
going to have the value.
So default.
Now the only thing that's remaining is that how we are going to process this user.
Now that's it.
We can actually close this guys and we can actually save this one up here and then we can close this
one.
So now we have extracted on the information from the order request body as well.
Now let's go ahead and create an order.
So for creating the order, we are going to simply use order, create simple method and pass on a big
gigantic object into it.
The object is going to look like this, so all the values are going to just pass it on like this.
The only value that is now here is the user.
So let's go ahead and say user.
The user is going to come from request dot user.
User is the entire object that we have injected in the middleware.
So I have to say dot ID is a dot ID or underscore ID.
No, this is going to be a dot underscore ID because this user object is coming up from the MongoDB
itself.
So just testing you out.
Okay, so this is all done.
And if the order was successfully created, let's go ahead and hold this order into available.
So we're going to call this one as simply order.
That is going to be a wait.
There we go further down the road.
We just need to send the response or rest dot status.
They should be looking all like I can do it very, very familiar stuff now.
And that is a kind of a win for me.
Okay.
So what we're going to do is, again, success is going to be true and we are going to send order as
a test.
That's it.
So told you, it's really simple.
Now, one thing I would like to mention here, which is very, very crucial to you, that you might
want to test this out from the postman itself, but there is a thing that you need to actually kind
of walk through if you are testing it with the postman or even from the front end documentation.
So when I'm saying this order items, let me open this order item and show you something very, very
tricky.
So order item and I open this up.
This is a product of mongoose dot schema, dot object ID.
So what is going to happen when you are going to try to build this array of order items?
You might be tempted in the postman itself that, Hey, where is the postman?
This is the postman that you might be creating a new route.
And inside this, let me just go ahead and add a sample one here.
So just like this and inside the body, raw JSON data.
So you might be saying, hey, this is my order items, let me go ahead and use this.
And codes, order my gain lowercase order items.
And inside this, this is going to be an array.
So you might be tempted to put up all the values like this and then you might be thinking, okay, so
this order item is a big gigantic object which has name and all of that.
So let's place the name and then I like to place the product.
So you'll come here and say, Hey, I would like to add a name here, so go ahead and say this name
and something like this and say test name.
Then you might be thinking, okay, I need to now add the product ID as well.
So you'll put up a comma and you will say product ID or just the product itself.
And this expects you to pass on an object ID and you might be very tempted to just grab something like
get all products.
I can grab order ID from here, copy it up here and paste it up here.
Now, although in majority of the cases this is going to work, but this is not truly the correct way
of handling the things.
If you are going to do and add things like this, your order is not going to be truly searchable because
this is a string, although your mongoose is going to accept that and is going to probably put this
thing because we haven't done a proper check of checking whether this is a valid mongoose ID or not.
But this notice here it says object ID and then inside the parenthesis they are saying this is a long
string number.
So there is a difference.
I have repeatedly said that this is not entirely adjacent data.
This is a based on data, based on gives you some of the additional features and functionality.
So testing of this is really, really tricky one here.
So that's why I'm not going to do this right now, but I'll just want to keep this information with
you.
That passing just the JSON is not the way this is an object ID and object ID should be treated differently
as compared to the JSON.
This is a baseline, so just keep that in mind and that's all is expected as of now.
Already we have done way above that what should be done in a in a kind of a beginner friendly course.
So there we go.
Your create order is all done.
Let's go ahead and create another method in the next video.