@Transactional
public AccountTransferResponseDto 계좌이체(AccountTransferRequestDto accountTransferRequestDto, Long userId) {
if (accountTransferRequestDto.getWithdrawNumber().longValue() == accountTransferRequestDto.getDepositNumber()
.longValue()) {
throw new CustomApiException("입출금계좌가 동일할 수 없습니다. ");
}
if (accountTransferRequestDto.getAmount() <= 0L) {
throw new CustomApiException("0원 이하의 금액을 입금할 수 없습니다. ");
}
Account withdrawAccountPS = accountRepository.findByNumber(accountTransferRequestDto.getWithdrawNumber())
.orElseThrow(
() -> new CustomApiException("출금계좌를 찾을 수 없습니다. "));
Account depositAccountPS = accountRepository.findByNumber(accountTransferRequestDto.getDepositNumber())
.orElseThrow(
() -> new CustomApiException("입금계좌를 찾을 수 없습니다. "));
withdrawAccountPS.checkOwner(userId);
withdrawAccountPS.checkSamePassword(accountTransferRequestDto.getWithdrawPassword());
withdrawAccountPS.checkBalance(accountTransferRequestDto.getAmount());
withdrawAccountPS.withdraw(accountTransferRequestDto.getAmount());
depositAccountPS.deposit(accountTransferRequestDto.getAmount());
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);
return new AccountTransferResponseDto(withdrawAccountPS, transactionPS);
}
@Getter
@Setter
public static class AccountTransferResponseDto {
private Long 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);
}