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:
1 |
Mono<Void> deleteById(String questionId); |
The implementation of this method in the CoreQuestionServiceImpl class will look like:
1 2 3 4 5 6 7 8 9 10 11 |
@Override public Mono<Void> deleteById(String questionId) { WebClient webClient = WebClient.builder() .baseUrl(getServiceUrl()) .build(); return webClient.delete() .uri("/question/" + questionId) .retrieve() .bodyToMono(Void.class); } |
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:
1 |
Mono<Void> deleteByQuestionId(String questionId); |
The implementation of this method in the CoreOptionServiceImpl class would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override public Mono<Void> deleteByQuestionId(String questionId) { WebClient client = WebClient.builder() .baseUrl(getServiceUrl()) .build(); WebClient.ResponseSpec responseSpec = client.delete() .uri("/option?questionId=" + questionId) .retrieve(); return responseSpec.bodyToMono(Void.class); } |
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:
1 |
Mono<Void> deleteById(String questionId); |
Implementation của method này trong class CompositeQuestionServiceImpl sẽ như sau:
1 2 3 4 5 6 |
@Override public Mono<Void> deleteById(String questionId) { return coreQuestionService.deleteById(questionId) .flatMap(v -> coreOptionService.deleteByQuestionId(questionId)) .subscribeOn(Schedulers.elastic()); } |
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.
1 2 3 4 |
@DeleteMapping("/{id}") public Mono<ResponseEntity<Void>> deleteQuestion(@PathVariable(value = "id") String questionId) { } |
We are just calling the deleteById() method of the CompositeQuestionService class:
1 2 3 4 5 6 |
@DeleteMapping("/{id}") public Mono<ResponseEntity<Void>> deleteQuestion(@PathVariable(value = "id") String questionId) { return compositeQuestionService.deleteById(questionId) .then(Mono.just(new ResponseEntity<Void>(HttpStatus.OK))) .defaultIfEmpty(ResponseEntity.notFound().build()); } |
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:
If I now delete the question with id is 5b810abbbd264712e80d7f29 then the result will look like this: