WEBVTT

00:00.020 --> 00:02.390
So I hope you gave this small challenge a go.

00:02.390 --> 00:07.280
And the goal is just to populate the code in both of these different methods.

00:07.310 --> 00:09.830
We'll start off in the calculate delivery fee.

00:09.860 --> 00:11.270
Pretty straightforward this one.

00:11.270 --> 00:19.910
We just need to return the subtotal check to see if this is greater than 10,000 which represents $100.

00:20.030 --> 00:22.850
If it is then we return zero.

00:22.880 --> 00:27.110
Otherwise we return 500 which equates to $5.

00:27.110 --> 00:28.970
So that one's pretty straightforward.

00:28.970 --> 00:32.300
The next one in the create order items.

00:33.500 --> 00:39.470
This one slightly trickier, but we'll create a variable to store our list of order items in.

00:39.470 --> 00:48.110
So we'll say var order items is a new list of type order item and add parentheses there.

00:49.010 --> 00:53.360
And we're passing in as a parameter here our list of items.

00:53.360 --> 00:59.360
So we need to loop over each of the items in that list in order to create each order item.

00:59.360 --> 01:00.770
So that's going to look like this.

01:00.770 --> 01:02.750
So we'll use a for each statement.

01:02.750 --> 01:07.220
And we can say var item in items that we pass in as a parameter.

01:08.150 --> 01:12.740
Now, one thing that we haven't talked about yet, which I understand if you missed this because I didn't

01:12.740 --> 01:20.330
even give a hint about this, it'd be a good idea to check to see if we have the quantity in stock of

01:20.330 --> 01:22.310
the item they wish to purchase.

01:22.310 --> 01:27.860
So we'll do a check inside here, and we're going to check to see if item dot product dot quantity in

01:27.860 --> 01:30.920
stock is less than the item dot quantity.

01:30.950 --> 01:38.900
Then at this point we will return null and in order to return null from this method, we would need

01:38.900 --> 01:40.610
to make this optional.

01:41.540 --> 01:47.360
So if you didn't have this check in there now, then please add this check as I'm going through this

01:47.360 --> 01:48.290
solution.

01:48.440 --> 01:51.200
The next thing we'll do is create the order item.

01:51.200 --> 01:55.430
So I'm going to say var order item equals new order item.

01:55.730 --> 02:03.470
And inside here we can say item ordered equals and then use new product item ordered.

02:03.980 --> 02:09.110
And inside here we can specify the product ID is equal to the item dot product id.

02:09.890 --> 02:17.160
The picture URL is equal to the item dot picture URL or product dot picture URL.

02:17.190 --> 02:22.830
I should say, and the name is going to be equal to the item dot product dot name.

02:22.830 --> 02:24.180
And then we can add a comma.

02:24.180 --> 02:28.830
We can set the price and set this equal to the item dot product dot price.

02:29.370 --> 02:36.990
The quantity we can set to the item dot quantity and then add a semicolon after this.

02:36.990 --> 02:42.120
And then we can add to the order items we created above.

02:42.150 --> 02:51.900
We can specify order items dot adds, and we can pass in the order item we have just created.

02:52.920 --> 02:56.520
And then we can also again I didn't mention the stock.

02:56.550 --> 02:59.130
So bonus points if you did get this bit.

02:59.340 --> 03:00.840
If not, please just add it.

03:00.840 --> 03:04.200
Now we're going to set the quantity in stock for the item.

03:04.200 --> 03:07.350
So we'll say item dot products, dot quantity and stock.

03:07.350 --> 03:12.060
And then we're going to deduct the item quantity from this.

03:12.060 --> 03:15.300
So we'll say minus equals item dot quantity.

03:15.990 --> 03:20.910
And then we can return the order items.

03:21.480 --> 03:30.690
And we've got a warning inside here, and that will be due to the optional nature of these items.

03:30.690 --> 03:36.420
So we need to add a defensive check here where we create the order items.

03:36.420 --> 03:39.690
And we're going to check to see if the items is equal to null.

03:39.720 --> 03:50.190
Then we're going to return a bad request and say some items out of stock okay.

03:50.190 --> 03:57.660
So that covers the code that we need in our orders controller for the order creation.

03:57.660 --> 03:59.730
Quite a chunky bit of code to do this.

03:59.730 --> 04:04.260
So what we'll take a look at next is testing our ability to create an order.

04:04.260 --> 04:05.940
And we'll see how it goes.

04:05.940 --> 04:14.430
And based on what we know from what we did earlier regarding the use of Dtos and how Entity Framework

04:14.730 --> 04:15.240
or our.

04:15.390 --> 04:21.690
Net controllers serialize the data, we may well run into problems with how we've configured our entities.

04:21.690 --> 04:27.840
So we'll test the order creation, and then we'll take a look at shaping the data if we need to based

04:27.840 --> 04:30.390
on what we return from these different endpoints.

04:30.390 --> 04:31.890
And that's coming up next.
