WEBVTT

00:00.020 --> 00:05.480
Okay, now that we've set up our entity classes and our database schema, our next task is to create

00:05.480 --> 00:13.190
the basket controller so that we've got endpoints which will allow our clients application to add items

00:13.190 --> 00:17.600
to a basket, retrieve the basket and remove items from a basket.

00:17.600 --> 00:20.360
So we're going to create a new file in the controllers folder.

00:20.390 --> 00:22.010
I'll just create a new class.

00:22.010 --> 00:27.980
And I'm going to call this basket controller and press return.

00:28.040 --> 00:34.430
And this is going to derive from our base API controller we created earlier.

00:35.090 --> 00:38.180
And we're going to have three endpoints in here.

00:38.180 --> 00:43.370
We're going to have an HTTP get which is going to go out and get our basket.

00:43.370 --> 00:46.130
So I'm going to say public async task.

00:46.160 --> 00:52.580
And we're going to return an action result of type basket.

00:53.720 --> 00:57.050
And we're going to call it get baskets.

00:57.200 --> 00:59.780
Inside this method we will.

00:59.780 --> 01:04.520
And in fact we need to inject our store context inside our controller as well.

01:04.520 --> 01:10.820
So just to the right of basket controller we'll specify store context and call it context.

01:10.850 --> 01:19.880
And then in the HTTP get method we will say var basket equals await context dot baskets.

01:20.240 --> 01:28.250
And when we want to include a related property then we've got a few different options.

01:28.250 --> 01:33.950
And the way that I'm going to demonstrate initially is to use something called eager loading.

01:33.950 --> 01:40.370
And the way that we use eager loading is we tell Entity Framework what we want to include, what related

01:40.370 --> 01:42.980
entities we wish to include with this.

01:42.980 --> 01:47.930
So I'm going to say that I want to include the basket items.

01:48.020 --> 01:57.320
And I'll just use X, which is a common developer shorthand way of saying instead of saying item for

01:57.320 --> 02:01.050
the thing that we're using as our argument here.

02:01.050 --> 02:07.170
Then a lot of developers just shortcut that and say x dot items instead.

02:07.200 --> 02:14.850
Very common that you'll see that some developers prefer to put the word of what it is that we're querying.

02:14.850 --> 02:20.340
But I don't know, I'm kind of okay with just using the X there.

02:20.520 --> 02:26.070
And also we want to get the related product for the item as well.

02:26.070 --> 02:29.100
So items have a relationship with the products.

02:29.100 --> 02:38.310
So to get that level we need to use Len include and we'll say x at the arrow say x dot products.

02:38.880 --> 02:42.330
And then we can use first or default async.

02:42.330 --> 02:46.620
And there's a number of different ways of getting or finding something in our database.

02:46.650 --> 02:51.630
I typically use first or default, but there's also single or default async as well.

02:51.660 --> 02:54.120
There are some subtle differences between them.

02:54.120 --> 02:55.200
They'll both do the same thing.

02:55.200 --> 02:56.700
For our purposes.

02:56.700 --> 02:59.460
The difference is the single or default.

02:59.460 --> 03:04.380
If there's more than one match in the table, then it will throw an exception.

03:04.380 --> 03:09.780
If we use first or default, it's just going to bring the first one it matches and return that.

03:09.810 --> 03:14.700
And if they don't exist in the database at all, then it's just going to return null for both of them.

03:14.730 --> 03:16.950
Typically I just use first or default.

03:16.950 --> 03:19.680
They're going to do the same thing in our case.

03:19.680 --> 03:22.920
And then inside parentheses we'll say x.

03:22.920 --> 03:28.080
And we're going to go for x dot basket id that's equal to.

03:28.170 --> 03:31.740
And we're going to get this ID from cookies.

03:32.130 --> 03:40.380
And when we issue a cookie to a client then that's going to be stored in the browser in the cookies

03:40.380 --> 03:41.100
area.

03:41.100 --> 03:47.400
And after we've configured a 1 or 2 more things, then the idea is that that cookie gets sent up with

03:47.400 --> 03:49.860
every request to our API.

03:49.860 --> 03:53.790
So if they've got a cookie in their browser, it's going to be sent up with this request.

03:53.790 --> 03:55.710
And we're going to use that to find the basket.

03:55.710 --> 03:58.200
So I'm going to specify request dot cookies.

03:58.210 --> 03:59.920
We've got access to them here.

03:59.920 --> 04:06.160
And then in square brackets we're going to look for a cookie called basket ID, and we may or may not

04:06.160 --> 04:07.420
have a basket.

04:07.420 --> 04:14.950
So I'm going to specify if basket is equal to null I'm not going to return an error I'm going to return

04:14.980 --> 04:24.430
no content as we don't require our users to have a basket when they visit our application.

04:24.430 --> 04:31.270
But we will need to check to see if they've got a basket, if they've got a cookie in their browser.

04:31.270 --> 04:37.210
So for the time being, we're just going to return no content if there is no basket for that user.

04:37.210 --> 04:44.080
And if we do, then we're going to return the basket.

04:45.580 --> 04:51.520
Now I'm just going to stub out two more methods and just put some comments in about what kind of things

04:51.520 --> 04:59.660
we're going to need to do in the post, we're going to use to add an item to our basket and also for

04:59.660 --> 05:00.410
the delete.

05:00.440 --> 05:04.220
What we're going to need to do when we delete an item from the basket.

05:04.250 --> 05:07.640
So the next method is going to be an HTTP post.

05:07.640 --> 05:10.190
And we don't need to give this any route parameters.

05:10.190 --> 05:17.750
So the next line we'll use public async task and Actionresult.

05:17.870 --> 05:23.990
And we don't need to return anything from this necessarily because the client knows what we're adding

05:24.020 --> 05:25.700
to the basket.

05:26.240 --> 05:30.740
So we don't need to send anything back to say yep, this has been added to the basket.

05:30.770 --> 05:33.620
We technically don't need to return anything from here.

05:33.650 --> 05:41.510
So I'm just going to say add item to basket as the method name, give it an int of the product id and

05:41.510 --> 05:45.620
an int of the quantity of what we're adding here.

05:45.620 --> 05:50.450
And just to add some comments in about the kind of things we're going to need to do for this method

05:50.480 --> 05:51.020
to work.

05:51.020 --> 05:55.490
So the first thing we'd need to do is get the basket from the database.

05:55.640 --> 05:59.360
If we do not have the baskets, then we would need to create a basket.

06:00.350 --> 06:04.280
Then we would need to get the products they wish to add to the baskets.

06:04.280 --> 06:12.200
Then we would need to add that item to the baskets, and then we would need to save changes so that

06:12.200 --> 06:14.030
we update our database.

06:14.030 --> 06:16.730
And then we would need to return something from this.

06:16.730 --> 06:23.180
And because technically we're creating a resource in a database, the correct response is typically

06:23.180 --> 06:29.420
a 201 for list and create an HTTP created response is a 201.

06:29.540 --> 06:32.900
So that's what we're going to try and achieve in that method.

06:32.900 --> 06:37.040
And I'll just flesh out what we're going to do for the delete inside here.

06:37.040 --> 06:41.180
And this is going to be public async task.

06:41.330 --> 06:43.430
And again action results.

06:43.430 --> 06:49.160
And again we wouldn't necessarily return anything from an HTTP delete method.

06:49.160 --> 06:53.240
So I'll specify remove basket item as the name of the method.

06:53.240 --> 07:02.340
And again we're going to take the int of the product id and the increase of the quantity as the parameters

07:02.340 --> 07:03.300
here.

07:04.170 --> 07:07.680
And what we'll need to do in here is once again get hold of the baskets.

07:07.680 --> 07:13.950
We would then need to remove the item or reduce its quantity.

07:14.070 --> 07:17.160
And then we would need to save changes.

07:17.160 --> 07:19.170
And then we could return from this.

07:19.200 --> 07:19.980
Okay.

07:20.010 --> 07:21.990
Obviously the comments aren't going to do anything.

07:21.990 --> 07:26.610
And we'd need to flesh these out, which we're going to take a look at in the next lesson.

07:26.610 --> 07:31.980
I just wanted to give you the idea of what we're going to try and do now, because I've put the steps

07:31.980 --> 07:32.340
in here.

07:32.340 --> 07:36.960
If you're feeling like a challenge and you want to give this a go yourself, before I actually demonstrate

07:36.960 --> 07:43.590
how to do this, please see how far you can get with the limited comments that I've put in there.

07:43.590 --> 07:47.970
And regardless, I'm going to go through it step by step from the next lesson.

07:47.970 --> 07:53.010
But please feel free to have a go and see how far you can get with this.

07:53.010 --> 07:55.950
And then I'll cover what we need to do next.
