public ResponseEntity<Map<String, String>> resetPassword(@RequestBody String email) {
// Create password token and send by email, if email doesn't exist, don't send it
Optional<AppUser> optionalUser = appUserService.findByEmail(email);
if (optionalUser.isEmpty()) {
throw new VerificationTokenException(HttpStatus.UNAUTHORIZED, "Email not found");
}
AppUser user = optionalUser.get();
if (user.isEnabled()) {
throw new VerificationTokenException(HttpStatus.FORBIDDEN, "Something went wrong");
}
Optional<VerificationToken> optionalVerificationToken = verificationTokenService.findByUser(user);
if (optionalVerificationToken.isPresent()) {
VerificationToken verificationToken = optionalVerificationToken.get();
verificationTokenService.delete(verificationToken);
}
// ...
}
These 3 if staments are used in multiple methods in my Controller, what is a proper way to make them reusable?
I could extract each if statement into an individuel method like so, but on what level should I put them in? Should I create an util class or keep them inside my Controller class which uses them?
private Optional<AppUser> getAppUser(String email) {
Optional<AppUser> optionalUser = appUserService.findByEmail(email);
if (optionalUser.isEmpty()) {
throw new VerificationTokenException(HttpStatus.UNAUTHORIZED, "Email not found");
}
return optionalUser;
}