WEBVTT

00:00.320 --> 00:05.390
Okay, now that we've got our basket entity created, I just want to add a couple of methods inside

00:05.390 --> 00:05.660
here.

00:05.660 --> 00:11.240
So it makes it easier to add and remove items from our basket items.

00:11.270 --> 00:13.700
Now we do this in the basket class itself.

00:13.700 --> 00:16.970
I'm just going to add a couple of methods inside here.

00:16.970 --> 00:18.890
The first one is going to be a public method.

00:18.890 --> 00:22.130
And we'll say public void add item.

00:22.130 --> 00:26.060
And we're going to add a type of product into our basket.

00:26.060 --> 00:30.140
So it's going to be a product and we'll call it product as the first argument.

00:30.140 --> 00:35.420
And we'll add the int and quantity as the second argument.

00:35.780 --> 00:40.070
And before we use the product, let's just add a bit of defensive code to make sure that we actually

00:40.070 --> 00:43.550
have the product before we try and use it to add something to our basket.

00:43.550 --> 00:47.120
So we're going to check to see if the product is equal to null.

00:47.570 --> 00:49.880
And we're in the context of an entity here.

00:49.880 --> 00:52.850
So the product should not be null.

00:53.240 --> 00:59.900
But if it is null then we're going to use the arguments null exception.

01:00.140 --> 01:06.780
And we're just going to use the throw if null method and pass in the product as an argument to that.

01:07.230 --> 01:13.050
So if the product is null, then we break out of this method and we throw an exception.

01:13.050 --> 01:18.750
So exceptions are exceptional and we shouldn't necessarily encounter this exception.

01:18.750 --> 01:20.520
But just for our compiler.

01:20.550 --> 01:24.450
And just in case we're just doing a bit of defensive coding there.

01:24.480 --> 01:26.460
We're also going to check the quantity as well.

01:26.460 --> 01:33.360
And we want to make sure that the quantity is not less than or equal to zero, because of course we

01:33.360 --> 01:35.160
want that to be a positive number.

01:35.190 --> 01:44.580
And if that is the case, then we're going to throw a new arguments exception and say as the message

01:44.580 --> 01:49.350
the quantity should be greater than zero.

01:50.280 --> 01:58.770
And as the second parameter here we'll use the name of and quantity for the exception.

01:58.770 --> 02:03.060
And let's just drop this down because I'm going off the edge of the screen.

02:03.090 --> 02:04.020
Okay.

02:04.440 --> 02:06.720
So there's are a couple of defensive checks.

02:06.720 --> 02:13.050
And next we'll go and get the item which could possibly already be in our basket.

02:13.050 --> 02:19.170
So if they're adding something to our basket, then possibly they're increasing the quantity or it's

02:19.170 --> 02:22.890
going to be a new product that we're adding to the basket.

02:22.890 --> 02:29.490
So we'll create a variable and we'll say var existing item equals.

02:29.490 --> 02:33.930
And we'll just use a method that we'll create to do this called find item.

02:33.930 --> 02:37.440
And we'll pass in as the argument the product ID.

02:37.440 --> 02:39.930
And we'll put our cursor inside the find item.

02:39.930 --> 02:42.930
And we'll generate the method find item.

02:42.930 --> 02:48.060
And we're going to return from this a basket item.

02:48.630 --> 02:53.160
And we're going to make this optional because it may or may not already be in our basket.

02:53.160 --> 02:58.770
So we're going to return possibly the basket item or NULL if we do not find it.

02:58.770 --> 03:07.350
And we're going to return from this method items, which is what we're using what we have called our

03:07.350 --> 03:08.880
basket items.

03:09.450 --> 03:12.330
Then we'll use the first or default method.

03:12.330 --> 03:20.080
And this method specifically it will find the first item that matches if it exists in our list of basket

03:20.080 --> 03:25.600
items, or it will return the default value for a basket item.

03:25.600 --> 03:33.160
Now, because the basket item is an object if it's not found, the default value for this is null.

03:33.160 --> 03:37.570
So that's what we would return if it doesn't exist in our basket items.

03:37.570 --> 03:39.040
So I'll specify item.

03:39.040 --> 03:40.360
Then we'll use an expression.

03:40.360 --> 03:46.540
So we use the arrow and say item dot product id is equal to product.

03:46.540 --> 03:48.160
Or I've just called it ID here.

03:48.160 --> 03:50.410
Let's call it product ID instead.

03:51.910 --> 04:00.070
And I'll specify product ID here just for clarity as our items have an ID property as well as the product

04:00.100 --> 04:00.970
ID property.

04:00.970 --> 04:04.480
So it helps to be clear what it is we're using there.

04:04.480 --> 04:10.330
So then we can check to see if our existing item which may be the basket item, or it may be null.

04:10.330 --> 04:14.110
Then we can check to see if we do have that already in our basket.

04:14.260 --> 04:22.840
So if the existing item is equal to null, then we know that we need to add this to our items.

04:22.840 --> 04:24.670
So we're going to say items add.

04:25.840 --> 04:29.290
And then we'll say new baskets item.

04:29.290 --> 04:31.660
And we'll drop down to the next line.

04:31.660 --> 04:36.760
Open curly brackets and we'll specify the product is equal to product.

04:37.450 --> 04:41.500
And the quantity is equal to quantity.

04:41.500 --> 04:44.470
And that's all we need to do when we're adding a product.

04:44.470 --> 04:46.630
And then we can add the else condition.

04:46.630 --> 04:49.510
So just below the if statement we'll specify else.

04:49.630 --> 04:51.490
Then open curly brackets.

04:51.490 --> 04:56.440
And we can say existing item and set the quantity property equals.

04:56.440 --> 05:01.900
And we can say existing item dot quantity plus quantity.

05:01.900 --> 05:04.510
And we get the little helper the ellipses here.

05:04.510 --> 05:06.250
And let's see.

05:06.280 --> 05:11.170
It tells us that for this particular expression we should use a compound assignment.

05:11.170 --> 05:12.070
That sounds interesting.

05:12.070 --> 05:15.340
Let's use the quick fix and see what this suggests.

05:15.340 --> 05:17.350
And sure, that makes a lot of sense.

05:17.350 --> 05:22.750
That's a lot more concise than the method I was showing there.

05:22.780 --> 05:23.470
So great.

05:23.470 --> 05:28.640
That's going to increase the quantity of the existing item if it was already in the basket.

05:28.640 --> 05:31.700
So that's the add item taken care of.

05:31.700 --> 05:36.650
And then we need two similar but different of course for the remove item.

05:36.650 --> 05:41.300
So we're going to say publicvoid remove item.

05:41.300 --> 05:45.470
And inside here we just need the products ID.

05:45.620 --> 05:47.750
And again we need the quantity.

05:47.750 --> 05:52.580
If they're removing or reducing the number of the items in the basket.

05:52.580 --> 05:58.340
So we're going to check once again to make sure the quantity is not less than or equal to zero.

05:58.370 --> 06:03.830
And if it is then we're going to throw a new argument exception.

06:03.830 --> 06:12.950
And we'll just use the same message and say quantity should be greater than zero.

06:13.340 --> 06:19.370
And once again we'll just pass in as a second parameter the name of quantity okay.

06:19.370 --> 06:20.990
And let's drop that down as well.

06:20.990 --> 06:23.690
So I don't go off the edge of the screen.

06:23.690 --> 06:28.370
So then we just need to get hold of the item from our basket.

06:28.370 --> 06:35.610
So we'll use var item equals and then we can use the find item method we created below and pass in the

06:35.610 --> 06:36.690
product id.

06:37.470 --> 06:43.080
And we need to check to see if the item is null because of course if we hover over the item then we

06:43.080 --> 06:47.040
can see that possibly could be null because we can see that question mark there.

06:47.250 --> 06:51.570
So if the item is equal to null, what do we do from this.

06:51.600 --> 06:53.220
Well we can't really do anything.

06:53.880 --> 06:59.280
So all we'll do is we won't throw an exception, but we'll simply return from this method and stop the

06:59.280 --> 07:02.670
execution of the removal of the item.

07:03.420 --> 07:09.150
Then we'll specify item dot quantity is minus equals quantity.

07:09.150 --> 07:14.880
And then we can check to see if the item dot quantity is equal.

07:14.910 --> 07:20.970
Or let's say less than equal to zero and equal to zero should equally do the same thing here.

07:20.970 --> 07:26.730
I'm just being maybe overly careful, but if they've got zero or less than zero items in the basket,

07:26.730 --> 07:29.940
then we just want to remove the item from the basket.

07:29.940 --> 07:34.320
So I'm going to say items remove and pass in the item.

07:34.350 --> 07:36.930
Now what's going on with these add and remove methods?

07:36.930 --> 07:40.770
By the way, when it comes to using them, we're not doing anything with the database.

07:40.770 --> 07:47.100
When we execute these, all we're doing is telling Entity Framework to track what the current state

07:47.100 --> 07:48.390
of the entity is.

07:48.390 --> 07:53.820
Because in order to use these methods, we're going to have to retrieve the entity, the basket from

07:53.820 --> 07:54.720
the database.

07:54.720 --> 08:01.920
And then we can use the basket functionality such as the add item or the remove item on that entity.

08:01.920 --> 08:07.350
And when we do use one of these methods, then we're simply going to be tracking the state of that entity

08:07.350 --> 08:08.040
in memory.

08:08.040 --> 08:14.790
At that point, nothing happens with the database until we call a save changes method in the database.

08:14.790 --> 08:16.800
But we'll come on to that when we use it.

08:16.800 --> 08:23.550
So these are helper methods that just make it easier for us to use this entity class in our code or

08:23.550 --> 08:28.260
inside our controllers when we add the endpoints for this kind of functionality.

08:28.260 --> 08:33.420
So what we're going to take a look at next is we're going to take a look at updating our DB context.

08:33.420 --> 08:39.240
And then we're going to take a look at creating a new migration so that we can update our database schema.

08:39.240 --> 08:40.890
And that's what's coming up.
