Creating Basket Entity
In this lesson, we're going to create basket entity
Creating basket entity#
Like any e-commerce application, an e-learning platform has a basket where you can keep your courses and pay for them while checking out. When it comes to storing the basket data, you can either keep it entirely with the client using the local storage by storing it inside in-memory databases such as redis or memcached, or you can store it inside the database. Like any other client information, we're going to use the later one because there's no harm in doing so, and it helps to make some additional functionalities such as reminding user about the cart products using emails or telling them about the discounts.
To start this functionality, we will have to create a Basket entity first, so let's go to our Entity project and create a new class called Basket. To start off, we need an Id to make it unique. Now we have to decide if we want users to sign in before making a purchase or not. Since we haven't seen the user's login till now, we can make it available for everyone. Let's create a new property called ClientId which will be a string. We will create a random string from our client side and assign to a user. Most importantly, we want a list of Basket items, and call it items. Let's import List from System.Collections.Generic and let's create BasketItem entity in a new file. Let's also initialize a new list of Basket items so that it's never undefined.
Let's create our BasketItem class now. We need the Id of the item which will be of type string. Now we need the courseID as well, of type Guid which is being bought, which means we will create a navigation property here, so we also need to create a Course entity with name, Course. If we were building an e-commerce application, we would also need the quantity but there's no point in buying the course more than once. Also, since we are creating a relationship with the basket, we also need to provide the BasketId of type int and Basket of type Basket. This is the same thing we did between Category and Course entity relationship.
Let's go back to our Basket entity and create methods to add and remove course from the cart. Let's create AddCourse method first. This will be public void AddCourse which will accept the course. Inside, we will use the All method which is provided by Linq that checks if all the elements satisfy the condition. If they do, it returns true; otherwise, it will return false. Let's import All from System.Linq. We will check if the course id of the item does not exist anywhere in the basket. If it doesn't, which means the course is not in the basket, we will add a new item to the basket which will be a new BasketItem and it will accept the course. The id of the basket item will be automatically generated.
Let's create RemoveCourse method now. It will accept only the courseId which will be of type Guid, so let's import it using System.Linq. Let's store the course in a variable and use Items.FirstOrDefault which will take an expression. item.CourseId is equal to the courseId passed to this method. Finally, we will use Items.Remove and pass this course.