Last active
June 3, 2020 22:31
-
-
Save hkakutalua/eb708ee9f47609d3a50f2c9a0e6a7715 to your computer and use it in GitHub Desktop.
Updated PeopleController
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(); | |
if (personUpdateDto.getFirstName().isPresent()) { | |
person.setFirstName(personUpdateDto.getFirstName().get()); | |
} | |
if (personUpdateDto.getLastName().isPresent()) { | |
person.setLastName(personUpdateDto.getLastName().get()); | |
} | |
if (personUpdateDto.getBirthday().isPresent()) { | |
person.setBirthday(personUpdateDto.getBirthday().get()); | |
} | |
if (personUpdateDto.getBio().isPresent()) { | |
person.setBio(personUpdateDto.getBio().get()); | |
} | |
if (personUpdateDto.getImageUrl().isPresent()) { | |
person.setImageUrl(personUpdateDto.getImageUrl().get()); | |
} | |
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