package shop.mtcoding.bank.service;
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import shop.mtcoding.bank.domain.account.Account;
import shop.mtcoding.bank.domain.account.AccountRepository;
import shop.mtcoding.bank.domain.transaction.Transaction;
import shop.mtcoding.bank.domain.transaction.TransactionRepository;
import shop.mtcoding.bank.dto.transaction.TransactionResponseDto.TransactionListResponseDto;
import shop.mtcoding.bank.handler.ex.CustomApiException;
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class TrsansactionService {
private final TransactionRepository transactionRepository;
private final AccountRepository accountRepository;
public TransactionListResponseDto 입출금목록보기(Long userId, Long accountNumber, String gubun, int page) {
// 1. account 존재유무
Account accountPS = accountRepository.findByNumber(accountNumber).orElseThrow(
() -> new CustomApiException("해당 계좌를 찾을 수 없습니다. "));
// 2. 계좌 소유자 확인
accountPS.checkOwner(userId);
// 3. 입출금내역 조회
List<Transaction> transactionListPS = transactionRepository.findTransactionList(accountPS.getId(), gubun,
page);
// 4. Dto
return new TransactionListResponseDto(transactionListPS, accountPS);
}
}
package shop.mtcoding.bank.dto.transaction;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import shop.mtcoding.bank.domain.account.Account;
import shop.mtcoding.bank.domain.transaction.Transaction;
import shop.mtcoding.bank.util.CustomDateUtil;
public class TransactionResponseDto {
@Getter
@Setter
public static class TransactionListResponseDto {
List<TransactionDto> transactions = new ArrayList<>();
public TransactionListResponseDto(List<Transaction> transactions, Account account) {
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 receiver;
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.receiver = transaction.getReceiver();
this.createdAt = CustomDateUtil.toStringFormat(transaction.getCreateAt());
this.tel = transaction.getTel() == null ? "없음" : transaction.getTel();
// 출금(null) 입금(값), 출금(값) 입금(null)
if (transaction.getDepositAccount() == null) {
this.balance = transaction.getDepositAccountBalance();
} else if (transaction.getWithdrawAccount() == null) {
this.balance = transaction.getWithdrawAccountBalance();
} else { // 출금(값) 입금(값)
if (accountNumber.longValue() == transaction.getDepositAccount().getNumber()) {
this.balance = transaction.getDepositAccountBalance();
} else {
this.balance = transaction.getWithdrawAccountBalance();
}
}
}
}
}
}
package shop.mtcoding.bank.web;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import shop.mtcoding.bank.config.auth.LoginUser;
import shop.mtcoding.bank.dto.ResponseDto;
import shop.mtcoding.bank.dto.transaction.TransactionResponseDto.TransactionListResponseDto;
import shop.mtcoding.bank.service.TrsansactionService;
@RequiredArgsConstructor
@RestController
@RequestMapping("/api")
public class TransactionController {
private final TrsansactionService trsansactionService;
@GetMapping("/s/account/{number}/transaction")
public ResponseEntity<?> findTransactionList(@PathVariable Long number,
@RequestParam(value = "gubun", defaultValue = "ALL") String gubun,
@RequestParam(value = "page", defaultValue = "0") Integer page,
@AuthenticationPrincipal LoginUser loginUser) {
TransactionListResponseDto transactionListResponseDto = trsansactionService
.입출금목록보기(loginUser.getUser().getId(), number, gubun, page);
return ResponseEntity.ok().body(new ResponseDto<>(1, "입출금목록보기 성공", transactionListResponseDto));
return new ResponseEntity<>(new ResponseDto<>(1, "입출금목록보기 성공", transactionListResponseDto), HttpStatus.OK);
}
}
아래 2개의 경우 모두 가능 알아서 선택