WEBVTT

00:00.020 --> 00:04.610
Okay, our response going back to postman in this case is a bit of a mess, and it's because of the

00:04.610 --> 00:09.650
way that our data is structured in our entity classes, and we should see the same response if we try

00:09.650 --> 00:10.790
and get our basket.

00:10.820 --> 00:16.910
Now what we should have that I didn't check in the previous lesson is the cookies and what we should

00:16.910 --> 00:17.960
see inside here.

00:17.960 --> 00:21.860
Although for some reason it does take a couple of times for this to appear.

00:21.860 --> 00:27.470
I need to click cookies, close it, and then click cookies again, at least on my version of postman

00:27.590 --> 00:29.840
before I actually see the basket ID.

00:29.870 --> 00:38.720
But what we have now is a cookie, and this cookie is going to go up to our API server with every request

00:38.750 --> 00:39.200
now.

00:39.200 --> 00:44.990
So if I do attempt to go and get the basket, then what we should find is that we do send up the cookie

00:44.990 --> 00:46.430
and that should return the basket.

00:46.430 --> 00:48.920
If we click send, then we do get this.

00:48.920 --> 00:54.890
And every time this changes in Net between versions, it seems what we should be seeing here is an exception,

00:54.890 --> 00:59.180
but I'm actually seeing the response with all of the properties inside there.

00:59.180 --> 01:02.840
We do see the 500 internal server error and what that should mean.

01:02.840 --> 01:11.290
If we go back to the API server then we should see the exception inside our logs here and not the response.

01:11.290 --> 01:14.950
But we can see this massive stack trace actually before it does give up.

01:14.950 --> 01:20.860
And we do see the actual exception that for some reason we're not seeing in postman, and it tells us

01:20.860 --> 01:23.470
a possible object cycle was detected.

01:23.470 --> 01:26.710
And that's because of the reasons I mentioned in the previous lesson.

01:26.710 --> 01:28.360
So how do we solve this problem then?

01:28.360 --> 01:29.560
What do we do with this?

01:29.560 --> 01:36.190
Well, we introduce something called a DTO, a data transfer object into our application.

01:36.190 --> 01:44.290
And a data transfer object is just a simple object that's used to transfer data between layers or systems

01:44.290 --> 01:45.790
in an application.

01:45.790 --> 01:51.370
And it typically only contains the data fields needed without any business logic.

01:51.370 --> 01:56.560
So effectively we're going to create an object that has the properties that we do want to return in

01:56.560 --> 01:57.100
this case.

01:57.100 --> 02:01.690
So let's go ahead and do that I'll just close things down at the top.

02:01.690 --> 02:02.800
I'll also stop the debugger.

02:02.800 --> 02:06.940
We don't need that for what we're doing now unless we run into a problem of course.

02:06.940 --> 02:12.040
And inside our API folder I'm going to create a new folder called Dtos.

02:12.070 --> 02:16.570
Inside the Dtos folder, I'm going to create a new file that's going to be a class, and I'm going to

02:16.570 --> 02:19.360
call it baskets DTO.

02:19.360 --> 02:26.980
And inside this class I'm just going to copy the basket properties that we're going to return the ID,

02:27.010 --> 02:34.690
the basket ID and the items, and I will paste it into the basket DTO.

02:36.130 --> 02:43.750
And then for the list of basket items I'm going to say basket item DTO, put my cursor in this and say

02:43.750 --> 02:50.560
generate class basket item DTO and then move this into its own file once again using the quick fix.

02:50.650 --> 02:52.930
And then I'm going to go to the basket item.

02:52.930 --> 02:58.840
And I'm going to copy the properties inside here to give us an idea of what it is we want to be returning.

02:58.840 --> 03:04.420
But this one definitely needs a bit of adjustment, so I'll just paste that in there for the time being.

03:04.420 --> 03:09.010
And then we'll come back and populate this with the correct code that we need in there.

03:09.010 --> 03:12.580
So for the basket item DTO we need the id.

03:12.610 --> 03:19.080
I'll just create a line below this and we'll have a prop Of string for the name of the products will

03:19.080 --> 03:20.100
have a prop.

03:20.100 --> 03:29.880
That's going to be a long for the price, and we'll have a prop of a string for the picture URL.

03:29.940 --> 03:39.810
We'll have another prop that's a string for the brand, another prop that is a string for the type,

03:40.890 --> 03:42.390
and that should be that.

03:42.390 --> 03:47.070
So we don't actually need these other properties live copied inside here the navigation properties can

03:47.070 --> 03:47.850
all go.

03:47.850 --> 03:55.530
And for the DTO as well we'll use the required operator or modifier inside here.

03:55.860 --> 03:58.260
So we specify that each of these are required.

03:58.260 --> 04:05.220
So I'll just copy the required there and paste it in here and not in the price.

04:05.220 --> 04:05.940
We don't need it in there.

04:05.940 --> 04:07.320
We need it in the picture URL.

04:07.320 --> 04:08.340
We need it in the brand.

04:08.370 --> 04:10.290
We need it in the type.

04:10.290 --> 04:13.260
And I'll just ignore whatever that warning is.

04:13.260 --> 04:16.410
I don't think that's a problem, but we'll soon see.

04:17.460 --> 04:22.790
So now we've got our basket item DTO and we've got our basket dito.

04:22.820 --> 04:26.090
That's returning the basket item dtos as the items.

04:26.090 --> 04:29.090
How do we use this then to solve the problem that we had?

04:29.120 --> 04:32.660
Well, let's go across to our basket controller once again.

04:32.690 --> 04:40.610
And for the short term or the very short term, instead of returning the basket here, we'll still get

04:40.610 --> 04:41.300
the basket.

04:41.300 --> 04:47.750
And that's going to include the item and the also the products, the related properties.

04:47.750 --> 04:53.780
But what we return this is where we get the problem, because our API controller attempts to format

04:53.780 --> 04:56.900
this into JSON using a JSON serializer.

04:56.900 --> 05:01.760
So to solve this problem we can say return new basket DTO instead.

05:01.760 --> 05:08.840
And we'll also need to change the return type here to basket DTO for the action result for the get basket

05:08.930 --> 05:17.000
and inside the basket DTO the new basket DTO we're going to need to supply the basket ID equal to basket

05:17.660 --> 05:19.520
dot basket ID.

05:20.210 --> 05:25.190
We'll need the items and we'll set this equal to basket dot items.

05:25.190 --> 05:31.870
And then we use the select so that we can select what we want from the items.

05:31.870 --> 05:40.930
So we'll use select and then we'll say X goes to New baskets item DTO.

05:40.930 --> 05:47.110
And then inside here we can effectively map the properties that we want to return for our basket items.

05:47.110 --> 05:52.540
So we're going to have well what I want in here is a product ID property.

05:52.540 --> 05:55.690
But I'm going to need to go back to the basket item DTO.

05:57.250 --> 06:01.990
And instead of ID here we don't need the basket item DTO.

06:02.020 --> 06:04.390
We just need the product ID for this.

06:05.680 --> 06:12.550
And back to our basket controller I'll specify the product ID and this is going to be equal to x dot

06:12.970 --> 06:13.990
product ID.

06:15.070 --> 06:21.250
And then we need the name which is going to equal x dot product dot name.

06:23.680 --> 06:28.870
And then the price is going to be x dot product dot price.

06:29.500 --> 06:34.420
Then the brand is going to be equal to x dot product dot.

06:34.450 --> 06:36.220
Not the most exciting code.

06:36.220 --> 06:38.920
This by the way this mapping type code.

06:38.920 --> 06:41.230
So we'll only do this once really.

06:41.230 --> 06:44.980
And then we'll create something more reusable that we can use.

06:44.980 --> 06:49.870
There is an approach to handle this on our behalf that I've used in other courses by the way called

06:49.870 --> 06:52.810
Automapper, but I'm not going to use that right now.

06:52.810 --> 06:57.610
It's not much mapping code that we need to write, and I just want to demonstrate how we can do this

06:57.610 --> 07:00.430
without adding a tool like Automapper.

07:00.430 --> 07:04.060
And we also need a property for the picture URL.

07:04.210 --> 07:07.900
So we'll set that equal to x dot product dot picture URL.

07:07.900 --> 07:10.810
And we also need the quantity inside here.

07:10.810 --> 07:12.700
So that's going to be x dot quantity.

07:12.730 --> 07:16.150
And then we need to specify that we want this to be output to a list.

07:16.150 --> 07:21.400
So we specify two list there and close it off with semicolon at the end.

07:21.430 --> 07:23.500
Not where I just tried to put it at the end of the list.

07:23.530 --> 07:24.520
We don't want it there.

07:24.580 --> 07:26.260
We need it at the end here.

07:26.260 --> 07:32.620
So that's our basket DTO returning the basket items inside here.

07:32.890 --> 07:38.550
Now I didn't include the ID here actually, and I think I've got it in the basket Dito.

07:38.580 --> 07:40.530
I don't need to send this ID back.

07:40.530 --> 07:42.060
We've got the basket ID.

07:42.090 --> 07:43.260
That is enough.

07:43.260 --> 07:46.860
And if I go and take a look at the basket controller, this looks fine.

07:46.860 --> 07:53.610
So what we should have and I'm going to restart the API server again I don't trust Hot Reload for this

07:53.610 --> 07:54.450
for some reason.

07:54.450 --> 07:59.100
So I'm just going to make sure this works first time and just restart the API controller.

07:59.100 --> 08:00.450
And we see a warning there.

08:00.450 --> 08:05.730
But that's only because we've got a method inside our controller that we haven't populated with code

08:05.730 --> 08:06.030
yet.

08:06.060 --> 08:07.500
The remove basket item.

08:07.500 --> 08:09.600
We don't need to worry about that for the time being.

08:09.600 --> 08:16.740
So now if I go and get the basket, what we should see this time is we get the basket with its items

08:16.740 --> 08:20.100
and we've got one item in that basket.

08:20.100 --> 08:23.820
But what we created to do that was not particularly pleasant.

08:23.820 --> 08:28.980
And I would like a better way of shaping data when we return it.

08:28.980 --> 08:35.460
So in the next lesson, we're going to take a look at a tool in our toolbox that helps us deal with

08:35.460 --> 08:38.250
repetitive code so that we don't repeat ourselves.

08:38.250 --> 08:43.650
And in the next lesson, we'll take a look at extension methods to help us do that.
