Junit Test Application-40-계좌이체 기능

jaegeunsong97·2023년 8월 9일
0

Junit Bank Application 깃허브

Junit Bank Application 기록 노션

  • 서비스
@Transactional
     public AccountTransferResponseDto 계좌이체(AccountTransferRequestDto accountTransferRequestDto, Long userId) {
          // 1. 출금계좌 != 입금계좌
          if (accountTransferRequestDto.getWithdrawNumber().longValue() == accountTransferRequestDto.getDepositNumber()
                    .longValue()) {
               throw new CustomApiException("입출금계좌가 동일할 수 없습니다. ");
          }

          // 2. 0원 체크
          if (accountTransferRequestDto.getAmount() <= 0L) {
               throw new CustomApiException("0원 이하의 금액을 입금할 수 없습니다. ");
          }

          // 3. 출금계좌 확인
          Account withdrawAccountPS = accountRepository.findByNumber(accountTransferRequestDto.getWithdrawNumber())
                    .orElseThrow(
                              () -> new CustomApiException("출금계좌를 찾을 수 없습니다. "));

          // 4. 입금계좌 확인
          Account depositAccountPS = accountRepository.findByNumber(accountTransferRequestDto.getDepositNumber())
                    .orElseThrow(
                              () -> new CustomApiException("입금계좌를 찾을 수 없습니다. "));

          // 5. 출금 소유자 확인(로그인한 사람과 비교)
          withdrawAccountPS.checkOwner(userId);

          // 6. 출금 비밀번호 확인
          withdrawAccountPS.checkSamePassword(accountTransferRequestDto.getWithdrawPassword());

          // 7. 출금계좌 잔액 확인
          withdrawAccountPS.checkBalance(accountTransferRequestDto.getAmount());

          // 8. 이체하기
          withdrawAccountPS.withdraw(accountTransferRequestDto.getAmount());
          depositAccountPS.deposit(accountTransferRequestDto.getAmount());

          // 7. 거래내역 남기기
          Transaction transaction = Transaction.builder()
                    .withdrawAccount(withdrawAccountPS)
                    .depositAccount(depositAccountPS)
                    .withdrawAccountBalance(withdrawAccountPS.getBalance())
                    .depositAccountBalance(depositAccountPS.getBalance())
                    .amount(accountTransferRequestDto.getAmount())
                    .gubun(TransactionEnum.TRANSFER)
                    .sender(accountTransferRequestDto.getWithdrawNumber() + "")
                    .receiver(accountTransferRequestDto.getDepositNumber() + "")
                    .build();
          Transaction transactionPS = transactionRepository.save(transaction);

          // 8. DTO
          return new AccountTransferResponseDto(withdrawAccountPS, transactionPS);
     }
  • dto
@Getter
     @Setter
     public static class AccountTransferResponseDto {

          private Long id; // 계좌 ID
          private Long number; // 계좌번호
          private Long balance; // 출금 계좌 잔액
          private TransactionDto transaction;

          public AccountTransferResponseDto(Account account, Transaction transaction) {
               this.id = account.getId();
               this.number = account.getNumber();
               this.balance = account.getBalance();
               this.transaction = new TransactionDto(transaction);
          }

          @Getter
          @Setter
          public class TransactionDto {
               private Long id;
               private String gubun;
               private String sender;
               private String receiver;
               private Long amount;
               @JsonIgnore
               private Long depositAccountBalalnce; // 입금계좌 잔액
               private String createdAt;

               public TransactionDto(Transaction transaction) {
                    this.id = transaction.getId();
                    this.gubun = transaction.getGubun().getValue();
                    this.sender = transaction.getSender();
                    this.receiver = transaction.getReceiver();
                    this.amount = transaction.getAmount();
                    this.createdAt = CustomDateUtil.toStringFormat(transaction.getCreateAt());
               }
          }
     }
     .
     .
     .
     @Getter
     @Setter
     public static class AccountTransferRequestDto {

          @NotNull
          @Digits(integer = 4, fraction = 4)
          private Long withdrawNumber;
          @NotNull
          @Digits(integer = 4, fraction = 4)
          private Long depositNumber;
          @NotNull
          @Digits(integer = 4, fraction = 4)
          private Long withdrawPassword;
          @NotNull
          private Long amount;
          @NotEmpty
          @Pattern(regexp = "TRASNFER")
          private String gubun;
     }
  • 컨트롤러
@PostMapping("/s/account/transfer")
     public ResponseEntity<?> transferAccount(@RequestBody @Valid AccountTransferRequestDto accountTransferRequestDto,
               BindingResult bindingResult,
               @AuthenticationPrincipal LoginUser loginUser) {
          AccountTransferResponseDto accountTrnasferResponseDto = accountService.계좌이체(accountTransferRequestDto,
                    loginUser.getUser().getId());
          return new ResponseEntity<>(new ResponseDto<>(1, "계좌이체 성공", accountTrnasferResponseDto),
                    HttpStatus.CREATED);
     }
profile
블로그 이전 : https://medium.com/@jaegeunsong97

0개의 댓글