Okay.
So our next step is to create the order entity itself.
So inside the order aggregates we'll create another new file another new class.
And this is going to be called order.
And inside this class we'll give it an int for its ID.
We'll also have a prop that's a string for the buyer email address.
We'll have a prop of type shipping address that's going to be called shipping address.
We'll have a prop that's a date time.
And we'll use the name of order date for this.
And we'll initialize this date to date time.
And we'll use Utcnow, which is a standard date format for anywhere in the world.
And when we return an object to our client browser with this type of date formatting, then our browser
will convert that into local time for us.
We'll also have a prop that's a list of type order item and we'll call this order items.
We'll have a prop that's a long fully subtotal of the order.
We'll also have a prop that's along for the delivery fee.
And even though we're not using it right now, we'll also add another long for the discount.
Something that's going to come a bit later.
And we'll also add a string for the payment intent ID.
And we'll also add the order status and call it order status.
And one more to go as far as properties goes.
Quite a few inside here.
We'll also have the payment summary and call it payment summary.
And we'll just have a helper method inside here.
And we'll use public long get total.
And we will return from this.
The subtotal plus the delivery fee minus the discounts.
And let's take a look at our required properties.
So where we've got things like the buyer email we definitely want that.
So that's going to be required.
And this is how we're going to identify which orders are related to a specific user.
So when it comes to fetching the user's orders then we've got a property that we can use for that.
The shipping address is going to be stored with the order.
And by using an owned property we've got an orders table.
And we'll have all of the properties for the shipping address inside the order table, not in a separate
table.
The order date is fairly self-explanatory.
We'll also have our list of items.
And because it's a list, we'll just initialize it with an empty list by giving it the square brackets.
We've got our subtotal, delivery fee and discounts.
And then we've got our payment intent ID.
Are we guaranteed to have this when we create the order?
In theory, we should have it from our baskets.
But I'm going to make this optional anyway, which is going to force us to be defensive in our code
and check for this before we try and use it anywhere.
So I'll make this optional just in case we do not have that.
The order status will give an initial value to order status dot pending.
When we first create an order and we'll also make the payment summary required when we create an order.
Because we should have that information because we're going to create the order after payment has been
made.
And then we've just got our get total function.
So we now have something new to add to our store context.
So I'll just close all of this at the top and we'll open up our store context.
And we will add inside here.
And the only thing we need to add inside here is the dbset of type order, which we get from order aggregates
and we're using the same names that stripe also has available.
So do be careful where you get this from and make sure it's coming from our own code, and we'll make
this required as well, just like the other ones above.
So what we should be able to do now is create a new migration and update our database schema.
So let's just go ahead and open a terminal.
We'll go back to our tab that's running the API.
We will need to stop our API at this point.
And I'll use dotnet f migrations ads.
And I'll just say order entity added as the name of the migration.
And this should work without any issues.
But we will see.
And that looks fine.
So let's go and take a look at what has been created inside the data folder inside migrations.
Then we've got our order entity added migration available.
Inside here we're creating a table called orders.
And notice what properties we have inside here we've got the normal things the SQLite autoincrement
for the ID, but then we've got the shipping address properties.
And because this is an owned property, it's owned by the orders table.
Then the shipping address gets added inline along with the other properties inside the order.
So we've got all of our shipping address properties there.
We've got our order date, the subtotal delivery fee, etc. and then we've got our payment summary details
again, because that's an owned property that appears inside the same table as the orders.
So that's the idea of that.
Inside here we've also got our order item table.
Again we've got an owned property for the product and the properties that we're storing inside there
as well as the other properties.
And that's it.
That looks good to me.
So what we should be able to do now is start our API.
And I'll just use dotnet watch to do that.
And hopefully this will start without any issues.
And again that looks fine to me.
So let's go and double check our database file.
And I'll just click on the store DB.
And what we should see now is that we've got our orders table along with all of its different columns,
including the owned properties.
And we've also got a table for our order item.
Now, I didn't make this pluralised, and in a way I wish I had because that's how I'd normally do it.
But to save a bit of time and the fact I know it's not going to cause any issues, I'm going to leave
that as it is.
But you've seen how to change a table name before.
We can give it the table attribute and then give it a table name, I'm going to say that's okay for
what we're doing in this demo, but if I did want to do that, I would need to give it the table name
property.
Then I would need to create a new migration, and then that would update the database schema accordingly.
But like I say, I'm not going to do that on this occasion.
So the next thing we need to think about is an orders controller.
So that we've got an end point so that we can create an order and get our existing orders from the database.
And we'll look at that next.