Check out the full series of Questions Management tutorial here.
In the previous tutorial, we prepared all the necessary configurations to build API deletion question: an object containing all the information of a question CompositeQuestion including Question, Category and Option; a CompositeQuestionService interface with CompositeQuestionServiceImpl implementation is responsible for handling the Composite Question Service, an ApiQuestionController that defines the APIs for API Question Service will start with a “/question” and information about the Composite Question Service is configured in the application.properties file. Now, let’s build this API!
To build API delete question, first, I will add a new deleteQuestion() method in the CompositeQuestionService interface:
1 |
Mono<HttpStatus> deleteQuestion(String questionId); |
The implementation of this method in the CompositeQuestionServiceImpl class is as follows:
1 2 3 4 5 6 7 8 9 10 11 |
@Override public Mono<HttpStatus> deleteQuestion(String questionId) { WebClient webClient = WebClient.builder() .baseUrl(getServiceUrl()) .build(); return webClient.delete() .uri("/question/" + questionId) .exchange() .map(response -> response.statusCode()); } |
As you can see, here I have used the WebClient object to connect to the Composite Question Service and call the API deletion question with the “/question/{id}” URI.
Next, I’m going to add a new method in ApiQuestionController that calls the deleteQuestion() method of the CompositeQuestionService to expose a “/question/ {id}” request as follows:
1 2 3 4 5 |
@DeleteMapping("/{id}") public Mono<ResponseEntity<HttpStatus>> deleteQuestion(@PathVariable(value = "id") String questionId) { return compositeQuestionService.deleteQuestion(questionId) .map(statusCode -> new ResponseEntity<HttpStatus>(statusCode)); } |
At this point, we have completed the API deletion question for API Question Service, let’s test it out.
Assuming that our Questions Management application currently has the following questions:
then when you request to delete the question with id “5b68de7ce0d76908736648ea”, the result will be: