WEBVTT

00:03.730 --> 00:05.230
Hey, did everyone taste here?

00:05.230 --> 00:09.460
And in this video we're going to go through and work with the controllers for our orders.

00:09.490 --> 00:13.860
Now, the only difficult part was the order was just the schema design.

00:13.870 --> 00:16.330
It's really a long and lengthy schema, and that's it.

00:16.330 --> 00:20.620
The rest of the things are pretty easy and in fact we have done so much of the work now things should

00:20.620 --> 00:22.770
be looking absolutely easy.

00:22.780 --> 00:27.430
So let's go into the controller, let's create a new file and I'm going to call this one as simply order

00:27.430 --> 00:29.740
controller dogs.

00:29.740 --> 00:35.290
Of course, we will be working on the routes and other things, but first let's finish up all of our

00:35.290 --> 00:39.730
control and in fact, majority of the one that we have and then we're going to set up routes for them.

00:39.730 --> 00:43.420
So the first thing is I would like to go ahead and bring the model of the order itself.

00:43.420 --> 00:44.590
So let's go ahead and do that.

00:44.590 --> 00:46.990
That is a pretty simple stuff, pretty ordinary stuff.

00:46.990 --> 00:48.280
We have done it many times.

00:48.280 --> 00:52.780
One directory back inside the models, we have the order, there we go.

00:52.840 --> 00:56.710
We are also going to need the model of the product as well.

00:56.710 --> 01:01.900
So let's go ahead and do that because there is going to be a time when receiving once we have received

01:01.900 --> 01:05.590
the order, we need to change something into the product model, which is the stock.

01:05.590 --> 01:07.210
So for that, we are going to need this.

01:07.210 --> 01:14.440
So let's go ahead and import product and the product is going to be coming from the model, which is

01:14.440 --> 01:14.980
product.

01:14.980 --> 01:15.730
There we go.

01:16.720 --> 01:20.620
Apart from this, let's go ahead and borrow some stuff from the payment controller.

01:21.100 --> 01:21.460
Yeah.

01:21.460 --> 01:27.580
So big promise we can borrow from here and again just saving some time and I guess that's pretty much

01:27.580 --> 01:27.730
it.

01:27.730 --> 01:29.470
That's all what we need in this case.

01:29.920 --> 01:31.990
Okay, let's go ahead and export a few methods.

01:31.990 --> 01:36.580
So let's go ahead and first say export DOT and this one is going to be create order.

01:36.580 --> 01:37.870
So let me just walk you through again.

01:37.870 --> 01:42.550
So these are all the routes of the order that we have to work on with.

01:42.970 --> 01:47.530
So let me go ahead and minimize this, zoom out a little bit and there we go.

01:48.400 --> 01:50.740
So these are all the routes that we have to work on.

01:50.890 --> 01:55.780
So the first one is actually to create order that get single order, get the order for the log.

01:55.780 --> 01:58.360
Then user admin should be able to get all the orders.

01:58.360 --> 02:03.490
Admin should be able to update any particular one order admin should be able to delete one order.

02:03.490 --> 02:04.420
So pretty simple stuff.

02:04.420 --> 02:05.800
Nothing groundbreaking here.

02:06.070 --> 02:09.400
So let's go ahead and call this one as simply create order.

02:09.400 --> 02:11.080
Sounds a good name for this one.

02:11.440 --> 02:16.090
And we're going to say this wrap around everything inside the big promise just like that.

02:16.600 --> 02:19.060
And let's go ahead and use a sync.

02:19.660 --> 02:23.800
And for that we are going to say request, response and the next.

02:26.380 --> 02:27.190
And there we go.

02:27.190 --> 02:32.740
Now we're going to work on a whole lot of things are going to come to us from the request body.

02:32.740 --> 02:37.110
So let's go ahead and say, hey, we want to extract a lot of things that are going to come from request

02:37.110 --> 02:37.900
dot body.

02:37.900 --> 02:41.560
So let's go ahead and see that how many things are going to come in.

02:41.560 --> 02:48.160
So for this, I would like to split my editor into this one and I would like to open up different file

02:48.160 --> 02:50.880
into this one, which is going to be model and ordered on.

02:50.890 --> 02:56.200
JS Okay, so this will help me to actually grab the things a little bit easier from here.

02:56.230 --> 02:59.080
Let's go ahead and see how many of the items are going to come here.

02:59.080 --> 03:02.980
So first is a shipping info, so I'm going to go ahead and say shipping info.

03:03.670 --> 03:05.560
This will be coming up from the request body.

03:05.560 --> 03:07.450
Yes, the entirety of the giant object.

03:07.450 --> 03:11.590
I expect somebody to pass me from the front and then comes up is the user.

03:11.590 --> 03:13.750
User will not come from the body itself.

03:13.750 --> 03:18.910
We need to access the user from request to user our middleware, other items.

03:18.910 --> 03:19.990
Yes, I expect this one.

03:19.990 --> 03:21.250
So I can actually copy this.

03:21.250 --> 03:23.020
Yeah, that's actually much more easier.

03:23.350 --> 03:25.750
So order item is going to come from here.

03:26.110 --> 03:30.520
Then we have this payment info that also should come up from the front end because somebody guy who

03:30.520 --> 03:36.610
has requested first the payment from the back end, then once the payment is successful, he should

03:36.610 --> 03:40.390
receive that data and then he should make a request for placing the order.

03:40.390 --> 03:43.510
So we expect that this payment info also should come from there.

03:43.510 --> 03:48.880
This is the kind of architecture or the flow of the application that we have planned up to X amount.

03:48.880 --> 03:52.900
Yes, you should give it to me and then the shipping amount.

03:52.930 --> 03:55.450
Of course, I expect this to come from the front end.

03:56.740 --> 04:04.450
And apart from this then we have total amount also expected to come from the front end and forgot a

04:04.450 --> 04:04.770
comma.

04:04.780 --> 04:05.470
There we go.

04:06.400 --> 04:10.660
And then order status has a default value so I'm not bothered about that part.

04:10.660 --> 04:13.960
Deliver that this is also going to have a date.

04:14.440 --> 04:17.020
So this is not going to be filled as of now.

04:17.020 --> 04:18.360
This is also not a required field.

04:18.370 --> 04:23.470
Once the delivery has been done, then we are going to change this field created that is automatically

04:23.470 --> 04:24.340
going to have the value.

04:24.340 --> 04:24.850
So default.

04:24.850 --> 04:28.960
Now the only thing that's remaining is that how we are going to process this user.

04:30.310 --> 04:31.480
Now that's it.

04:31.480 --> 04:38.170
We can actually close this guys and we can actually save this one up here and then we can close this

04:38.170 --> 04:38.440
one.

04:39.100 --> 04:44.410
So now we have extracted on the information from the order request body as well.

04:44.440 --> 04:45.910
Now let's go ahead and create an order.

04:45.910 --> 04:51.430
So for creating the order, we are going to simply use order, create simple method and pass on a big

04:51.430 --> 04:53.020
gigantic object into it.

04:53.140 --> 04:58.870
The object is going to look like this, so all the values are going to just pass it on like this.

04:59.080 --> 05:01.900
The only value that is now here is the user.

05:01.900 --> 05:03.460
So let's go ahead and say user.

05:03.460 --> 05:06.250
The user is going to come from request dot user.

05:06.250 --> 05:09.160
User is the entire object that we have injected in the middleware.

05:09.160 --> 05:13.540
So I have to say dot ID is a dot ID or underscore ID.

05:13.570 --> 05:18.430
No, this is going to be a dot underscore ID because this user object is coming up from the MongoDB

05:18.430 --> 05:18.850
itself.

05:18.850 --> 05:20.290
So just testing you out.

05:20.410 --> 05:22.000
Okay, so this is all done.

05:22.000 --> 05:25.870
And if the order was successfully created, let's go ahead and hold this order into available.

05:25.870 --> 05:28.150
So we're going to call this one as simply order.

05:28.180 --> 05:29.740
That is going to be a wait.

05:29.740 --> 05:32.590
There we go further down the road.

05:32.590 --> 05:35.470
We just need to send the response or rest dot status.

05:35.470 --> 05:39.250
They should be looking all like I can do it very, very familiar stuff now.

05:39.370 --> 05:42.310
And that is a kind of a win for me.

05:42.730 --> 05:42.970
Okay.

05:43.000 --> 05:48.130
So what we're going to do is, again, success is going to be true and we are going to send order as

05:48.130 --> 05:48.520
a test.

05:48.520 --> 05:49.300
That's it.

05:49.300 --> 05:51.010
So told you, it's really simple.

05:51.010 --> 05:56.500
Now, one thing I would like to mention here, which is very, very crucial to you, that you might

05:56.500 --> 06:00.970
want to test this out from the postman itself, but there is a thing that you need to actually kind

06:00.970 --> 06:06.010
of walk through if you are testing it with the postman or even from the front end documentation.

06:06.010 --> 06:10.960
So when I'm saying this order items, let me open this order item and show you something very, very

06:10.960 --> 06:11.560
tricky.

06:11.650 --> 06:13.650
So order item and I open this up.

06:13.660 --> 06:17.650
This is a product of mongoose dot schema, dot object ID.

06:17.680 --> 06:22.480
So what is going to happen when you are going to try to build this array of order items?

06:22.480 --> 06:26.590
You might be tempted in the postman itself that, Hey, where is the postman?

06:26.590 --> 06:29.590
This is the postman that you might be creating a new route.

06:29.590 --> 06:33.610
And inside this, let me just go ahead and add a sample one here.

06:33.610 --> 06:40.060
So just like this and inside the body, raw JSON data.

06:40.060 --> 06:45.910
So you might be saying, hey, this is my order items, let me go ahead and use this.

06:46.690 --> 06:51.640
And codes, order my gain lowercase order items.

06:53.800 --> 06:56.080
And inside this, this is going to be an array.

06:56.080 --> 07:01.060
So you might be tempted to put up all the values like this and then you might be thinking, okay, so

07:01.060 --> 07:04.570
this order item is a big gigantic object which has name and all of that.

07:04.570 --> 07:07.420
So let's place the name and then I like to place the product.

07:07.690 --> 07:12.490
So you'll come here and say, Hey, I would like to add a name here, so go ahead and say this name

07:12.790 --> 07:15.730
and something like this and say test name.

07:15.730 --> 07:19.240
Then you might be thinking, okay, I need to now add the product ID as well.

07:19.240 --> 07:24.940
So you'll put up a comma and you will say product ID or just the product itself.

07:24.940 --> 07:30.520
And this expects you to pass on an object ID and you might be very tempted to just grab something like

07:30.520 --> 07:31.330
get all products.

07:31.330 --> 07:35.500
I can grab order ID from here, copy it up here and paste it up here.

07:35.530 --> 07:40.360
Now, although in majority of the cases this is going to work, but this is not truly the correct way

07:40.360 --> 07:41.260
of handling the things.

07:41.260 --> 07:45.940
If you are going to do and add things like this, your order is not going to be truly searchable because

07:45.940 --> 07:50.500
this is a string, although your mongoose is going to accept that and is going to probably put this

07:50.500 --> 07:55.210
thing because we haven't done a proper check of checking whether this is a valid mongoose ID or not.

07:55.210 --> 08:01.720
But this notice here it says object ID and then inside the parenthesis they are saying this is a long

08:01.720 --> 08:02.530
string number.

08:02.530 --> 08:03.730
So there is a difference.

08:03.730 --> 08:06.820
I have repeatedly said that this is not entirely adjacent data.

08:06.820 --> 08:11.320
This is a based on data, based on gives you some of the additional features and functionality.

08:11.320 --> 08:13.690
So testing of this is really, really tricky one here.

08:13.690 --> 08:18.040
So that's why I'm not going to do this right now, but I'll just want to keep this information with

08:18.040 --> 08:18.160
you.

08:18.160 --> 08:23.830
That passing just the JSON is not the way this is an object ID and object ID should be treated differently

08:23.830 --> 08:24.970
as compared to the JSON.

08:24.970 --> 08:29.170
This is a baseline, so just keep that in mind and that's all is expected as of now.

08:29.170 --> 08:34.660
Already we have done way above that what should be done in a in a kind of a beginner friendly course.

08:34.660 --> 08:35.410
So there we go.

08:35.440 --> 08:36.820
Your create order is all done.

08:36.820 --> 08:39.940
Let's go ahead and create another method in the next video.
