I'd like to point out that you are using Optional
badly. Generally the use of isPresent
/isEmpty
and get
are an anti-pattern. Instead in this case you should be using orElseThrow
and ifPresent
, for example like this:
AppUser user = appUserService.findByEmail(email).orElseThrow(
() =>-> new VerificationTokenException(HttpStatus.UNAUTHORIZED, "Email not found")
);
if (user.isEnabled()) {
throw new VerificationTokenException(HttpStatus.FORBIDDEN, "Something went wrong");
}
verificationTokenService.findByUser(user).ifPresent(verificationTokenService::delete);