WEBVTT

00:00.000 --> 00:00.270
Okay.

00:00.300 --> 00:02.820
So our next step is to create the order entity itself.

00:02.820 --> 00:07.410
So inside the order aggregates we'll create another new file another new class.

00:07.410 --> 00:09.570
And this is going to be called order.

00:10.050 --> 00:15.120
And inside this class we'll give it an int for its ID.

00:16.560 --> 00:23.520
We'll also have a prop that's a string for the buyer email address.

00:24.840 --> 00:32.010
We'll have a prop of type shipping address that's going to be called shipping address.

00:32.340 --> 00:34.860
We'll have a prop that's a date time.

00:36.780 --> 00:40.140
And we'll use the name of order date for this.

00:40.140 --> 00:43.200
And we'll initialize this date to date time.

00:43.200 --> 00:49.830
And we'll use Utcnow, which is a standard date format for anywhere in the world.

00:49.890 --> 00:57.810
And when we return an object to our client browser with this type of date formatting, then our browser

00:57.810 --> 01:00.390
will convert that into local time for us.

01:00.450 --> 01:11.160
We'll also have a prop that's a list of type order item and we'll call this order items.

01:12.570 --> 01:16.710
We'll have a prop that's a long fully subtotal of the order.

01:18.780 --> 01:22.980
We'll also have a prop that's along for the delivery fee.

01:23.220 --> 01:28.650
And even though we're not using it right now, we'll also add another long for the discount.

01:29.460 --> 01:31.680
Something that's going to come a bit later.

01:31.920 --> 01:37.440
And we'll also add a string for the payment intent ID.

01:39.150 --> 01:45.630
And we'll also add the order status and call it order status.

01:45.630 --> 01:48.870
And one more to go as far as properties goes.

01:48.870 --> 01:50.070
Quite a few inside here.

01:50.070 --> 01:56.880
We'll also have the payment summary and call it payment summary.

01:56.880 --> 02:00.570
And we'll just have a helper method inside here.

02:00.570 --> 02:04.020
And we'll use public long get total.

02:05.170 --> 02:07.030
And we will return from this.

02:07.060 --> 02:13.150
The subtotal plus the delivery fee minus the discounts.

02:13.930 --> 02:16.360
And let's take a look at our required properties.

02:16.360 --> 02:20.080
So where we've got things like the buyer email we definitely want that.

02:20.080 --> 02:21.640
So that's going to be required.

02:22.180 --> 02:27.910
And this is how we're going to identify which orders are related to a specific user.

02:27.910 --> 02:33.550
So when it comes to fetching the user's orders then we've got a property that we can use for that.

02:33.580 --> 02:36.700
The shipping address is going to be stored with the order.

02:36.760 --> 02:41.290
And by using an owned property we've got an orders table.

02:41.290 --> 02:46.840
And we'll have all of the properties for the shipping address inside the order table, not in a separate

02:46.870 --> 02:47.620
table.

02:47.650 --> 02:50.320
The order date is fairly self-explanatory.

02:50.350 --> 02:53.230
We'll also have our list of items.

02:53.230 --> 03:00.160
And because it's a list, we'll just initialize it with an empty list by giving it the square brackets.

03:00.220 --> 03:03.040
We've got our subtotal, delivery fee and discounts.

03:03.040 --> 03:05.230
And then we've got our payment intent ID.

03:05.230 --> 03:09.980
Are we guaranteed to have this when we create the order?

03:10.010 --> 03:12.800
In theory, we should have it from our baskets.

03:12.800 --> 03:18.980
But I'm going to make this optional anyway, which is going to force us to be defensive in our code

03:18.980 --> 03:22.430
and check for this before we try and use it anywhere.

03:22.430 --> 03:25.760
So I'll make this optional just in case we do not have that.

03:25.880 --> 03:32.210
The order status will give an initial value to order status dot pending.

03:34.280 --> 03:40.520
When we first create an order and we'll also make the payment summary required when we create an order.

03:40.520 --> 03:45.710
Because we should have that information because we're going to create the order after payment has been

03:45.710 --> 03:46.160
made.

03:46.160 --> 03:48.830
And then we've just got our get total function.

03:48.830 --> 03:52.520
So we now have something new to add to our store context.

03:52.520 --> 03:56.900
So I'll just close all of this at the top and we'll open up our store context.

03:56.990 --> 03:59.660
And we will add inside here.

03:59.660 --> 04:09.370
And the only thing we need to add inside here is the dbset of type order, which we get from order aggregates

04:09.370 --> 04:12.460
and we're using the same names that stripe also has available.

04:12.460 --> 04:18.190
So do be careful where you get this from and make sure it's coming from our own code, and we'll make

04:18.190 --> 04:21.730
this required as well, just like the other ones above.

04:21.760 --> 04:27.670
So what we should be able to do now is create a new migration and update our database schema.

04:27.670 --> 04:29.560
So let's just go ahead and open a terminal.

04:29.560 --> 04:31.870
We'll go back to our tab that's running the API.

04:31.870 --> 04:34.840
We will need to stop our API at this point.

04:34.840 --> 04:38.650
And I'll use dotnet f migrations ads.

04:39.760 --> 04:44.680
And I'll just say order entity added as the name of the migration.

04:44.680 --> 04:47.590
And this should work without any issues.

04:47.590 --> 04:48.490
But we will see.

04:48.520 --> 04:49.660
And that looks fine.

04:49.660 --> 04:56.560
So let's go and take a look at what has been created inside the data folder inside migrations.

04:56.560 --> 05:00.640
Then we've got our order entity added migration available.

05:00.850 --> 05:04.600
Inside here we're creating a table called orders.

05:04.600 --> 05:11.060
And notice what properties we have inside here we've got the normal things the SQLite autoincrement

05:11.060 --> 05:14.690
for the ID, but then we've got the shipping address properties.

05:14.690 --> 05:18.620
And because this is an owned property, it's owned by the orders table.

05:18.620 --> 05:24.680
Then the shipping address gets added inline along with the other properties inside the order.

05:24.680 --> 05:27.050
So we've got all of our shipping address properties there.

05:27.080 --> 05:32.870
We've got our order date, the subtotal delivery fee, etc. and then we've got our payment summary details

05:32.870 --> 05:38.540
again, because that's an owned property that appears inside the same table as the orders.

05:39.410 --> 05:41.540
So that's the idea of that.

05:41.600 --> 05:45.470
Inside here we've also got our order item table.

05:45.500 --> 05:51.380
Again we've got an owned property for the product and the properties that we're storing inside there

05:51.380 --> 05:53.060
as well as the other properties.

05:53.060 --> 05:54.620
And that's it.

05:54.620 --> 05:55.550
That looks good to me.

05:55.550 --> 05:59.720
So what we should be able to do now is start our API.

06:00.080 --> 06:04.670
And I'll just use dotnet watch to do that.

06:05.720 --> 06:09.770
And hopefully this will start without any issues.

06:10.250 --> 06:11.910
And again that looks fine to me.

06:11.910 --> 06:15.720
So let's go and double check our database file.

06:15.750 --> 06:17.970
And I'll just click on the store DB.

06:18.330 --> 06:24.810
And what we should see now is that we've got our orders table along with all of its different columns,

06:24.810 --> 06:26.760
including the owned properties.

06:26.760 --> 06:29.790
And we've also got a table for our order item.

06:29.790 --> 06:34.380
Now, I didn't make this pluralised, and in a way I wish I had because that's how I'd normally do it.

06:34.380 --> 06:39.300
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

06:39.300 --> 06:40.110
that as it is.

06:40.110 --> 06:43.530
But you've seen how to change a table name before.

06:43.530 --> 06:49.530
We can give it the table attribute and then give it a table name, I'm going to say that's okay for

06:49.530 --> 06:54.480
what we're doing in this demo, but if I did want to do that, I would need to give it the table name

06:54.480 --> 06:55.320
property.

06:55.350 --> 07:00.660
Then I would need to create a new migration, and then that would update the database schema accordingly.

07:00.660 --> 07:03.660
But like I say, I'm not going to do that on this occasion.

07:03.660 --> 07:06.720
So the next thing we need to think about is an orders controller.

07:06.720 --> 07:12.720
So that we've got an end point so that we can create an order and get our existing orders from the database.

07:12.720 --> 07:14.370
And we'll look at that next.
