Created
June 3, 2020 22:49
-
-
Save hkakutalua/2d985b0064c17b449f6a1833d7de3736 to your computer and use it in GitHub Desktop.
Updated PeopleController with JsonNullableUtils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/api/people") | |
public class PeopleController { | |
... | |
@PatchMapping("{id}") | |
public ResponseEntity<Void> updatePerson(@PathVariable("id") long id, | |
@Valid @RequestBody PersonUpdateDto personUpdateDto) { | |
Optional<Person> personOptional = peopleRepository.getById(id); | |
if (!personOptional.isPresent()) { | |
return ResponseEntity.notFound().build(); | |
} | |
Person person = personOptional.get(); | |
JsonNullableUtils.changeIfPresent(personUpdateDto.getFirstName(), person::setFirstName); | |
JsonNullableUtils.changeIfPresent(personUpdateDto.getLastName(), person::setLastName); | |
JsonNullableUtils.changeIfPresent(personUpdateDto.getBirthday(), person::setBirthday); | |
JsonNullableUtils.changeIfPresent(personUpdateDto.getBio(), person::setBio); | |
JsonNullableUtils.changeIfPresent(personUpdateDto.getImageUrl(), person::setImageUrl); | |
peopleRepository.save(person); | |
return ResponseEntity.noContent().build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment