WEBVTT

00:00.020 --> 00:00.260
Okay.

00:00.290 --> 00:03.020
Our next task is to create the order itself.

00:03.020 --> 00:10.190
And to help us with this, we'll create an create order DTO so that we can take the information we need

00:10.190 --> 00:11.930
from the client.

00:11.930 --> 00:13.730
And we'll create a new file in here.

00:13.730 --> 00:19.490
And it's going to be a class and we'll call it Create Order DTO.

00:19.610 --> 00:25.190
And what information do we need to get to be able to create an order.

00:25.190 --> 00:31.490
And we effectively need to if we go and take a look at the order, what do we need to get from the browser

00:31.490 --> 00:32.810
that we do not already have.

00:32.810 --> 00:34.640
So we're going to have the buyer email.

00:34.640 --> 00:37.640
The ID gets created for us, the shipping address, we don't know.

00:37.640 --> 00:39.080
That's something we will need.

00:39.110 --> 00:41.570
The order date we're initializing anyway.

00:41.570 --> 00:46.940
The order items we get from the basket, the subtotal, delivery fee and discount we can work out for

00:46.940 --> 00:53.840
ourselves the payment intent ID we can also get from the basket the order status we create, and the

00:53.840 --> 00:56.570
payment summary is something else that we need to get from the client.

00:56.570 --> 01:03.590
So the only things we need in our DTO is this payment summary and also the shipping address as well.

01:03.590 --> 01:08.750
That's something that we cannot Not ascertain without getting that information from the browser.

01:09.410 --> 01:11.630
So we'll have a couple of properties in here.

01:11.630 --> 01:14.300
One will be for the shipping address.

01:14.300 --> 01:19.760
And we'll get that from our order aggregates and we'll call it shipping address.

01:19.760 --> 01:25.070
And we'll also have another property in here for the payment summary.

01:25.100 --> 01:27.080
And we'll call it payment summary.

01:27.080 --> 01:30.290
And we'll make both of these required.

01:33.710 --> 01:34.520
Okay.

01:35.120 --> 01:39.080
And then we can go back to our orders controller.

01:39.080 --> 01:41.930
And we'll create the method to create a new order.

01:41.930 --> 01:45.500
So this is going to be an HTTP post.

01:45.980 --> 01:49.370
And we'll specify public async task.

01:49.760 --> 01:52.130
That's an action result.

01:52.130 --> 01:56.870
And we are going to return the order from this once it has been created.

01:56.870 --> 01:59.360
And we'll call it create order.

01:59.360 --> 02:04.790
And we'll pass up as its argument the Create Order DTO.

02:04.790 --> 02:07.670
And we'll just call this order DTO.

02:07.730 --> 02:12.740
So the first thing we'll do is we'll get hold of our basket, so we'll use our baskets.

02:13.580 --> 02:21.380
Or in fact, we can just use the basket ID because we've got inside our extension methods we created

02:21.380 --> 02:23.660
earlier basket extensions.

02:23.660 --> 02:31.520
And we have inside here a get basket with items method that guarantees we get the baskets or throws

02:31.520 --> 02:33.170
an exception if it cannot.

02:33.170 --> 02:35.990
So we'll use this extension methods here.

02:35.990 --> 02:45.320
So in our orders controller we'll use var basket equals and say await and context dot baskets.

02:45.320 --> 02:48.980
And we'll use the get basket with items method.

02:48.980 --> 02:55.310
And we need to pass in our basket ID which we can use the request dot cookies.

02:55.370 --> 02:59.420
And in square brackets we can specify the basket ID.

03:00.080 --> 03:03.290
So next we'll check to see if we have the basket.

03:03.290 --> 03:06.440
So we'll check to see if the basket is equal to null.

03:06.530 --> 03:07.430
Or.

03:07.460 --> 03:10.700
We'll also make sure there's items in the basket as well.

03:10.700 --> 03:17.090
And we'll make sure the basket dot items dot count is equal to zero.

03:17.150 --> 03:26.120
And if that is the case, then we're going to return a bad request and say basket is empty or not found.

03:26.990 --> 03:30.500
And below this we'll create a method to go and get our order items.

03:30.500 --> 03:35.480
So I'll save our items equals create order items.

03:35.480 --> 03:39.440
And we'll pass into this method the basket dot items.

03:39.830 --> 03:42.500
And we'll put our cursor where the arrow is.

03:42.530 --> 03:44.060
And we'll use the quick fix.

03:44.060 --> 03:47.780
And we'll generate the method for creating the order items.

03:47.780 --> 03:49.220
And we'll come back to that.

03:49.940 --> 03:53.270
Following this we will work out our subtotal.

03:53.630 --> 03:58.610
So the var subtotal equals items dot sum.

03:58.610 --> 04:04.130
And we don't have access to the sum yet because we need to specify what we're returning from our create

04:04.130 --> 04:05.660
order items method.

04:05.690 --> 04:08.540
So let's swap object here for a list.

04:11.090 --> 04:13.700
Of type order item.

04:14.420 --> 04:16.580
And then we can work out our subtotal.

04:16.580 --> 04:18.950
So this is going to be items dot.

04:18.950 --> 04:31.310
Start some and we'll use x at the arrow and x dot price multiplied by x dot quantity.

04:31.310 --> 04:33.710
And then we'll add the delivery fee.

04:33.710 --> 04:37.010
So we'll say var delivery fee equals.

04:37.160 --> 04:39.320
And we'll create a method for this as well.

04:39.320 --> 04:43.400
So we'll use calculate delivery fee.

04:43.700 --> 04:48.740
And we'll pass in the subtotal as its parameter.

04:48.740 --> 04:51.020
And we'll put a cursor inside here.

04:51.020 --> 04:54.140
And we'll generate this method as well.

04:54.140 --> 04:57.110
And this is going to return a long.

04:57.140 --> 05:02.780
And again we won't worry about populating the content of that method yet.

05:02.780 --> 05:05.180
And below this we can create our new order.

05:05.180 --> 05:08.000
So we'll say var order equals new order.

05:09.680 --> 05:17.750
And we'll specify our order items is equal to the items the buyer email will set equal to the user dot.

05:17.750 --> 05:19.310
Get username.

05:19.850 --> 05:24.990
The shipping address we can get from our order DTO Auto shipping address.

05:25.530 --> 05:29.220
The delivery fee will equal the delivery fee.

05:30.180 --> 05:33.180
The subtotal will equal the subtotal.

05:34.560 --> 05:43.560
The payment summary will get from our order DTO dot Payment Summary and the payment intent ID will get

05:43.560 --> 05:46.890
from the basket dot Payment intent ID.

05:49.140 --> 05:55.620
And then we can use our context dot orders dot add and pass in the order.

05:57.600 --> 06:07.860
And at this point we can remove our baskets from the dot net side or our back end side of things.

06:07.860 --> 06:13.560
So we'll use context dot baskets dot remove baskets.

06:14.310 --> 06:18.540
And we'll also as a backup delete the cookie as well from here as well.

06:18.540 --> 06:26.670
So we'll say response dot cookies dot delete and pass in the basket ID.

06:28.830 --> 06:32.160
And then we can save our changes into the database.

06:32.160 --> 06:38.010
So I'll say var results equals await context dot save changes async.

06:38.040 --> 06:44.550
And we'll make sure that is greater than zero changes that have been saved into our database.

06:44.940 --> 06:45.960
We'll check the results.

06:45.960 --> 06:58.020
And if say if not results then we will return a bad request and say problem creating order.

07:00.030 --> 07:03.270
And finally we can return the order.

07:03.270 --> 07:07.020
And because we're creating something we're creating a resource in our database.

07:07.020 --> 07:16.200
The correct response for this is a createdat action or created at root or a 201 created result is what

07:16.200 --> 07:17.910
gets them back to the browser.

07:17.910 --> 07:23.220
But we can use a method provided by dotnet called Createdat action.

07:23.220 --> 07:30.480
And for the location header that gets returned, we just want to point to where the client can find

07:30.480 --> 07:32.920
the recently created order.

07:33.190 --> 07:42.670
So we'll use name of and we'll use get order details as this is the method, the endpoint that would

07:42.670 --> 07:45.340
give us the specific order that's been created.

07:45.340 --> 07:48.460
And the order details takes a parameter.

07:49.210 --> 07:52.600
So we need to specify that inside here as the second argument.

07:52.600 --> 07:57.010
And we'll say new in curly brackets we'll use ID equals.

07:57.010 --> 08:00.460
And then we can use the order id as its parameter.

08:00.460 --> 08:04.270
And finally we can return the order from this method.

08:04.270 --> 08:08.530
And that will result in a 201 created being sent back to the browser.

08:08.830 --> 08:13.210
So we've got a couple of methods that don't have any logic inside them.

08:13.450 --> 08:15.610
And we'll call this a challenge.

08:15.610 --> 08:24.610
So your challenge is to populate what we need inside here for both of these different methods.

08:24.610 --> 08:27.340
And please give this a go.

08:27.340 --> 08:32.110
And in the next lesson I'll cover what it is step by step.

08:32.110 --> 08:37.900
What we need to do to get this functionality complete in this controller.
