Questions Management – Composite Question Service – Build API update 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 build API update question in the Composite Question Service: an object that contains 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 the Core Question Service; a CoreOptionService interface with the implementation is CoreOptionServiceImpl that handles the Core Option Service; a CompositeQuestionService interface that handles Core Services; a CompositeQuestionController defines APIs for Composite Question Service that start with “/question” and the Core Services information is configured in the application.properties file. Now, we are going to build this API!

To build API update question, I first will define a new method in the CoreQuestionService that takes care of calling the Core Question Service API to update question as follows:

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

Next we will also define a new method in the CompositeQuestionService that will call the updateQuestion() method of the CoreQuestionService class and combine the information from the Core Category Service and Core Option Service to return to the user after updating the question.
The contents of this method are as follows:

Implementation of this method:

As you can see, here I will check if the category id which this question belongs to exists in the system or not by calling the Core Category Service to look up category by id. In case this category exists, we just call the updateQuestion() method of the CoreQuestionService class to update the question.

After updating the question, we need to call Core Option Service to get all options of this question to build the result.

The last thing we need to do is to add a new method to the CompositeQuestionController class to expose a PUT request with the data in the body as the content of the question to be updated.

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

Here, in case you do not find the category id which this question belongs to, an HTTP 404 Not Found error will be returned to the user as you see in the code above.

OK, here we have finished building the update question API for Composite Question Service. Let’s test it.

Suppose I currently have the following questions:

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

If I now update the first question in the list above, the id is 5ad5d9d651bd7a0aefa87fb3, the result will look like this:

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

Add Comment