Okay, now that we've got the logic to both retrieve a basket and add an item into the basket, this
gives us enough to test what's going to happen when we use these different endpoints.
Now, because we've got some actual logic to take a look at now, maybe now's worthwhile to take a look
at what's going to happen via the debugger as we go through this code.
So I'm just going to add a breakpoint onto line 24, in my case where we go ahead and retrieve the basket.
And this will give us an opportunity to see what is available inside our code and step through the code
and effectively check the logic as we're going through and see what information we have available to
us.
So now's a good opportunity to do that and what we might want to do, because we've added some new code
and we have a new controller, is just restart our API server before we go ahead and do this.
And we can use control R to restart this.
So I'll get that Underway.
And I'll talk about this soon because this is something new in dotnet nine and now that has restarted,
let's go ahead and run our debugger.
So I'll hit the debugging tab and we want to use the dotnet core attach one.
So I'll hit the play button.
And then I'll search for API inside.
Here I'll select API and off the debugger goes and starts running.
And let's head over to postman to test these requests.
So inside section seven we've got a request here to go and get the basket.
Now we haven't added a breakpoint to this.
We don't have a basket for anybody just yet.
So if I send this request, what we would expect to get based on our code is a 200 success, but no
content for the basket.
So let's click send or not a 200.
A 200 for no content is what we would expect from that.
That's fine.
That's what we would expect.
Now, if we take a look at adding an item to the basket.
I've got a product ID equal to two and a quantity equal to one.
And these are being passed up as query string parameters.
Now if I go and take a look at the code that we have inside our basket controller, then these are the
two parameters.
And the name of what we're using here has to match.
So if I want product ID this argument to be bound to the HTTP request that I'm making so that the controller
can use these parameters for the product ID and the quantity, then the names have to match exactly
the same.
So product ID is product id and quantity is quantity.
So they have to match.
Otherwise the API controller won't automatically bind to them.
Now when it comes to API controllers and conventions, what we have here is passing in two properties
as arguments.
Now these are not going in the body of the Request, but because they're simple integers or they could
be strings, then our API controller is automatically going to check the query string parameters and
see if it can bind to these two properties.
We don't need to tell it where to find these parameters.
It's going to know.
And we get that functionality, that automatic binding because of this API controller attributes.
That's one of its features.
It provides that auto binding between the things like the query string.
If we're sending up parameters in that way, or if we're sending up properties in the body of a request,
then the API controller will automatically bind them to that.
If we were not using the API controller attribute for whatever reason, then we would need to tell our
API where to find this by using, in this case from query for each of these properties.
But we don't need that because of the way that we're using that API controller.
So let's take a look.
So all we need for this is just a product ID and a quantity of that product.
And what should happen is we go and create a new basket and then return that basket.
Now just to set the expectations, what we've got in our code, I'll just quickly go back here.
This line here is going to give us a problem returning the basket from this request.
The way that we have our code at the moment is going to be a problem.
So I'm not expecting this to work, but we will get to the point where list line saving the changes
into the database does work.
It's just returning the basket at this stage is going to be problematic.
So let's give it a go and I'll click send.
And what should happen is we're taken to our breakpoint in the basket controller.
And indeed we are.
And we can see straight away that we have the quantity and the product ID correctly sets based on the
parameters that we've passed in to this request.
So that's the first thing we really want to look at when we're debugging something, are we getting
the values that we'd expect here?
If these were anything different, then we would know that we've got an issue that we need to take a
look at.
So the first line here is to set the basket and go ahead and retrieve it.
So if I.
Well we've got options here with our debugger controls we can step over which will take us to the next
line.
But because we've got a method inside here called Retrieve Basket, we can actually step into this which
will take us into the retrieve basket method.
And sure enough, it does.
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,
and this is going to return to us from our database if we have it a basket based on the cookies.
So where does the cookie come from?
Well, it comes from the request.
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
inside here we've got our cookies.
And if we had a cookie, then it would be visible inside here.
Of course, we don't have a basket yet, so we do not have a cookie.
So we know from this method that we're not going to return a basket.
So I'm just going to step over and step over again.
And this takes us to the next line of code.
So currently our basket if we take a look at our debugger our basket is currently null.
And we know that this line of code is going to create a basket if the basket is null.
So once again let's step inside and take a look at the create basket method which it's now taken us
to.
So the first line is to create the basket ID.
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
a Guid formatted as a string.
Then we create some cookie options and we set the is essential property and the expiry.
So if we step over this line then we'll be able to take a look at our cookie options that are available,
and specifically the properties that we've set the is essential and the expires, which is a date time
object.
Okay.
So we'll keep going and then we'll step over again.
And at this point we do create a new basket.
And if we step over this line then at the moment we have a basket now created in memory with the basket
ID.
And it doesn't have an ID at this stage.
The reason for this is because it's not currently saved into our database.
We need SQLite to generate that ID, and once it has been saved into our database, it will be given
an ID.
So even if we step over the next line, the context baskets and then our basket still at this point
does not have an ID, and that's because nothing's happened with our database yet.
So we can step over and step over again.
And now we have the basket.
And if we go to the next line, next task is to go out and find the product.
So we'll step over that.
And what we should have now is our product populated with all of its properties.
And then we can go ahead and add an item into our basket.
So we'll step over this.
And if we take a look at our basket now what we should have is a list of items with a count of one.
And inside this item we have the product.
And again we've got things like the ID that's set to zero.
But we do have the quantity.
The product ID still is set to zero inside there.
That should be populated once we save our entity into the database.
But that doesn't happen until this line.
So if we do step over this line, then we go to the result and we can see that our result is equal to
true, which means our database has now been updated.
And what we can also see is because we've now saved this into the database.
Our basket has an ID of one.
The product ID inside the basket items has been set as well to the product ID based on what we retrieve
from the database.
And we're looking good.
Our basket has been given an ID as well.
Now, I mentioned that this isn't going to work because I expect that we're going to get an exception
based on what we're doing here.
And if I step over again and step over again, then no exception.
Okay, so we've gone to the last line, which means we have returned something to postman, and we'll
just click play to finish the request.
So we didn't get an exception, but what did we get.
And we've actually got the basket item.
Okay.
So interestingly I've got an internal server error and it seems I've actually got the response as well.
But if we take a look at this Response.
Then this is kind of what I was expecting to see.
What I was really expecting to see was that we encountered a cyclic error, because what we have here
is we've got our basket being returned with some items.
Our items are returning products.
And because we have fully defined the basket inside there.
So we see the basket again.
And that basket has items so on and so forth.
What's happening here is our API controller is attempting to serialize the object into JSON format.
And because of the way that we've defined our entity for the basket item, if I go to the entities and
basket item, this is the problem right here where we've got our basket inside our basket items.
When it's trying to format this into JSON, it keeps using the basket over and over again.
And because we're including the basket items as well as when we return this, it's is getting itself
into a bit of a pickle.
And that used to return an exception that we would see inside postman.
But if you do see something slightly different, don't worry, we're going to fix this very soon.
But what we do have now, and what we can also confirm, if we take a look at our database and the store
DB, then what we should find is that inside our baskets table, we've got a new basket.
And inside the baskets item table we have a product inside there.
Not a great deal of detail because it's a related entity.
Then we don't see the full product details inside here or the full basket.
We just see the IDs of those and that's absolutely fine.
So that's a more useful use of using the debugger.
But we do have a problem as we can clearly see inside postman.
This is clearly not good.
Sending our data back like this.
What a mess.
And we'll take a look at shaping our data next so that we avoid this kind of situation.