반응형
회원 도메인에서 Username이 중복이 되면 안되므로 아래 같이 로직을 추가했습니다.
Optional<Account> account = accountRepository.findByUsername(username);
if (account.isPresent()) throw new AccountDuplicateException(username);
하지만 Optional을 반환하지 말고 ifPresent
를 사용하면 바로 Exception을 처리할 수 있습니다.
accountRepository.findByUsername(username)
.ifPresent(a -> {
throw new AccountDuplicateException(username)
});
훨씬 더 간결해지고 알아보기 쉬운 코드가 되었습니다.
감사합니다.
반응형
반응형
회원 도메인에서 Username이 중복이 되면 안되므로 아래 같이 로직을 추가했습니다.
Optional<Account> account = accountRepository.findByUsername(username);
if (account.isPresent()) throw new AccountDuplicateException(username);
하지만 Optional을 반환하지 말고 ifPresent
를 사용하면 바로 Exception을 처리할 수 있습니다.
accountRepository.findByUsername(username)
.ifPresent(a -> {
throw new AccountDuplicateException(username)
});
훨씬 더 간결해지고 알아보기 쉬운 코드가 되었습니다.
감사합니다.
반응형