diff --git a/src/main/java/com/flab/tess/dto/ErrorResponse.java b/src/main/java/com/flab/tess/dto/ErrorResponse.java new file mode 100644 index 0000000..882314d --- /dev/null +++ b/src/main/java/com/flab/tess/dto/ErrorResponse.java @@ -0,0 +1,10 @@ +package com.flab.tess.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ErrorResponse { + private String message; +} \ No newline at end of file diff --git a/src/main/java/com/flab/tess/service/CustomUserDetailService.java b/src/main/java/com/flab/tess/service/CustomUserDetailService.java index d1e663a..80e9443 100644 --- a/src/main/java/com/flab/tess/service/CustomUserDetailService.java +++ b/src/main/java/com/flab/tess/service/CustomUserDetailService.java @@ -6,14 +6,12 @@ import com.flab.tess.repository.UserRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.hibernate.Hibernate; import org.springframework.cache.annotation.Cacheable; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; -import javax.transaction.Transactional; import java.security.Principal; @RequiredArgsConstructor @@ -24,7 +22,6 @@ public class CustomUserDetailService implements UserDetailsService { private final UserRepository userRepository; public User findUser(Principal principal){ - log.info("cache miss " + principal.getName()); return userRepository.findByLoginId(principal.getName()) .orElseThrow(()-> new UsernameNotFoundException("사용자를 찾을 수 없습니다")); } diff --git a/src/main/java/com/flab/tess/service/TransactionService.java b/src/main/java/com/flab/tess/service/TransactionService.java index dcacf98..4ef1f64 100644 --- a/src/main/java/com/flab/tess/service/TransactionService.java +++ b/src/main/java/com/flab/tess/service/TransactionService.java @@ -2,22 +2,17 @@ import com.flab.tess.domain.Account; import com.flab.tess.domain.Transaction; -import com.flab.tess.domain.User; import com.flab.tess.dto.*; import com.flab.tess.repository.AccountRepository; import com.flab.tess.repository.TransactionRepository; -import com.flab.tess.repository.UserRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.math.BigDecimal; import java.math.BigInteger; -import java.time.LocalDateTime; import java.util.List; import java.util.NoSuchElementException; -import java.util.Set; -import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -41,6 +36,10 @@ public Transaction saveTransaction(WithdrawalRequestDto withdrawalRequestDto){ Account sendAccount = accountRepository.findById(sender) .orElseThrow(() -> new IllegalArgumentException("송신 계좌가 존재하지 않습니다.")); + if(receiveAccount.equals(sendAccount)){ + throw new IllegalArgumentException("송신 계좌와 수신 계좌가 같습니다."); + } + //받는 사람, 보내는 사람 계좌 잔액 업데이트 Account proceedReceiveAccount = receiveAccount.deposit(amount); Account proceedSenderAccount = sendAccount.withdraw(amount); diff --git a/src/main/java/com/flab/tess/util/ControllerExceptionAdvice.java b/src/main/java/com/flab/tess/util/ControllerExceptionAdvice.java new file mode 100644 index 0000000..b29a2a4 --- /dev/null +++ b/src/main/java/com/flab/tess/util/ControllerExceptionAdvice.java @@ -0,0 +1,15 @@ +package com.flab.tess.util; + +import com.flab.tess.dto.ErrorResponse; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class ControllerExceptionAdvice { + + @ExceptionHandler + public ErrorResponse handleException(IllegalArgumentException e){ + return new ErrorResponse(e.getMessage()); + } + +}