This video is available to students only

Creating Remove Item Method

In this lesson, we're going to create removeItem method

Creating Remove Item method#

Now we are left with creating removeItem method, so let's do that right now. This will again take the CourseId of type Guid. First of all, we will get the basket using ExtractBasket method. If the basket is null, we can return the NotFound response with new ApiResponse with 404 status code. We are not going to create a new basket in this case because customer wants to remove item which means they most probably have a basket already. If the basket is not null, we want to use the RemoveCourse method which we created and pass the courseId. Again, we will check if the changes are made to the database, using result of variable equal to context.SaveChangesAsync more than zero. If there are changes, we can send the Ok response. If there are no changes made, we can return a BadRequest, new ApiResponse with status code 400 and message, "Problem removing item from the basket".

Now we don't want to send the Basket item as it is to the client because like before, it will create a circular dependency, so let's create our BasketDto. We can copy the properties from Basket entity and paste it here. We can change BasketItem to BasketItemDto and create it in a new file. Now let's think about what we want to return to the client. We can return the courseId which is of type Guid, title of type string, price of type float, image of type url and the instructor of type string. This looks enough for now. We can now go back to the mapping profiles class and map them together. First, let's create a map from Basket to BasketDto. The next map will be between the BasketItem and the BasketItemDto. This time, we will use ForMember. First one is for the CourseId. We will map from c.Course.Id, and we can copy it four more times, replace CourseId with Title, then with price, with image and finally with the instructor.

We can go to our basket controller now, and inject automapper to our constructor. Using IMapper and importing it from autoMapper, let's call it mapper and initialize field from parameter. Wherever we are using Basket, we can replace it with BasketDto. Inside GetBasket method, let's create a new basketResponse variable and write mapper.map and inside angled brackets, we will write Basket and BasketDto, pass basket variable as parameter, and return basketResponse instead. We can copy and paste it inside our AddItemToBasket method as well, and return basketResponse in place of basket.

Now we are ready to check. Let's open postman. Open module 8 and run Add course to Basket method. It already has courseId in the query, so just press send. We see the clientId and the course, so this works well. Let's try and run this again, and this time, we see an error which means it's working fine. Let's run the get basket request, and we see the course which we just added.

Finally, let's run the remove item which is successful. Let's run the getbasket request and we see no item. Everything's working as expected. Right now, we're using the same API through which we're circulating the Cookie, but when we interact with the client, the domain changes and we need to add another method to our CORS property, so let's open our startup class and inside services.AddCors method, add AllowCredentials to the existing chain. Also, we can add OrderBy property to our chain; otherwise, we will see the OrderBy warning in our console. And that's it for now.

API/Controllers/BasketController.cs:

Start a new discussion. All notification go to the author.