Making Clear Basket Endpoint

In this lesson, we're going to make clear basket endpoint

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

Thumbnail for the \newline course The newline Guide to Fullstack ASP.NET Core and React
  • [00:00 - 00:09] After the payment is successful, we want to remove the basket from Redux as well as from the database. With that, we also want to add courses into a user's account.

    [00:10 - 00:16] Let's start by going to the basket entity and make a clear basket method there. So let's open basket.cs.

    [00:17 - 00:35] Since we are going to use it from our controllers, let's make it public. So here, let me write public void and let's call this clear basket.

    [00:36 - 00:46] Inside the method, we can make payment intent ID to be null. So let's write payment intent ID and make it equal to null.

    [00:47 - 01:00] The same way we want to make client secret to be null as well. And since all our basket items are inside the items property, we can clear them using item.clear.

    [01:01 - 01:16] Now let's go to the basket controller. Now let's create a new HTTP delete request and the endpoint can be clear.

    [01:17 - 01:25] So let's write HTTP delete and let's call it clear. We have to give it a name because we already have an HTTP delete request.

    [01:26 - 01:31] So this is called clear. We are not returning anything from this function.

    [01:32 - 01:48] So let's call it public async task of action result. And let's call it remove basket.

    [01:49 - 02:06] Let's start by creating a new variable basket, which will be equal to extract method. So we will await use the extract basket method and inside, we will use get client ID method.

    [02:07 - 02:14] So it client ID. Now if the basket is null, we will return a not found error.

    [02:15 - 02:37] So if basket is null, return not found. After this, we will use basket.clear basket method, which we have just created.

    [02:38 - 02:45] I'm talking about this method. Now we can simply copy the logic from here.

    [02:46 - 02:59] So let's copy it because it's almost the same and paste it here because if result is there, that means the changes are being made. We will return okay.

    [03:00 - 03:18] Otherwise if there is any error, we will return bad request and we can change the error from problem, removing the item from the basket to problem clearing the basket. We can now go back to the agent file.

    [03:19 - 03:44] So let's go to the agent file and inside basket, we can create another clear method. This will be a delete request as well, so we can use requests.del and here we will pass basket slash clear.

    [03:45 - 04:04] Now we can go to the checkout component and inside the function and if it's successful, here we can write await agent.baskets.clear. We are now left with adding courses to users account.

    [04:05 - 04:06] Let's start working on it in the next lesson.