Check out the full series of Questions Management tutorial here.
In the previous tutorial, we prepared all the necessary configurations to build API update a question such as: a Question document object containing the information of a question, a QuestionRepository for manipulation with MongoDB, a QuestionController defining the APIs for Core Question Service starting with “/question” and the connection information to the MongoDB server is configured in the application.properties file. Now, we are going to build this API!
To build API update a question, I will add a method to expose a PUT request “{id}” with the id as the id of the question that we need to update. We also need to define more body data as the new content of the question we will update as follows:
1 2 3 4 5 6 |
@PutMapping("/{id}") public Mono<ResponseEntity<Question>> updateQuestion( @PathVariable(value = "id") String questionId, @RequestBody Question question) { } |
The steps to update a question include:
First, we need to check whether the question that we need to update does exist or not based on the id that the user is passing on.
Spring Data MongoDB Reactive has provided us with a way to search by id so we just need to call it.
1 |
questionRepository.findById(questionId) |
In case this question exists, we will use the information passed in the body data to update the information in the database:
1 2 3 4 5 6 7 |
questionRepository.findById(questionId) .flatMap(existingQuestion -> { existingQuestion.setCategoryId(question.getCategoryId()); existingQuestion.setDescription(question.getDescription()); return questionRepository.save(existingQuestion); }) |
Then returning the new information of the question after updating the database with the HTTP status code of 200 OK.
1 2 3 4 5 6 7 8 |
questionRepository.findById(questionId) .flatMap(existingQuestion -> { existingQuestion.setCategoryId(question.getCategoryId()); existingQuestion.setDescription(question.getDescription()); return questionRepository.save(existingQuestion); }) .map(updatedQuestion -> new ResponseEntity<>(updatedQuestion, HttpStatus.OK)) |
In case this question does not exist in the database, we will return the result of the HTTP status code is 404 Not Found.
1 2 3 4 5 6 7 8 9 |
questionRepository.findById(questionId) .flatMap(existingQuestion -> { existingQuestion.setCategoryId(question.getCategoryId()); existingQuestion.setDescription(question.getDescription()); return questionRepository.save(existingQuestion); }) .map(updatedQuestion -> new ResponseEntity<>(updatedQuestion, HttpStatus.OK)) .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); |
The entire contents of the updateQuestion() method will now look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
@PutMapping("/{id}") public Mono<ResponseEntity<Question>> updateQuestion( @PathVariable(value = "id") String questionId, @RequestBody Question question) { return questionRepository.findById(questionId) .flatMap(existingQuestion -> { existingQuestion.setCategoryId(question.getCategoryId()); existingQuestion.setDescription(question.getDescription()); return questionRepository.save(existingQuestion); }) .map(updatedQuestion -> new ResponseEntity<>(updatedQuestion, HttpStatus.OK)) .defaultIfEmpty(new ResponseEntity<>(HttpStatus.NOT_FOUND)); } |
Now that we’ve finished building the API to update a question for the Core Question Service, let’s test it out.
Suppose currently in the database I have the following questions:
then when I request to update the question with id is 5b73593785ddfe03c26a11ea, the result will be: