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:
1 |
Mono<Question> updateQuestion(String questionId, Question question); |
The implementation of this method in the CoreQuestionServiceImpl class will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override public Mono<Question> updateQuestion(String questionId, Question question) { WebClient webClient = WebClient.builder() .baseUrl(getServiceUrl()) .build(); return webClient.put() .uri("/question/" + questionId) .body(BodyInserters.fromObject(question)) .retrieve() .bodyToMono(Question.class); } |
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:
1 |
Mono<CompositeQuestion> updateQuestion(String questionId, Question question); |
Implementation of this method:
1 2 3 4 5 6 7 8 9 |
@Override public Mono<CompositeQuestion> updateQuestion(String questionId, Question question) { return coreCategoryService.findById(question.getCategoryId()) .flatMap(category -> coreQuestionService.updateQuestion(questionId, question) .flatMap(q -> coreOptionService.getOptions(q.getId()) .collectList() .map(options -> new CompositeQuestion(q.getId(), q.getDescription(), category, options))) ).subscribeOn(Schedulers.elastic()); } |
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.
1 2 3 4 |
@PutMapping("/{id}") public Mono<ResponseEntity<CompositeQuestion>> updateQuestion(@PathVariable(value = "id") String questionId, @RequestBody Question question) { } |
We are just calling the updateQuestion() method of the CompositeQuestionService class:
1 2 3 4 5 6 7 |
@PutMapping("/{id}") public Mono<ResponseEntity<CompositeQuestion>> updateQuestion(@PathVariable(value = "id") String questionId, @RequestBody Question question) { return compositeQuestionService.updateQuestion(questionId, question) .map(compositeQuestion -> ResponseEntity.ok(compositeQuestion)) .defaultIfEmpty(ResponseEntity.notFound().build()); } |
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:
If I now update the first question in the list above, the id is 5ad5d9d651bd7a0aefa87fb3, the result will look like this: