Okay, now that we've got our basket entity created, I just want to add a couple of methods inside
here.
So it makes it easier to add and remove items from our basket items.
Now we do this in the basket class itself.
I'm just going to add a couple of methods inside here.
The first one is going to be a public method.
And we'll say public void add item.
And we're going to add a type of product into our basket.
So it's going to be a product and we'll call it product as the first argument.
And we'll add the int and quantity as the second argument.
And before we use the product, let's just add a bit of defensive code to make sure that we actually
have the product before we try and use it to add something to our basket.
So we're going to check to see if the product is equal to null.
And we're in the context of an entity here.
So the product should not be null.
But if it is null then we're going to use the arguments null exception.
And we're just going to use the throw if null method and pass in the product as an argument to that.
So if the product is null, then we break out of this method and we throw an exception.
So exceptions are exceptional and we shouldn't necessarily encounter this exception.
But just for our compiler.
And just in case we're just doing a bit of defensive coding there.
We're also going to check the quantity as well.
And we want to make sure that the quantity is not less than or equal to zero, because of course we
want that to be a positive number.
And if that is the case, then we're going to throw a new arguments exception and say as the message
the quantity should be greater than zero.
And as the second parameter here we'll use the name of and quantity for the exception.
And let's just drop this down because I'm going off the edge of the screen.
Okay.
So there's are a couple of defensive checks.
And next we'll go and get the item which could possibly already be in our basket.
So if they're adding something to our basket, then possibly they're increasing the quantity or it's
going to be a new product that we're adding to the basket.
So we'll create a variable and we'll say var existing item equals.
And we'll just use a method that we'll create to do this called find item.
And we'll pass in as the argument the product ID.
And we'll put our cursor inside the find item.
And we'll generate the method find item.
And we're going to return from this a basket item.
And we're going to make this optional because it may or may not already be in our basket.
So we're going to return possibly the basket item or NULL if we do not find it.
And we're going to return from this method items, which is what we're using what we have called our
basket items.
Then we'll use the first or default method.
And this method specifically it will find the first item that matches if it exists in our list of basket
items, or it will return the default value for a basket item.
Now, because the basket item is an object if it's not found, the default value for this is null.
So that's what we would return if it doesn't exist in our basket items.
So I'll specify item.
Then we'll use an expression.
So we use the arrow and say item dot product id is equal to product.
Or I've just called it ID here.
Let's call it product ID instead.
And I'll specify product ID here just for clarity as our items have an ID property as well as the product
ID property.
So it helps to be clear what it is we're using there.
So then we can check to see if our existing item which may be the basket item, or it may be null.
Then we can check to see if we do have that already in our basket.
So if the existing item is equal to null, then we know that we need to add this to our items.
So we're going to say items add.
And then we'll say new baskets item.
And we'll drop down to the next line.
Open curly brackets and we'll specify the product is equal to product.
And the quantity is equal to quantity.
And that's all we need to do when we're adding a product.
And then we can add the else condition.
So just below the if statement we'll specify else.
Then open curly brackets.
And we can say existing item and set the quantity property equals.
And we can say existing item dot quantity plus quantity.
And we get the little helper the ellipses here.
And let's see.
It tells us that for this particular expression we should use a compound assignment.
That sounds interesting.
Let's use the quick fix and see what this suggests.
And sure, that makes a lot of sense.
That's a lot more concise than the method I was showing there.
So great.
That's going to increase the quantity of the existing item if it was already in the basket.
So that's the add item taken care of.
And then we need two similar but different of course for the remove item.
So we're going to say publicvoid remove item.
And inside here we just need the products ID.
And again we need the quantity.
If they're removing or reducing the number of the items in the basket.
So we're going to check once again to make sure the quantity is not less than or equal to zero.
And if it is then we're going to throw a new argument exception.
And we'll just use the same message and say quantity should be greater than zero.
And once again we'll just pass in as a second parameter the name of quantity okay.
And let's drop that down as well.
So I don't go off the edge of the screen.
So then we just need to get hold of the item from our basket.
So we'll use var item equals and then we can use the find item method we created below and pass in the
product id.
And we need to check to see if the item is null because of course if we hover over the item then we
can see that possibly could be null because we can see that question mark there.
So if the item is equal to null, what do we do from this.
Well we can't really do anything.
So all we'll do is we won't throw an exception, but we'll simply return from this method and stop the
execution of the removal of the item.
Then we'll specify item dot quantity is minus equals quantity.
And then we can check to see if the item dot quantity is equal.
Or let's say less than equal to zero and equal to zero should equally do the same thing here.
I'm just being maybe overly careful, but if they've got zero or less than zero items in the basket,
then we just want to remove the item from the basket.
So I'm going to say items remove and pass in the item.
Now what's going on with these add and remove methods?
By the way, when it comes to using them, we're not doing anything with the database.
When we execute these, all we're doing is telling Entity Framework to track what the current state
of the entity is.
Because in order to use these methods, we're going to have to retrieve the entity, the basket from
the database.
And then we can use the basket functionality such as the add item or the remove item on that entity.
And when we do use one of these methods, then we're simply going to be tracking the state of that entity
in memory.
At that point, nothing happens with the database until we call a save changes method in the database.
But we'll come on to that when we use it.
So these are helper methods that just make it easier for us to use this entity class in our code or
inside our controllers when we add the endpoints for this kind of functionality.
So what we're going to take a look at next is we're going to take a look at updating our DB context.
And then we're going to take a look at creating a new migration so that we can update our database schema.
And that's what's coming up.