Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] 계좌 송금 시 예외 핸들러 추가 #23

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/com/flab/tess/dto/ErrorResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.flab.tess.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ErrorResponse {
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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("사용자를 찾을 수 없습니다"));
}
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/com/flab/tess/service/TransactionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/flab/tess/util/ControllerExceptionAdvice.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Loading