I have a method in my Service for updating existing organisations.
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:
public Optional<Organisation> update(Organisation org) { return organisationRepository.findById(org.getId()) // what should be here?
Advertisement
Answer
This is what I was looking for:
public Optional<Organisation> update(Organisation org) { return organisationRepository.findById(org.getId()) .map(organisationRepository::save); }