WEBVTT

00:00.590 --> 00:06.170
Okay, now that we've got the logic to both retrieve a basket and add an item into the basket, this

00:06.170 --> 00:10.940
gives us enough to test what's going to happen when we use these different endpoints.

00:10.970 --> 00:17.510
Now, because we've got some actual logic to take a look at now, maybe now's worthwhile to take a look

00:17.540 --> 00:20.930
at what's going to happen via the debugger as we go through this code.

00:20.930 --> 00:28.100
So I'm just going to add a breakpoint onto line 24, in my case where we go ahead and retrieve the basket.

00:28.100 --> 00:34.550
And this will give us an opportunity to see what is available inside our code and step through the code

00:34.550 --> 00:41.210
and effectively check the logic as we're going through and see what information we have available to

00:41.240 --> 00:41.840
us.

00:41.840 --> 00:47.960
So now's a good opportunity to do that and what we might want to do, because we've added some new code

00:48.110 --> 00:54.890
and we have a new controller, is just restart our API server before we go ahead and do this.

00:54.890 --> 00:58.100
And we can use control R to restart this.

00:58.100 --> 01:00.500
So I'll get that Underway.

01:00.740 --> 01:10.460
And I'll talk about this soon because this is something new in dotnet nine and now that has restarted,

01:10.460 --> 01:13.070
let's go ahead and run our debugger.

01:13.070 --> 01:18.350
So I'll hit the debugging tab and we want to use the dotnet core attach one.

01:18.350 --> 01:19.790
So I'll hit the play button.

01:19.790 --> 01:21.650
And then I'll search for API inside.

01:21.650 --> 01:26.990
Here I'll select API and off the debugger goes and starts running.

01:26.990 --> 01:30.530
And let's head over to postman to test these requests.

01:30.530 --> 01:35.090
So inside section seven we've got a request here to go and get the basket.

01:35.120 --> 01:36.920
Now we haven't added a breakpoint to this.

01:36.920 --> 01:40.280
We don't have a basket for anybody just yet.

01:40.280 --> 01:46.790
So if I send this request, what we would expect to get based on our code is a 200 success, but no

01:46.790 --> 01:47.960
content for the basket.

01:47.960 --> 01:50.570
So let's click send or not a 200.

01:50.600 --> 01:54.020
A 200 for no content is what we would expect from that.

01:54.050 --> 01:54.890
That's fine.

01:54.890 --> 01:56.750
That's what we would expect.

01:56.780 --> 02:00.130
Now, if we take a look at adding an item to the basket.

02:00.130 --> 02:05.350
I've got a product ID equal to two and a quantity equal to one.

02:05.350 --> 02:08.710
And these are being passed up as query string parameters.

02:08.740 --> 02:14.860
Now if I go and take a look at the code that we have inside our basket controller, then these are the

02:14.860 --> 02:15.820
two parameters.

02:15.820 --> 02:19.540
And the name of what we're using here has to match.

02:19.540 --> 02:28.690
So if I want product ID this argument to be bound to the HTTP request that I'm making so that the controller

02:28.720 --> 02:35.110
can use these parameters for the product ID and the quantity, then the names have to match exactly

02:35.470 --> 02:36.220
the same.

02:36.220 --> 02:39.430
So product ID is product id and quantity is quantity.

02:39.460 --> 02:41.590
So they have to match.

02:41.620 --> 02:46.240
Otherwise the API controller won't automatically bind to them.

02:46.270 --> 02:55.120
Now when it comes to API controllers and conventions, what we have here is passing in two properties

02:55.120 --> 02:56.410
as arguments.

02:56.440 --> 03:01.840
Now these are not going in the body of the Request, but because they're simple integers or they could

03:01.840 --> 03:08.140
be strings, then our API controller is automatically going to check the query string parameters and

03:08.140 --> 03:11.320
see if it can bind to these two properties.

03:11.950 --> 03:14.800
We don't need to tell it where to find these parameters.

03:14.800 --> 03:16.030
It's going to know.

03:16.570 --> 03:23.320
And we get that functionality, that automatic binding because of this API controller attributes.

03:23.320 --> 03:25.060
That's one of its features.

03:25.210 --> 03:29.680
It provides that auto binding between the things like the query string.

03:29.680 --> 03:36.910
If we're sending up parameters in that way, or if we're sending up properties in the body of a request,

03:36.910 --> 03:41.440
then the API controller will automatically bind them to that.

03:42.340 --> 03:47.950
If we were not using the API controller attribute for whatever reason, then we would need to tell our

03:47.980 --> 03:53.830
API where to find this by using, in this case from query for each of these properties.

03:53.830 --> 03:58.720
But we don't need that because of the way that we're using that API controller.

03:58.720 --> 04:00.220
So let's take a look.

04:00.220 --> 04:04.420
So all we need for this is just a product ID and a quantity of that product.

04:04.420 --> 04:09.490
And what should happen is we go and create a new basket and then return that basket.

04:09.520 --> 04:14.350
Now just to set the expectations, what we've got in our code, I'll just quickly go back here.

04:14.380 --> 04:18.670
This line here is going to give us a problem returning the basket from this request.

04:18.670 --> 04:22.510
The way that we have our code at the moment is going to be a problem.

04:22.510 --> 04:29.380
So I'm not expecting this to work, but we will get to the point where list line saving the changes

04:29.380 --> 04:31.720
into the database does work.

04:31.750 --> 04:35.080
It's just returning the basket at this stage is going to be problematic.

04:35.080 --> 04:37.480
So let's give it a go and I'll click send.

04:37.480 --> 04:42.640
And what should happen is we're taken to our breakpoint in the basket controller.

04:42.640 --> 04:44.110
And indeed we are.

04:45.010 --> 04:52.240
And we can see straight away that we have the quantity and the product ID correctly sets based on the

04:52.240 --> 04:55.030
parameters that we've passed in to this request.

04:55.030 --> 04:59.510
So that's the first thing we really want to look at when we're debugging something, are we getting

04:59.510 --> 05:01.370
the values that we'd expect here?

05:01.370 --> 05:05.300
If these were anything different, then we would know that we've got an issue that we need to take a

05:05.300 --> 05:06.050
look at.

05:06.170 --> 05:11.330
So the first line here is to set the basket and go ahead and retrieve it.

05:11.360 --> 05:12.980
So if I.

05:13.550 --> 05:18.770
Well we've got options here with our debugger controls we can step over which will take us to the next

05:18.770 --> 05:19.400
line.

05:19.400 --> 05:25.010
But because we've got a method inside here called Retrieve Basket, we can actually step into this which

05:25.010 --> 05:28.130
will take us into the retrieve basket method.

05:28.160 --> 05:29.360
And sure enough, it does.

05:29.390 --> 05:34.280
Now there's not a great deal we're going to see from this as we're going to step over this line of code,

05:34.280 --> 05:41.960
and this is going to return to us from our database if we have it a basket based on the cookies.

05:43.190 --> 05:44.630
So where does the cookie come from?

05:44.630 --> 05:45.830
Well, it comes from the request.

05:45.830 --> 05:54.620
So if we go into lists and we take a look at the HTTP context, and then we can take a look at the request

05:54.710 --> 05:57.420
inside here we've got our cookies.

05:58.590 --> 06:01.770
And if we had a cookie, then it would be visible inside here.

06:01.800 --> 06:05.010
Of course, we don't have a basket yet, so we do not have a cookie.

06:05.010 --> 06:08.850
So we know from this method that we're not going to return a basket.

06:08.850 --> 06:11.790
So I'm just going to step over and step over again.

06:11.790 --> 06:14.100
And this takes us to the next line of code.

06:14.100 --> 06:21.360
So currently our basket if we take a look at our debugger our basket is currently null.

06:21.360 --> 06:26.940
And we know that this line of code is going to create a basket if the basket is null.

06:26.940 --> 06:31.860
So once again let's step inside and take a look at the create basket method which it's now taken us

06:31.860 --> 06:32.490
to.

06:32.520 --> 06:35.550
So the first line is to create the basket ID.

06:35.550 --> 06:42.240
So if we step over and then step over again we can now see that we've got a basket ID that is set to

06:42.270 --> 06:45.120
a Guid formatted as a string.

06:45.120 --> 06:51.510
Then we create some cookie options and we set the is essential property and the expiry.

06:51.510 --> 06:57.120
So if we step over this line then we'll be able to take a look at our cookie options that are available,

06:57.120 --> 07:04.710
and specifically the properties that we've set the is essential and the expires, which is a date time

07:04.740 --> 07:05.760
object.

07:07.110 --> 07:07.590
Okay.

07:07.590 --> 07:10.620
So we'll keep going and then we'll step over again.

07:10.650 --> 07:12.900
And at this point we do create a new basket.

07:12.900 --> 07:20.520
And if we step over this line then at the moment we have a basket now created in memory with the basket

07:20.520 --> 07:20.880
ID.

07:20.880 --> 07:23.940
And it doesn't have an ID at this stage.

07:23.940 --> 07:28.950
The reason for this is because it's not currently saved into our database.

07:28.950 --> 07:36.240
We need SQLite to generate that ID, and once it has been saved into our database, it will be given

07:36.240 --> 07:36.960
an ID.

07:38.610 --> 07:46.470
So even if we step over the next line, the context baskets and then our basket still at this point

07:46.470 --> 07:51.060
does not have an ID, and that's because nothing's happened with our database yet.

07:51.060 --> 07:53.880
So we can step over and step over again.

07:53.880 --> 07:55.620
And now we have the basket.

07:55.620 --> 07:59.130
And if we go to the next line, next task is to go out and find the product.

07:59.130 --> 08:00.600
So we'll step over that.

08:00.600 --> 08:05.160
And what we should have now is our product populated with all of its properties.

08:05.190 --> 08:09.480
And then we can go ahead and add an item into our basket.

08:09.480 --> 08:11.100
So we'll step over this.

08:11.130 --> 08:17.370
And if we take a look at our basket now what we should have is a list of items with a count of one.

08:17.370 --> 08:22.020
And inside this item we have the product.

08:22.050 --> 08:25.740
And again we've got things like the ID that's set to zero.

08:25.740 --> 08:27.300
But we do have the quantity.

08:27.300 --> 08:30.780
The product ID still is set to zero inside there.

08:30.810 --> 08:36.630
That should be populated once we save our entity into the database.

08:36.630 --> 08:38.910
But that doesn't happen until this line.

08:38.910 --> 08:45.810
So if we do step over this line, then we go to the result and we can see that our result is equal to

08:45.840 --> 08:50.520
true, which means our database has now been updated.

08:51.420 --> 08:55.680
And what we can also see is because we've now saved this into the database.

08:55.710 --> 08:58.590
Our basket has an ID of one.

08:58.620 --> 09:07.860
The product ID inside the basket items has been set as well to the product ID based on what we retrieve

09:07.860 --> 09:08.880
from the database.

09:08.880 --> 09:09.870
And we're looking good.

09:09.900 --> 09:12.330
Our basket has been given an ID as well.

09:12.360 --> 09:17.280
Now, I mentioned that this isn't going to work because I expect that we're going to get an exception

09:17.280 --> 09:18.780
based on what we're doing here.

09:18.810 --> 09:26.400
And if I step over again and step over again, then no exception.

09:26.430 --> 09:34.680
Okay, so we've gone to the last line, which means we have returned something to postman, and we'll

09:34.680 --> 09:37.110
just click play to finish the request.

09:37.110 --> 09:39.420
So we didn't get an exception, but what did we get.

09:39.420 --> 09:41.730
And we've actually got the basket item.

09:41.760 --> 09:42.300
Okay.

09:42.330 --> 09:52.200
So interestingly I've got an internal server error and it seems I've actually got the response as well.

09:52.200 --> 09:54.740
But if we take a look at this Response.

09:54.770 --> 09:57.530
Then this is kind of what I was expecting to see.

09:57.530 --> 10:05.150
What I was really expecting to see was that we encountered a cyclic error, because what we have here

10:05.150 --> 10:08.600
is we've got our basket being returned with some items.

10:08.720 --> 10:11.330
Our items are returning products.

10:11.960 --> 10:16.100
And because we have fully defined the basket inside there.

10:16.100 --> 10:17.300
So we see the basket again.

10:17.300 --> 10:20.150
And that basket has items so on and so forth.

10:20.180 --> 10:29.780
What's happening here is our API controller is attempting to serialize the object into JSON format.

10:29.810 --> 10:35.990
And because of the way that we've defined our entity for the basket item, if I go to the entities and

10:35.990 --> 10:42.110
basket item, this is the problem right here where we've got our basket inside our basket items.

10:42.440 --> 10:47.360
When it's trying to format this into JSON, it keeps using the basket over and over again.

10:47.360 --> 10:54.650
And because we're including the basket items as well as when we return this, it's is getting itself

10:54.650 --> 10:56.210
into a bit of a pickle.

10:56.780 --> 11:01.880
And that used to return an exception that we would see inside postman.

11:01.880 --> 11:08.540
But if you do see something slightly different, don't worry, we're going to fix this very soon.

11:08.540 --> 11:15.230
But what we do have now, and what we can also confirm, if we take a look at our database and the store

11:15.230 --> 11:20.210
DB, then what we should find is that inside our baskets table, we've got a new basket.

11:20.330 --> 11:26.150
And inside the baskets item table we have a product inside there.

11:26.150 --> 11:28.490
Not a great deal of detail because it's a related entity.

11:28.490 --> 11:32.870
Then we don't see the full product details inside here or the full basket.

11:32.870 --> 11:36.050
We just see the IDs of those and that's absolutely fine.

11:36.050 --> 11:39.560
So that's a more useful use of using the debugger.

11:39.560 --> 11:43.040
But we do have a problem as we can clearly see inside postman.

11:43.040 --> 11:44.390
This is clearly not good.

11:44.390 --> 11:46.490
Sending our data back like this.

11:46.520 --> 11:47.450
What a mess.

11:47.450 --> 11:54.140
And we'll take a look at shaping our data next so that we avoid this kind of situation.
