account 조회 1번, 동저쿼리 조회 1번
public AccountDetailResponseDto 계좌상세보기(Long number, Long userId, Integer page) {
// 1. 구분, page 고정
String gubun = "ALL";
// 2. 계좌 찾기
Account accountPS = accountRepository.findByNumber(number)
.orElseThrow(
() -> new CustomApiException("계좌를 찾을 수 없습니다"));
// 3. 계좌소유자 확인
accountPS.checkOwner(userId);
// 4. 입출금내역 조회
List<Transaction> transactionListPS = transactionRepository.findTransactionList(accountPS.getId(), gubun,
page);
// 5. Dto
return new AccountDetailResponseDto(accountPS, transactionListPS);
}
@Getter
@Setter
public static class AccountDetailResponseDto {
private Long id;
private Long number;
private Long balance;
private List<TransactionDto> transactions = new ArrayList<>();
public AccountDetailResponseDto(Account account, List<Transaction> transactions) {
this.id = account.getId();
this.number = account.getNumber();
this.balance = account.getBalance();
this.transactions = transactions.stream()
.map((transaction) -> new TransactionDto(transaction, account.getNumber()))
.collect(Collectors.toList());
}
@Getter
@Setter
public class TransactionDto {
private Long id;
private String gubun;
private Long amount;
private String sender;
private String reciver;
private String tel;
private String createdAt;
private Long balance;
public TransactionDto(Transaction transaction, Long accountNumber) {
this.id = transaction.getId();
this.gubun = transaction.getGubun().getValue();
this.amount = transaction.getAmount();
this.sender = transaction.getSender();
this.reciver = transaction.getReceiver();
this.createdAt = CustomDateUtil.toStringFormat(transaction.getCreateAt());
this.tel = transaction.getTel() == null ? "없음" : transaction.getTel();
if (transaction.getDepositAccount() == null) {
this.balance = transaction.getWithdrawAccountBalance();
} else if (transaction.getWithdrawAccount() == null) {
this.balance = transaction.getDepositAccountBalance();
} else {
if (accountNumber.longValue() == transaction.getDepositAccount().getNumber().longValue()) {
this.balance = transaction.getDepositAccountBalance();
} else {
this.balance = transaction.getWithdrawAccountBalance();
}
}
}
}
}
@GetMapping("/s/account/{number}")
public ResponseEntity<?> findDetailAccount(@PathVariable Long number,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@AuthenticationPrincipal LoginUser loginUser) {
AccountDetailResponseDto accountDetailResponseDto = accountService.계좌상세보기(number,
loginUser.getUser().getId(), page);
return new ResponseEntity<>(new ResponseDto<>(1, "계좌상세보기 성공", accountDetailResponseDto), HttpStatus.CREATED);
}