Okay, our response going back to postman in this case is a bit of a mess, and it's because of the
way that our data is structured in our entity classes, and we should see the same response if we try
and get our basket.
Now what we should have that I didn't check in the previous lesson is the cookies and what we should
see inside here.
Although for some reason it does take a couple of times for this to appear.
I need to click cookies, close it, and then click cookies again, at least on my version of postman
before I actually see the basket ID.
But what we have now is a cookie, and this cookie is going to go up to our API server with every request
now.
So if I do attempt to go and get the basket, then what we should find is that we do send up the cookie
and that should return the basket.
If we click send, then we do get this.
And every time this changes in Net between versions, it seems what we should be seeing here is an exception,
but I'm actually seeing the response with all of the properties inside there.
We do see the 500 internal server error and what that should mean.
If we go back to the API server then we should see the exception inside our logs here and not the response.
But we can see this massive stack trace actually before it does give up.
And we do see the actual exception that for some reason we're not seeing in postman, and it tells us
a possible object cycle was detected.
And that's because of the reasons I mentioned in the previous lesson.
So how do we solve this problem then?
What do we do with this?
Well, we introduce something called a DTO, a data transfer object into our application.
And a data transfer object is just a simple object that's used to transfer data between layers or systems
in an application.
And it typically only contains the data fields needed without any business logic.
So effectively we're going to create an object that has the properties that we do want to return in
this case.
So let's go ahead and do that I'll just close things down at the top.
I'll also stop the debugger.
We don't need that for what we're doing now unless we run into a problem of course.
And inside our API folder I'm going to create a new folder called Dtos.
Inside the Dtos folder, I'm going to create a new file that's going to be a class, and I'm going to
call it baskets DTO.
And inside this class I'm just going to copy the basket properties that we're going to return the ID,
the basket ID and the items, and I will paste it into the basket DTO.
And then for the list of basket items I'm going to say basket item DTO, put my cursor in this and say
generate class basket item DTO and then move this into its own file once again using the quick fix.
And then I'm going to go to the basket item.
And I'm going to copy the properties inside here to give us an idea of what it is we want to be returning.
But this one definitely needs a bit of adjustment, so I'll just paste that in there for the time being.
And then we'll come back and populate this with the correct code that we need in there.
So for the basket item DTO we need the id.
I'll just create a line below this and we'll have a prop Of string for the name of the products will
have a prop.
That's going to be a long for the price, and we'll have a prop of a string for the picture URL.
We'll have another prop that's a string for the brand, another prop that is a string for the type,
and that should be that.
So we don't actually need these other properties live copied inside here the navigation properties can
all go.
And for the DTO as well we'll use the required operator or modifier inside here.
So we specify that each of these are required.
So I'll just copy the required there and paste it in here and not in the price.
We don't need it in there.
We need it in the picture URL.
We need it in the brand.
We need it in the type.
And I'll just ignore whatever that warning is.
I don't think that's a problem, but we'll soon see.
So now we've got our basket item DTO and we've got our basket dito.
That's returning the basket item dtos as the items.
How do we use this then to solve the problem that we had?
Well, let's go across to our basket controller once again.
And for the short term or the very short term, instead of returning the basket here, we'll still get
the basket.
And that's going to include the item and the also the products, the related properties.
But what we return this is where we get the problem, because our API controller attempts to format
this into JSON using a JSON serializer.
So to solve this problem we can say return new basket DTO instead.
And we'll also need to change the return type here to basket DTO for the action result for the get basket
and inside the basket DTO the new basket DTO we're going to need to supply the basket ID equal to basket
dot basket ID.
We'll need the items and we'll set this equal to basket dot items.
And then we use the select so that we can select what we want from the items.
So we'll use select and then we'll say X goes to New baskets item DTO.
And then inside here we can effectively map the properties that we want to return for our basket items.
So we're going to have well what I want in here is a product ID property.
But I'm going to need to go back to the basket item DTO.
And instead of ID here we don't need the basket item DTO.
We just need the product ID for this.
And back to our basket controller I'll specify the product ID and this is going to be equal to x dot
product ID.
And then we need the name which is going to equal x dot product dot name.
And then the price is going to be x dot product dot price.
Then the brand is going to be equal to x dot product dot.
Not the most exciting code.
This by the way this mapping type code.
So we'll only do this once really.
And then we'll create something more reusable that we can use.
There is an approach to handle this on our behalf that I've used in other courses by the way called
Automapper, but I'm not going to use that right now.
It's not much mapping code that we need to write, and I just want to demonstrate how we can do this
without adding a tool like Automapper.
And we also need a property for the picture URL.
So we'll set that equal to x dot product dot picture URL.
And we also need the quantity inside here.
So that's going to be x dot quantity.
And then we need to specify that we want this to be output to a list.
So we specify two list there and close it off with semicolon at the end.
Not where I just tried to put it at the end of the list.
We don't want it there.
We need it at the end here.
So that's our basket DTO returning the basket items inside here.
Now I didn't include the ID here actually, and I think I've got it in the basket Dito.
I don't need to send this ID back.
We've got the basket ID.
That is enough.
And if I go and take a look at the basket controller, this looks fine.
So what we should have and I'm going to restart the API server again I don't trust Hot Reload for this
for some reason.
So I'm just going to make sure this works first time and just restart the API controller.
And we see a warning there.
But that's only because we've got a method inside our controller that we haven't populated with code
yet.
The remove basket item.
We don't need to worry about that for the time being.
So now if I go and get the basket, what we should see this time is we get the basket with its items
and we've got one item in that basket.
But what we created to do that was not particularly pleasant.
And I would like a better way of shaping data when we return it.
So in the next lesson, we're going to take a look at a tool in our toolbox that helps us deal with
repetitive code so that we don't repeat ourselves.
And in the next lesson, we'll take a look at extension methods to help us do that.