Questions Management – Core Category Service – Build API deleting category using Spring WebFlux and Spring Data MongoDB Reactive

Check out the full series of Questions Management tutorial here.

In the previous tutorial, we prepared all the information we needed to create the category deletion API: a Category document object containing information about a category, a CategoryRepository to manipulate MongoDB, a CategoryController defines the APIs of the Core Category Service that will start with “/category” and the connection information to the MongoDB server is configured in the application.properties file. Now, we will proceed to build API to delete a category.

To create a category deletion API, we will add a new method to expose a DELETE request “{id}” with the id of the category we want to delete:

You can see the post Binding variables in URI request to method’s parameters using @PathVariable annotation in Spring MVC to better understand the variable id in the above request URL.

The steps to remove a category include:

First, we need to check that the category that we want to delete does exist or not based on the id that the user passing.

Spring Data MongoDB Reactive has provided us with a way to search by id so we just need to call it.

In case this category exists, we will delete it and return the HTTP status code to 200 OK.

If it does not exist, return the HTTP status code of 404 Not Found.

The entire contents of the deleteCategory() method will now look like this:

At this point, we have completed the building API deleting category for the Core Category Service, let’s test it.

Suppose, currently in the database I have the following categories:

Questions Management – Core Category Service – Build API deleting category using Spring WebFlux and Spring Data MongoDB Reactive

so, when I request to delete the category with id is 5b22fdcc3ec1ae0367a68bf1:

Questions Management – Core Category Service – Build API deleting category using Spring WebFlux and Spring Data MongoDB Reactive

The result will be:

Questions Management – Core Category Service – Build API deleting category using Spring WebFlux and Spring Data MongoDB Reactive



The last thing we need to do is add a new Unit Test to the code we just added.

In the previous tutorial, I created a class test for CategoryController called CategoryControllerTest, a Mock object for CategoryRepository, and injected this Mock object into the CategoryController class:

Initializing the mock every time I run a test case:

Now we will add two methods to test the deleteCategory() method of the CategoryController class as follows:

A method to test if there is a category based on passing id:

A method to test for the absence of category based on passing id:

Run “Maven test” in STS or “mvn test” with Apache Maven, you will not see any errors.

Questions Management – Core Category Service – Build API deleting category using Spring WebFlux and Spring Data MongoDB Reactive
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
2Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin

Add Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.