I have a method in my Service for updating existing organisations.
JavaScript
x
public Optional<Organisation> update(Organisation org) {
Optional<Organisation> optionalOrganisation = organisationRepository.findById(org.getId());
if (optionalOrganisation.isPresent()) {
Organisation organisationToUpdate = optionalOrganisation.get();
organisationRepository.save(organisationToUpdate);
}
return Optional.empty();
}
How to refactor this method to one line?
It should be something like this:
JavaScript
public Optional<Organisation> update(Organisation org) {
return organisationRepository.findById(org.getId()) // what should be here?
Advertisement
Answer
This is what I was looking for:
JavaScript
public Optional<Organisation> update(Organisation org) {
return organisationRepository.findById(org.getId())
.map(organisationRepository::save);
}