DELETE /account
파라미터 : 사용자 아이디, 계좌 번호
응답 실패 : 사용자 또는 계좌가 없는 경우, 사용자 아이디오 계좌 소유자가 다른 경우, 계좌가 이미 해지 상태인 경우, 잔액이 있는 경우 실패 응답
성공 응답 : 사용자 아이디, 계좌번호, 해지 일시
응답 성공시
계좌 상태 수정 : IN_USE -> UNREGISTERED
계좌 해지일시 수정 : null -> 현재 시간
요청 / 응답 구조
요청
{
"userId" : 1,
"accountNumber" : "1000000000"
}
응답
{
"userId" : 1,
"accountNumber" : "1000000000"
}
Dto - deleteAccount
package com.example.account.dto;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import java.time.LocalDateTime;
public class DeleteAccount {
@Getter
@Setter
@AllArgsConstructor
public static class Request{
@NotNull
@Min(1)
private Long userId;
@NotBlank
@Size(min = 10, max = 10)
private String accountNumber;
}
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class Response{
private Long userId;
private String accountNumber;
private LocalDateTime unregisteredAt;
public static Response from(AccountDto accountDto){
return Response.builder()
.userId(accountDto.getUserId())
.accountNumber(accountDto.getAccountNumber())
.unregisteredAt(accountDto.getUnregisteredAt())
.build();
}
}
}
AccountController - deleteAccount
// 계좌 해제하기
@DeleteMapping("/account")
DeleteAccount.Response deleteAccount(@RequestBody @Valid DeleteAccount.Request request) {
return DeleteAccount.Response.from(
accountService.deleteAccount(request.getUserId(), request.getAccountNumber()));
}
AccountService - deleteAccount
@Transactional
public AccountDto deleteAccount(Long userId, String accountNumber){
// 사용자가 존재하는가
AccountUser accountUser = accountUserRepository.findById(userId)
.orElseThrow(() -> new AccountException(ErrorCode.USER_NOT_FOUND));
// 계좌가 존재하는 가
Account account = accountRepository.findByAccountNumber(accountNumber)
.orElseThrow(() -> new AccountException(ErrorCode.ACCOUNT_NOT_FOUND));
// 사용자 아이디와 계좌의 소유주가 같은가
validateDeleteAccount(accountUser, account);
account.setAccountStatus(UNREGISTERED);
account.setUnRegisteredAt(LocalDateTime.now());
return AccountDto.fromEntity(accountRepository.save(account));
}
private void validateDeleteAccount(AccountUser accountUser, Account account){
if(account.getBalance() > 0){ // 계좌에 금액이 남아있다면
throw new AccountException(ErrorCode.BALANCE_NOT_EMPTY);
}
if(account.getAccountStatus().equals(AccountStatus.UNREGISTERED)){
// 계좌가 이미 등록해지가 된 경우
throw new AccountException(ErrorCode.ACCOUNT_ALREADY_UNREGISTRED);
}
if(!accountUser.getId().equals(account.getAccountUser().getId())){
// 사용자와 계좌의 주인이 맞지 않은 경우
throw new AccountException(ErrorCode.USER_ACCOUNT_UN_MATCH);
}
}
'Project' 카테고리의 다른 글
[Toss] 계좌 확인(1) (0) | 2023.08.22 |
---|---|
[Toss] 계좌 해지(2) (0) | 2023.08.22 |
[Toss] 계좌 생성(4) (0) | 2023.08.21 |
[Toss] 계좌 생성(3) (0) | 2023.08.18 |
[Toss] 계좌 생성(2) (0) | 2023.08.18 |