WEBVTT

00:00.000 --> 00:06.090
Okay, so we've written some code, some tedious looking code to shape the data from an entity which

00:06.090 --> 00:12.150
has given us problems when we return it due to the JSON serializer into a DTO, which obviously our

00:12.150 --> 00:14.250
JSON serializer is happy with.

00:14.280 --> 00:16.620
Now we need to use this code in a few locations.

00:16.620 --> 00:17.460
We need to use it here.

00:17.460 --> 00:22.650
Of course in the get basket method, we also need it in the add item to basket method because we are

00:22.650 --> 00:24.090
returning our basket there.

00:24.090 --> 00:29.640
Now we could use the same trick and copy our code and do the same thing there.

00:29.640 --> 00:35.100
But that's not good practice and you'll often hear the phrase do not repeat yourself.

00:35.130 --> 00:43.170
Often shortened to dry DIY, and we'll use one of our tools in our dry toolbox, which is extension

00:43.170 --> 00:49.860
methods, which helps us reduce duplicate code and create reusable code that we can use wherever we

00:49.860 --> 00:50.670
need to.

00:50.700 --> 00:56.310
So in order to create an extension method and I'm going to talk about extension methods here, and I'll

00:56.340 --> 00:59.010
explain what they are as we go and use them.

00:59.010 --> 01:03.270
Inside our API folder we're going to create a new folder called extensions.

01:03.720 --> 01:10.220
And inside this extensions folder, we're going to create a new file and we're going to call it basket.

01:10.220 --> 01:12.890
In fact we need to create a new class first of all.

01:12.890 --> 01:17.960
And then I'm just going to call it basket extensions and press return.

01:17.990 --> 01:20.180
Now an extension method.

01:20.300 --> 01:22.880
First of all we make this a static class.

01:22.880 --> 01:29.420
Because when we use an extension method we don't say new basket extensions and then get access to the

01:29.420 --> 01:31.550
code and methods inside that class.

01:31.550 --> 01:33.560
We don't want to use it that way.

01:33.590 --> 01:39.800
A static class means that we can use the methods inside here without actually creating a new instance

01:39.800 --> 01:42.320
of this basket extensions class.

01:42.320 --> 01:47.210
And inside our static class we're going to create a static method.

01:47.210 --> 01:49.370
So I'll say public static.

01:49.670 --> 01:53.030
And the next thing is what are we going to return from this.

01:53.060 --> 01:57.080
Well we want to return a basket DTO from this extension method.

01:57.080 --> 01:59.690
And I'm going to call it to DTO.

01:59.690 --> 02:05.720
And what we're going to use this extension method on is our basket entity.

02:05.720 --> 02:14.480
So we specify list the keyword lists and then baskets Gets from API entities, as this is where we want

02:14.480 --> 02:17.210
to use this extension method.

02:17.210 --> 02:22.970
So our basket, when we have a basket, we will be able to say and I'll just put it in a comment.

02:22.970 --> 02:29.840
After this we will be able to say basket to DTO because we're effectively creating an extension to the

02:29.840 --> 02:35.000
basket, an extension method that extends the basket entity functionality.

02:35.000 --> 02:42.020
And one of its methods after we've done this will be to use this two DTO method.

02:42.020 --> 02:45.020
So we'll call this baskets.

02:45.020 --> 02:52.190
And inside here we will effectively return a new basket DTO.

02:52.190 --> 02:56.450
And if we go back to our basket controller what we've already got the code inside here.

02:56.450 --> 03:04.160
So I'm just going to cut this from the basket controller and paste it into our basket extensions.

03:04.250 --> 03:07.850
So when we use this method it returns the basket DTO.

03:07.880 --> 03:08.570
Great.

03:08.570 --> 03:14.690
And if we go back to our basket controller how we use this is we're going to return the basket.

03:14.780 --> 03:22.460
And notice now that we have a two DTO method available that we can use that we get from API extensions.

03:22.460 --> 03:24.350
So we'll just utilize that.

03:24.350 --> 03:26.960
And now we have much cleaner code.

03:26.960 --> 03:32.150
And this is reusable because I can also use it in the add item to basket.

03:32.150 --> 03:40.880
And where we're returning the basket we can specify basket to DTO and execute that and what we could

03:40.880 --> 03:41.450
do as well.

03:41.450 --> 03:45.530
We're not doing it at the moment because I did imply that we were not going to return the basket.

03:45.560 --> 03:51.650
We could also give this a return type as well, although it doesn't really do very much for us because

03:51.650 --> 03:55.700
we're returning this inside an HTTP response object.

03:55.700 --> 03:57.680
So we're not going to get type safety.

03:57.680 --> 03:58.700
So it's a bit pointless.

03:58.700 --> 04:05.360
But just because I've mentioned it, we could also specify that we're returning a basket DTO from this

04:05.360 --> 04:06.290
method as well.

04:06.290 --> 04:08.540
But it doesn't do anything for us.

04:08.540 --> 04:10.640
But it doesn't cause us a problem either.

04:10.640 --> 04:14.480
And when I say it doesn't do anything for us, well, it does a little bit because we can instantly

04:14.480 --> 04:20.240
look at this method and see now what it is we're returning from this method without looking down in

04:20.240 --> 04:23.300
the code to take a look at this return statement.

04:23.300 --> 04:25.190
So does this fix our problem?

04:25.190 --> 04:26.390
Well, let's open up the terminal.

04:26.390 --> 04:31.670
First of all, because we may need to restart our API controller or our API server.

04:31.670 --> 04:35.930
So I'm going to say yes to that and wait for this to restart.

04:35.930 --> 04:39.740
And let's go back across to postman.

04:39.740 --> 04:42.200
And we'll click the send to get the baskets.

04:42.200 --> 04:44.870
And this functionality is still working.

04:44.870 --> 04:47.360
Let's try and add another item to the basket.

04:47.360 --> 04:53.180
And I'm just going to repeat the same request because what they should do is increase the quantity to

04:53.210 --> 04:53.540
two.

04:53.570 --> 04:54.740
So I'll click send.

04:54.740 --> 04:58.220
And what we get back is the shaped data.

04:58.220 --> 05:00.050
We can see the quantity is two.

05:00.080 --> 05:04.760
And if I send another request to add another product for instance and click send.

05:04.760 --> 05:07.850
So I'm changing it to product ID of three and click send.

05:07.880 --> 05:13.940
Now we have two products two items in our basket, one with a quantity of two and one with a quantity

05:13.940 --> 05:14.900
of one.

05:15.170 --> 05:16.070
Perfect.

05:16.070 --> 05:21.200
And if we go and get a basket, we should see the updated basket there because we're sending our cookie

05:21.200 --> 05:23.000
to and from our API server.

05:23.000 --> 05:28.220
So just to confirm that as well because you don't see a lot of detail in this postman window.

05:28.220 --> 05:34.100
If we do, open up the console and let's just clear this because it's very busy.

05:34.100 --> 05:36.680
And I'll click send again just to get the basket.

05:36.680 --> 05:39.230
And I click on this request.

05:39.260 --> 05:41.120
Do not worry about this warning.

05:41.150 --> 05:46.010
Unable to verify the first certificate because dotnet uses a self-signed certificate.

05:46.010 --> 05:49.970
So postman is unable to verify that this is a valid certificate.

05:50.000 --> 05:55.520
A self-signed certificate is never valid, certainly not in the big bad world of the internet, but

05:55.520 --> 05:59.840
we do use it in a developer environment, so self-signed is fine and we do not need to worry about that

05:59.870 --> 06:00.500
warning.

06:00.500 --> 06:02.840
If we go and take a look at the request headers.

06:02.840 --> 06:08.810
What we can see inside here is that we are sending up that cookie with this request, and that is how

06:08.810 --> 06:13.220
we're able to get our baskets from our API server.

06:14.210 --> 06:19.220
So please do use this postman console, another tool in the toolbox for troubleshooting.

06:19.220 --> 06:24.470
And if you're not getting your basket back, then you would definitely want to come in here and see

06:24.470 --> 06:26.180
what you're using for the basket ID.

06:26.210 --> 06:30.260
Does that match the property you're using in your API, for example.

06:30.260 --> 06:34.760
So please do use all of the tools available when you encounter a problem.

06:34.760 --> 06:37.970
And the console in postman is another good one to use.

06:38.000 --> 06:45.350
Okay, so now we've got our extension method set up and we have a bit more work to do in our controller.

06:45.980 --> 06:52.310
And if we go and take a look at what we currently have for our delete method, then this is what we

06:52.310 --> 06:53.300
need to populate.

06:53.300 --> 06:59.600
So what I'd suggest is you pause the video at this stage and just populate this and see if you can get

06:59.600 --> 07:05.240
this to work and use the postman request to remove an item from the basket.

07:05.240 --> 07:09.800
And I would suggest changing this quantity from 4 to 1, for example.

07:09.800 --> 07:16.790
So you remove one of the product IDs of two and a quantity of one, and see if you can get that functionality

07:16.790 --> 07:17.900
to work.

07:18.530 --> 07:23.720
Before you take a look at the next lesson, I am going to go through this step by step in the next lesson,

07:23.720 --> 07:29.150
but I think you should be okay with getting this one completed without seeing that lesson.

07:29.150 --> 07:30.560
So please give that a go.

07:30.560 --> 07:35.090
And like I mentioned in the next lesson, I'm going to cover what we need to do for that anyway.
