Okay, so we've written some code, some tedious looking code to shape the data from an entity which
has given us problems when we return it due to the JSON serializer into a DTO, which obviously our
JSON serializer is happy with.
Now we need to use this code in a few locations.
We need to use it here.
Of course in the get basket method, we also need it in the add item to basket method because we are
returning our basket there.
Now we could use the same trick and copy our code and do the same thing there.
But that's not good practice and you'll often hear the phrase do not repeat yourself.
Often shortened to dry DIY, and we'll use one of our tools in our dry toolbox, which is extension
methods, which helps us reduce duplicate code and create reusable code that we can use wherever we
need to.
So in order to create an extension method and I'm going to talk about extension methods here, and I'll
explain what they are as we go and use them.
Inside our API folder we're going to create a new folder called extensions.
And inside this extensions folder, we're going to create a new file and we're going to call it basket.
In fact we need to create a new class first of all.
And then I'm just going to call it basket extensions and press return.
Now an extension method.
First of all we make this a static class.
Because when we use an extension method we don't say new basket extensions and then get access to the
code and methods inside that class.
We don't want to use it that way.
A static class means that we can use the methods inside here without actually creating a new instance
of this basket extensions class.
And inside our static class we're going to create a static method.
So I'll say public static.
And the next thing is what are we going to return from this.
Well we want to return a basket DTO from this extension method.
And I'm going to call it to DTO.
And what we're going to use this extension method on is our basket entity.
So we specify list the keyword lists and then baskets Gets from API entities, as this is where we want
to use this extension method.
So our basket, when we have a basket, we will be able to say and I'll just put it in a comment.
After this we will be able to say basket to DTO because we're effectively creating an extension to the
basket, an extension method that extends the basket entity functionality.
And one of its methods after we've done this will be to use this two DTO method.
So we'll call this baskets.
And inside here we will effectively return a new basket DTO.
And if we go back to our basket controller what we've already got the code inside here.
So I'm just going to cut this from the basket controller and paste it into our basket extensions.
So when we use this method it returns the basket DTO.
Great.
And if we go back to our basket controller how we use this is we're going to return the basket.
And notice now that we have a two DTO method available that we can use that we get from API extensions.
So we'll just utilize that.
And now we have much cleaner code.
And this is reusable because I can also use it in the add item to basket.
And where we're returning the basket we can specify basket to DTO and execute that and what we could
do as well.
We're not doing it at the moment because I did imply that we were not going to return the basket.
We could also give this a return type as well, although it doesn't really do very much for us because
we're returning this inside an HTTP response object.
So we're not going to get type safety.
So it's a bit pointless.
But just because I've mentioned it, we could also specify that we're returning a basket DTO from this
method as well.
But it doesn't do anything for us.
But it doesn't cause us a problem either.
And when I say it doesn't do anything for us, well, it does a little bit because we can instantly
look at this method and see now what it is we're returning from this method without looking down in
the code to take a look at this return statement.
So does this fix our problem?
Well, let's open up the terminal.
First of all, because we may need to restart our API controller or our API server.
So I'm going to say yes to that and wait for this to restart.
And let's go back across to postman.
And we'll click the send to get the baskets.
And this functionality is still working.
Let's try and add another item to the basket.
And I'm just going to repeat the same request because what they should do is increase the quantity to
two.
So I'll click send.
And what we get back is the shaped data.
We can see the quantity is two.
And if I send another request to add another product for instance and click send.
So I'm changing it to product ID of three and click send.
Now we have two products two items in our basket, one with a quantity of two and one with a quantity
of one.
Perfect.
And if we go and get a basket, we should see the updated basket there because we're sending our cookie
to and from our API server.
So just to confirm that as well because you don't see a lot of detail in this postman window.
If we do, open up the console and let's just clear this because it's very busy.
And I'll click send again just to get the basket.
And I click on this request.
Do not worry about this warning.
Unable to verify the first certificate because dotnet uses a self-signed certificate.
So postman is unable to verify that this is a valid certificate.
A self-signed certificate is never valid, certainly not in the big bad world of the internet, but
we do use it in a developer environment, so self-signed is fine and we do not need to worry about that
warning.
If we go and take a look at the request headers.
What we can see inside here is that we are sending up that cookie with this request, and that is how
we're able to get our baskets from our API server.
So please do use this postman console, another tool in the toolbox for troubleshooting.
And if you're not getting your basket back, then you would definitely want to come in here and see
what you're using for the basket ID.
Does that match the property you're using in your API, for example.
So please do use all of the tools available when you encounter a problem.
And the console in postman is another good one to use.
Okay, so now we've got our extension method set up and we have a bit more work to do in our controller.
And if we go and take a look at what we currently have for our delete method, then this is what we
need to populate.
So what I'd suggest is you pause the video at this stage and just populate this and see if you can get
this to work and use the postman request to remove an item from the basket.
And I would suggest changing this quantity from 4 to 1, for example.
So you remove one of the product IDs of two and a quantity of one, and see if you can get that functionality
to work.
Before you take a look at the next lesson, I am going to go through this step by step in the next lesson,
but I think you should be okay with getting this one completed without seeing that lesson.
So please give that a go.
And like I mentioned in the next lesson, I'm going to cover what we need to do for that anyway.