Okay, it's time to take a look at adding a new feature into our application.
And because we're making an e-commerce type of store, then we need a shopping cart or shopping basket.
I'll stick with basket terminology for this course, but shopping cart?
Shopping basket, whichever you prefer.
And we're going to take a look at adding a basket feature in this section.
Now we're going to do both the API and the client side.
We're going to implement this feature from start to finish.
So first of all when adding a new feature we typically start with the entity, the thing that we want
to store in our database.
So we'll go to our entities folder I'll right click say New File.
And this is going to be a class.
And we'll call it basket and press return.
Inside here we're going to create some properties for our basket.
So the basket is going to have an ID.
We'll use the prop snippet to get the auto implemented property and the ID of the basket.
It's going to be an integer.
We'll also have a string for the basket ID.
And why would I have something different for a basket ID compared to the ID?
Hmm.
Well, we'll come on to that soon.
But effectively we're going to store this value as a cookie in the user's browser.
And that way we persist client side effectively.
The items in the user's basket.
I guess technically we could do the ID that way, but I'd rather this be a Guid.
And yes, we could make the ID a Guid as well, but this is the approach I'm going for.
Even if it feels like we're doubling up on IDs here, I just feel it's kind of more appropriate to have
this as a string and storing a string value in local storage.
That's where we're going to store the basket ID, and that's going to allow our user to go out and fetch
the basket from the database whenever they refresh their page, whether they go away and they come back,
they'll always be able to get their basket as long as they've got that cookie in their browser's storage.
We're going to persist this bit of information in the client browser, and then we're going to have
a list for basket item and we'll call it items.
And we're going to initialize this one to an empty array or an empty list.
This is really a shorthand nowadays for saying do list of baskets item.
But we don't need to type all of this in anymore.
In fact, we can see the ellipses there and it tells us that this can be simplified to lists nowadays.
So we don't have the basket item yet.
And we've got the nullable warning about the basket ID as well.
Now we always want our baskets to have the basket ID, so I'm going to set that to be required.
And I'm going to put my cursor inside the basket item.
Use the quick fix.
Either use the light bulb or command and period and we will generate class basket item.
And let's also put our cursor inside this, as I'd rather it be inside its own file, so we can use
the quick fix again and move type to basket item dot CSS.
And if we go to our basket item, we could just right click this, go to its definition.
And this opens up our new basket item class.
So inside here each of our basket items is also going to have an ID.
And what this effectively means is we're going to have a table in our database for baskets.
And we're also going to have a separate table in our database for basket items.
So we'll have another prop that's also an int for the quantity of the product that they're going to
have in their basket.
Now we're storing our basket in the database which means we can take advantage of relationships.
We can have a relationship between our basket item and the product that is stored in our database,
which means we don't need to specify all of the different properties of a product when we add something
to our basket or our shopping cart, and these are often referred to as navigation properties inside
a class.
and the way that we use these, because we're going to have effectively in here, we're going to have
a 1 to 1 relationship between a basket item and a product.
So we'll specify a prop of an int of product ID and a prop of products and call it product.
And once again we get the warning here.
Now again we're not going to have a basket item that isn't related to a product, nor do we want that.
So we'll also specify required for this in here.
Now thinking about it I'm on net nine now.
And what I would normally do in dotnet eight and prior for this kind of thing because this is how they
specify to do it in the documentation is we would specify our navigation property like this.
We would allow it to be nullable so that we don't get an error when we try and create a new migration.
But I want to give this a go.
I'm not 100% sure it's going to work if I use the required property.
But just for your peace of mind and my peace of mind, I'm going to give it a go anyway.
And the worst that can happen is we get an error when we try and use a migration, and we have to revert
it to the approach I demonstrated just now.
So our items are going to have a relationship with the products, a 1 to 1 relationship.
And also our basket is going to have a relationship with the basket item.
This is known as a one to many relationship.
One basket can have many items.
And in the case of a basket item, one item is related to one product.
So that's our entity class setup.
In the next lesson, I want to add a couple of methods inside our basket class, as I would like to
make it easy to add an item into our basket and also remove an item into our basket.
It sounds easy, but there's a couple of things we need to think about when we do this, and we'll take
a look at that next.