Okay.
Welcome to section seven.
And in this section we're going to take a look at adding our shopping basket feature.
I've used basket as the term I'm using in this training course for the thing that the users can put
products into.
But if you prefer to use the term shopping cart, then please feel free to do so in your application.
It doesn't make any difference at all, of course, in the functionality that we're going to add.
So inside this section we're going to add the shopping cart feature.
And I use the terms interchangeably myself even though I'm using basket in the app.
And we're also going to take a look at where to store this thing that we put products into.
There are different options, and there are pros and cons to each different option, which we'll take
a look at.
Then we're going to take a look at Entity Framework relationships, which might give you a clue as to
where we might be storing our basket.
And because we're looking at Entity Framework relationships, then we need to think about how we're
going to shape the data that we return to the client.
And that's what's coming up in this section.
But before we get into it, let's talk about where we store this thing.
And we do have options.
We could store it in local storage, we could store it in the database, and we could store it in cookies
in our browser.
And there are pros and cons to each of these different approaches.
If we take a look at local storage, for instance, then this is the simplest possible place to put
our basket.
It's very simple.
It's persistent because if the user closes their browser down and then comes back to our application,
nothing gets deleted from local storage.
It's persistent.
And because it's local storage in our browser, it's easily accessible from our JavaScript code or our
react code.
And also because it's local storage, it works offline.
If we were going to enable that kind of functionality into our app, then they would be able to add
and remove items from their shopping cart without any issues.
And the reason it works offline is one of the cons, because our server is completely unaware of what
the user's putting into or taking out of their basket, which means our marketing department, if we
had one, would have no idea what products are popular, what people are removing and adding into our
shopping cart.
So it's not a particularly useful option for a large scale e-commerce platform that does have such a
thing as a marketing department.
Also, there is limited storage in local storage.
I think 5 to 10MB is the maximum, which should be plenty, of course, for a shopping cart, but it's
just another con if you like, for local storage.
And you could claim their security risks because once again, it's local storage, easily accessible
from JavaScript code, and could open it up for a malicious user who could put and take away things
from that shopping cart.
I'm not saying that's a particularly useful thing for a malicious user to do, but it's just something
else in the cons list.
Another option would be cookies, and the pros of this one is that cookies are accessible on the server
side as well as the client side, and because it's a cookie, we can control how long it persists for
in the browser's cookie storage.
So that solves one of the problems about what's a user putting into and taking out of their basket.
But it comes with some downsides as well.
The maximum size of a cookie is four kilobytes, and it could have a performance impact because a cookie
is sent to and from the server for every single HTTP request that goes to that domain that the cookie
belongs to.
So even if they're making requests that have absolutely nothing to do with their shopping cart, they're
still sending their shopping cart backwards and forwards to the server on every single request.
So that's something else to think about as well with that approach.
Another approach, of course, is to store our shopping carts in the database.
And of course that means we get persistence.
It's secure because it's on our backend server.
It's a scalable as our server is because our server and our database would be scalable.
And it also gives our marketing team access to the analytics that we could create from their activities,
Depending on how we design the cart feature, we could enable event type behavior in our shopping cart
so that we track every time they add or remove something from that shopping cart.
We're not doing that part, by the way, but we are going to use the databases where we're storing our
shopping cart.
Some of the downsides, though, is the complexity for sure.
We're going to involve our database, so that naturally makes it more complex than either of the other
two options.
So overload is something that we need to think about as well.
It's just another thing that we're storing in our database.
Not really a major issue though, of course.
And it's of course online only.
If our server is not available, then the shopping cart cannot be retrieved or updated from the client
side.
But the database is the option that we're going to go for in this training course.
And another thing that we get if we do use the database is relationships.
We're going to be working with relational data.
And that means we can configure our database to use a relationship between our baskets, the items and
the products.
So we're going to introduce two different types of relationships in this particular section as well.
We're going to have a basket.
And we could say that our basket has many items in it.
And this would be referred to as a one to many kind of relationship.
One basket many items.
Also, our basket items are going to have a relationship with the product.
Each basket item we could say has one product or is related to a single product, and that would be
referred to as a 1 to 1 relationship.
So in this section, we're going to take a look at how we configure Entity Framework to work with tables
that are related to each other.
So we're going to have a basket table and we're going to have a basket items table.
And we're going to have a relationship between the basket and the basket items.
And also a relationship between the basket item and the product as well.
And because Entity Framework is convention based, then it's pretty straightforward to set up these
relationships using those conventions.
And that's what we're going to take a look at in this section.
So let's begin.