인생 디벨로퍼

[Bank App] 12강 이체하기 본문

Project/개인 Project - Bank App

[Bank App] 12강 이체하기

뫄뫙뫄 2023. 6. 16. 21:25
728x90
반응형

Dto 만들기

package shop.mtcoding.bankapp.dto.account;

import lombok.Getter;
import lombok.Setter;

@Setter
@Getter
public class AccountTransferReqDto {
    private Long amount;
    private String wAccountNumber;
    private String dAccountNumver;
    private String wAccountPassword;
}

Controller

   @PostMapping("/account/transfer")
    public String transfer(AccountTransferReqDto accountTransferReqDto) {
        User principal = (User) session.getAttribute("principal");
        if (principal == null) {
            throw new CustomException("로그인이 필요한 서비스입니다.", HttpStatus.UNAUTHORIZED);
        }

        if (accountTransferReqDto.getWAccountNumber().equals(accountTransferReqDto.getDAccountNumber())) {
            throw new CustomException("출금계좌와 입금계좌가 동일할 수 없습니다", HttpStatus.BAD_REQUEST);
        }
        if (accountTransferReqDto.getAmount() == null) {
            throw new CustomException("amount를 입력해주세요", HttpStatus.BAD_REQUEST);
        }
        if (accountTransferReqDto.getAmount().longValue() <= 0) {
            throw new CustomException("출금액이 0원 이하일 수 없습니다.", HttpStatus.BAD_REQUEST);
        }
        if (accountTransferReqDto.getWAccountNumber() == null || accountTransferReqDto.getWAccountNumber().isEmpty()) {
            throw new CustomException("출금 계좌번호를 입력해 주세요", HttpStatus.BAD_REQUEST);
        }
        if (accountTransferReqDto.getDAccountNumber() == null || accountTransferReqDto.getDAccountNumber().isEmpty()) {
            throw new CustomException("입금 계좌번호를 입력해 주세요", HttpStatus.BAD_REQUEST);
        }
        if (accountTransferReqDto.getWAccountPassword() == null
                || accountTransferReqDto.getWAccountPassword().isEmpty()) {
            throw new CustomException("계좌 비밀번호를 입력해 주세요", HttpStatus.BAD_REQUEST);
        }
        int accountId = accountService.이체하기(accountTransferReqDto, principal.getId());

        return "redirect:/account/" + accountId;

    }

서비스 로직 

  • 출금 계좌존재 여부 확인
  • 입금 계좌존재 여부 확인
  • 출금 계좌패스워드 확인
  • 출금 잔액확인
  • 출금계좌 소유주 확인
  • 출금
  • 입금
  • 거래내역 남기기

Service

@Transactional
    public int 이체하기(AccountTransferReqDto accountTransferReqDto, int principalId) {
        // 1. 출금 계좌존재 여부 확인
        Account wAccountPS = accountRepository.findByNumber(accountTransferReqDto.getWAccountNumber());
        if (wAccountPS == null) {
            throw new CustomException("출금 계좌가 존재하지 않습니다", HttpStatus.BAD_REQUEST);
        }
        // 2. 입금 계좌존재 여부 확인
        Account dAccountPS = accountRepository.findByNumber(accountTransferReqDto.getDAccountNumber());
        if (dAccountPS == null) {
            throw new CustomException("입금 계좌가 존재하지 않습니다", HttpStatus.BAD_REQUEST);
        }
        // 3. 출금 계좌패스워드 확인
        wAccountPS.checkPassword(accountTransferReqDto.getWAccountPassword());
        // 4. 출금 잔액확인
        dAccountPS.checkPassword(accountTransferReqDto.getWAccountPassword());
        // 5. 출금계좌 소유주 확인
        wAccountPS.checkOwner(principalId);
        // 6. 출금
        wAccountPS.withdraw(accountTransferReqDto.getAmount());
        accountRepository.updateById(wAccountPS);
        // 7. 입금
        dAccountPS.deposit(accountTransferReqDto.getAmount());
        accountRepository.updateById(dAccountPS);
        // 8. 거래내역 남기기
        History history = new History();
        history.setAmount(accountTransferReqDto.getAmount());
        history.setWAccountId(wAccountPS.getId());
        history.setDAccountId(dAccountPS.getId());
        history.setWBalance(wAccountPS.getBalance());
        history.setDBalance(dAccountPS.getBalance());

        historyRepository.insert(history);
        
        // 9. ID 리던
        return wAccountPS.getId();
    }

결과

728x90
반응형