Questions Management – Composite Question Service – Build API delete question using Spring WebFlux

Check out the full series of Questions Management tutorial here.

In the previous tutorial, we have prepared all the necessary configurations to be able to construct API delete question for Composite Question Service such as: an object containing all the information of a question CompositeQuestion, including Question, Category and Option; a CoreCategoryService interface with an implementation of CoreCategoryServiceImpl that deals with Core Category Service; a CoreQuestionService interface with a CoreQuestionServiceImpl implementation that handles for Core Question Service; a CoreOptionService interface with the implementation is CoreOptionServiceImpl that handles for Core Option Service; a CompositeQuestionService interface that handles Core Services; a CompositeQuestionController defines Composite Question Service APIs that start with “/question” and the Core Services information is configured in the application.properties file. Now, let’s build this API!

First, I’ll talk about the idea of ​​deleting a question. To remove a question from the Composite Question Service, you will need to call the Core Question Service to delete the question and also call the Core Option Service to clear all the options that belong to the question.

I will define a new method in the CoreQuestionService that takes care of calling the Core Question Service API to delete the question as follows:

The implementation of this method in the CoreQuestionServiceImpl class will look like:

Next, I will define a new method in the CoreOptionService that takes care of calling the Core Option Service API to remove all of the options belong to this question:

The implementation of this method in the CoreOptionServiceImpl class would look like this:

Next, I will also define a method in the CompositeQuestionService to combine the call to the Core Question Service and the Core Option Service to delete the question as follows:

Implementation của method này trong class CompositeQuestionServiceImpl sẽ như sau:

As you can see, I have called through the Core Question Service first, in case it is possible to delete the question in this service, we call Core Option Service to delete the options.

The last thing we need to do is to add a new method to the CompositeQuestionController class to expose a “/{id}” DELETE request with the id of the question we want to delete.

We are just calling the deleteById() method of the CompositeQuestionService class:

OK, here we have completed the construction of the API delete question for the Composite Question Service. Let’s test it.

Suppose I currently have the following questions:

Questions Management – Composite Question Service – Build API delete question using Spring WebFlux

If I now delete the question with id is 5b810abbbd264712e80d7f29 then the result will look like this:

Questions Management – Composite Question Service – Build API delete question using Spring WebFlux

Questions Management – Composite Question Service – Build API delete question using Spring WebFlux
Đánh giá bài viết
Chia sẽ bài viết này ...Share on Facebook
Facebook
0Share on Google+
Google+
0Tweet 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.