Okay, now that we've set up our entity classes and our database schema, our next task is to create
the basket controller so that we've got endpoints which will allow our clients application to add items
to a basket, retrieve the basket and remove items from a basket.
So we're going to create a new file in the controllers folder.
I'll just create a new class.
And I'm going to call this basket controller and press return.
And this is going to derive from our base API controller we created earlier.
And we're going to have three endpoints in here.
We're going to have an HTTP get which is going to go out and get our basket.
So I'm going to say public async task.
And we're going to return an action result of type basket.
And we're going to call it get baskets.
Inside this method we will.
And in fact we need to inject our store context inside our controller as well.
So just to the right of basket controller we'll specify store context and call it context.
And then in the HTTP get method we will say var basket equals await context dot baskets.
And when we want to include a related property then we've got a few different options.
And the way that I'm going to demonstrate initially is to use something called eager loading.
And the way that we use eager loading is we tell Entity Framework what we want to include, what related
entities we wish to include with this.
So I'm going to say that I want to include the basket items.
And I'll just use X, which is a common developer shorthand way of saying instead of saying item for
the thing that we're using as our argument here.
Then a lot of developers just shortcut that and say x dot items instead.
Very common that you'll see that some developers prefer to put the word of what it is that we're querying.
But I don't know, I'm kind of okay with just using the X there.
And also we want to get the related product for the item as well.
So items have a relationship with the products.
So to get that level we need to use Len include and we'll say x at the arrow say x dot products.
And then we can use first or default async.
And there's a number of different ways of getting or finding something in our database.
I typically use first or default, but there's also single or default async as well.
There are some subtle differences between them.
They'll both do the same thing.
For our purposes.
The difference is the single or default.
If there's more than one match in the table, then it will throw an exception.
If we use first or default, it's just going to bring the first one it matches and return that.
And if they don't exist in the database at all, then it's just going to return null for both of them.
Typically I just use first or default.
They're going to do the same thing in our case.
And then inside parentheses we'll say x.
And we're going to go for x dot basket id that's equal to.
And we're going to get this ID from cookies.
And when we issue a cookie to a client then that's going to be stored in the browser in the cookies
area.
And after we've configured a 1 or 2 more things, then the idea is that that cookie gets sent up with
every request to our API.
So if they've got a cookie in their browser, it's going to be sent up with this request.
And we're going to use that to find the basket.
So I'm going to specify request dot cookies.
We've got access to them here.
And then in square brackets we're going to look for a cookie called basket ID, and we may or may not
have a basket.
So I'm going to specify if basket is equal to null I'm not going to return an error I'm going to return
no content as we don't require our users to have a basket when they visit our application.
But we will need to check to see if they've got a basket, if they've got a cookie in their browser.
So for the time being, we're just going to return no content if there is no basket for that user.
And if we do, then we're going to return the basket.
Now I'm just going to stub out two more methods and just put some comments in about what kind of things
we're going to need to do in the post, we're going to use to add an item to our basket and also for
the delete.
What we're going to need to do when we delete an item from the basket.
So the next method is going to be an HTTP post.
And we don't need to give this any route parameters.
So the next line we'll use public async task and Actionresult.
And we don't need to return anything from this necessarily because the client knows what we're adding
to the basket.
So we don't need to send anything back to say yep, this has been added to the basket.
We technically don't need to return anything from here.
So I'm just going to say add item to basket as the method name, give it an int of the product id and
an int of the quantity of what we're adding here.
And just to add some comments in about the kind of things we're going to need to do for this method
to work.
So the first thing we'd need to do is get the basket from the database.
If we do not have the baskets, then we would need to create a basket.
Then we would need to get the products they wish to add to the baskets.
Then we would need to add that item to the baskets, and then we would need to save changes so that
we update our database.
And then we would need to return something from this.
And because technically we're creating a resource in a database, the correct response is typically
a 201 for list and create an HTTP created response is a 201.
So that's what we're going to try and achieve in that method.
And I'll just flesh out what we're going to do for the delete inside here.
And this is going to be public async task.
And again action results.
And again we wouldn't necessarily return anything from an HTTP delete method.
So I'll specify remove basket item as the name of the method.
And again we're going to take the int of the product id and the increase of the quantity as the parameters
here.
And what we'll need to do in here is once again get hold of the baskets.
We would then need to remove the item or reduce its quantity.
And then we would need to save changes.
And then we could return from this.
Okay.
Obviously the comments aren't going to do anything.
And we'd need to flesh these out, which we're going to take a look at in the next lesson.
I just wanted to give you the idea of what we're going to try and do now, because I've put the steps
in here.
If you're feeling like a challenge and you want to give this a go yourself, before I actually demonstrate
how to do this, please see how far you can get with the limited comments that I've put in there.
And regardless, I'm going to go through it step by step from the next lesson.
But please feel free to have a go and see how far you can get with this.
And then I'll cover what we need to do next.