So I hope you gave this small challenge a go.
And the goal is just to populate the code in both of these different methods.
We'll start off in the calculate delivery fee.
Pretty straightforward this one.
We just need to return the subtotal check to see if this is greater than 10,000 which represents $100.
If it is then we return zero.
Otherwise we return 500 which equates to $5.
So that one's pretty straightforward.
The next one in the create order items.
This one slightly trickier, but we'll create a variable to store our list of order items in.
So we'll say var order items is a new list of type order item and add parentheses there.
And we're passing in as a parameter here our list of items.
So we need to loop over each of the items in that list in order to create each order item.
So that's going to look like this.
So we'll use a for each statement.
And we can say var item in items that we pass in as a parameter.
Now, one thing that we haven't talked about yet, which I understand if you missed this because I didn't
even give a hint about this, it'd be a good idea to check to see if we have the quantity in stock of
the item they wish to purchase.
So we'll do a check inside here, and we're going to check to see if item dot product dot quantity in
stock is less than the item dot quantity.
Then at this point we will return null and in order to return null from this method, we would need
to make this optional.
So if you didn't have this check in there now, then please add this check as I'm going through this
solution.
The next thing we'll do is create the order item.
So I'm going to say var order item equals new order item.
And inside here we can say item ordered equals and then use new product item ordered.
And inside here we can specify the product ID is equal to the item dot product id.
The picture URL is equal to the item dot picture URL or product dot picture URL.
I should say, and the name is going to be equal to the item dot product dot name.
And then we can add a comma.
We can set the price and set this equal to the item dot product dot price.
The quantity we can set to the item dot quantity and then add a semicolon after this.
And then we can add to the order items we created above.
We can specify order items dot adds, and we can pass in the order item we have just created.
And then we can also again I didn't mention the stock.
So bonus points if you did get this bit.
If not, please just add it.
Now we're going to set the quantity in stock for the item.
So we'll say item dot products, dot quantity and stock.
And then we're going to deduct the item quantity from this.
So we'll say minus equals item dot quantity.
And then we can return the order items.
And we've got a warning inside here, and that will be due to the optional nature of these items.
So we need to add a defensive check here where we create the order items.
And we're going to check to see if the items is equal to null.
Then we're going to return a bad request and say some items out of stock okay.
So that covers the code that we need in our orders controller for the order creation.
Quite a chunky bit of code to do this.
So what we'll take a look at next is testing our ability to create an order.
And we'll see how it goes.
And based on what we know from what we did earlier regarding the use of Dtos and how Entity Framework
or our.
Net controllers serialize the data, we may well run into problems with how we've configured our entities.
So we'll test the order creation, and then we'll take a look at shaping the data if we need to based
on what we return from these different endpoints.
And that's coming up next.