Skip to main content
Lambda Syntax
Source Link
RoToRa
  • 11.4k
  • 1
  • 23
  • 51

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);

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);

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);
Source Link
RoToRa
  • 11.4k
  • 1
  • 23
  • 51

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);