기본 규칙
POST / account 요청
parameter : 사용자 아이디, 초기 잔액
policy : 사용자가 없는 경우, 계좌 19개(사용자당 최대 보유 가능 계좌 수)인 경우 실패 응답
response : 사용자 아이디, 계좌번호, 등록일시
저장이 필요한 정보
Column | Data Type | 상세 설명 |
id | pk | primary key |
accountUser | AccountUser | 사용자 테이블과 연결(n:1) |
accountNumber | String | 계좌 번호 |
accountStatus | AccountStatus | 계좌 상태(IN_USE, UNREGISTRED) |
balance | Long | 계좌 잔액 |
registerAt | LocalDateTime | 계좌 등록일시 |
unregisteredAt | LocalDateTime | 계좌 해지일시 |
createdAt | LocalDateTime | 계좌 생성일시 |
updatedAt | LocalDateTime | 계좌 수정일시 |
요청과 응답 구조
요청
{
"userId":1,
"initBalance":100
}
응답
{
"userId" : 1,
"accountNumber" : "1234567890",
"registeredAt":"2023-08-17T23:26:14.2323"
}
Domain
AccountUser
package com.example.account.domain;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Entity
@EntityListeners(AuditingEntityListener.class)
public class AccountUser {
@Id
@GeneratedValue
private Long id;
private String name;
@CreatedDate
private LocalDateTime createdAt;
@LastModifiedDate
private LocalDateTime updatedAt;
}
Account
package com.example.account.domain;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import java.time.LocalDateTime;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder // Getter,
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Account { // Account table 설정
// primary key
@Id
@GeneratedValue
private Long id;
@ManyToOne
private AccountUser accountUser;
private String accountNumber;
@Enumerated(EnumType.STRING) // 0,1,2.. 숫자가 아닌 enum을 String 형태로 그대로 저장
private AccountStatus accountStatus;
private Long balance;
private LocalDateTime registeredAt; // 계좌 등록 일시
private LocalDateTime unRegisteredAt; // 계좌 해지 일시
@CreatedDate // 자동 저장
private LocalDateTime createdAt; // 계좌 생성 일시
@LastModifiedDate // 자동 저장
private LocalDateTime updatedAt; // 계좌 수정 일시
}
Config
package com.example.account.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
@Configuration
@EnableJpaAuditing
public class JpaAuditingConfig {
}
@EnableJpaAduiting : JPA에서 시간에 대해서 자동으로 값을 넣어주는 기능
Dto
package com.example.account.dto;
import lombok.*;
import java.time.LocalDateTime;
public class CreateAccount {
@Getter
@Setter
public static class Request {
private Long userId;
private Long initalBalance;
}
@Getter
@Setter
@NoArgsConstructor // 매개변수가 없는 생성자
@AllArgsConstructor
@Builder
public static class Response {
private Long userId;
private String accountNumber;
private LocalDateTime registeredAt;
}
}
inner class static으로 요청 응답에 대한 부분을 만들어준다.
'Project' 카테고리의 다른 글
[Toss] 계좌 해제(1) (0) | 2023.08.21 |
---|---|
[Toss] 계좌 생성(4) (0) | 2023.08.21 |
[Toss] 계좌 생성(3) (0) | 2023.08.18 |
[Toss] 계좌 생성(2) (0) | 2023.08.18 |
[Toss] 설계 및 기본 구조 (0) | 2023.08.17 |