Jak powinno się pisać kod bez null checków z wykorzystaniem optionali. Czy to jest poprawna implementacja? Rzucanie wyjątkiem gdy nie znajdzie danego resource, a następnie obsługa w try catch taka jak tu? :P
public class ProfileInfoService {
public void saveOrUpdate(ProfileInfoDTO profileInfoDTO, UserPrincipal userPrincipal) {
User user = userService.loadUserById(userPrincipal.getId());
ProfileInfo profileInfo;
try {
profileInfo = getProfileInfo(user);
profileInfo.setDisplayName(profileInfoDTO.displayName);
profileInfo.setCommunicationEmail(profileInfoDTO.displayName);
profileInfo.setCity(profileInfoDTO.city);
profileInfo.setPostCode(profileInfoDTO.postCode);
} catch (NoResultException e) {
profileInfo = new ProfileInfo(profileInfoDTO.displayName, profileInfoDTO.communicationEmail,
profileInfoDTO.city, profileInfoDTO.postCode, user);
}
profileInfoDao.mergeProfileInfo(profileInfo);
}
public ProfileInfo getProfileInfo(User user) throws NoResultException {
ProfileInfo profileInfo = profileInfoDao.getProfileInfo(user)
.orElseThrow(() -> new NoResultException("Profile Info not found with id" + user.getId()));
return profileInfo;
}
}