Project
[Toss] 계좌 확인(1)
sangyunpark
2023. 8. 22. 15:43
GET /account?user_id={userId}
파라미터 : 사용자 아이디
실패 응답 : 사용자 없는 경우
성공 응답 : List<계좌번호, 잔액> 구조로 응답(사용 중인 계좌만)
요청
GET /account?user_id={userId}
응답
{
{
"accountNumber" : "1000000000",
"balance" : 1000L
},
{
"accountNumber" : "10000000001",
"balance" : 1000L
},
}
AccountRepository
List<Account> findByAccountUser(AccountUser accountUser); // JPA
Account 클래스 내부에 accountUser가 존재하므로 가능하다.
UserInfo
package com.example.account.domain;
import com.example.account.dto.AccountDto;
import lombok.*;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class AccountInfo {
private String accountNumber;
private Long balance;
public static AccountInfo from(AccountDto accountDto){
return AccountInfo.builder()
.accountNumber(accountDto.getAccountNumber())
.balance(accountDto.getBalance())
.build();
}
}
Controller
@GetMapping("/account")
public List<AccountInfo> getAccountsByUserId(@RequestParam("user_id") Long userId){
return accountService.getAccountByUserId(userId).stream().map(AccountInfo::from).collect(Collectors.toList());
}
Service
@Transactional
public List<AccountDto> getAccountByUserId(Long userId){
AccountUser accountUser = accountUserRepository.findById(userId)
.orElseThrow(() -> new AccountException(ErrorCode.USER_NOT_FOUND));
List<Account> accounts = accountRepository.findByAccountUser(accountUser);
return accounts.stream()
.filter(account -> account.getAccountStatus().equals(IN_USE))
.map(AccountDto::fromEntity)
.collect(Collectors.toList());
}