[ 2주차 과제 ] 시퀀스 다이어그램, ERD

2025. 4. 1. 23:57향해99 8기

시퀀스 다이어그램

1. 잔액 충전 요청 및 이력 저장

요구사항 1번 잔액 충전 (식별자는 요청 본문에 포함)

더보기
sequenceDiagram
    participant User
    participant Point
    participant PointHistory

    User->>Point: [POST] /api/v1/points/charge<br>잔액 충전 요청
    Point->>Point: 잔액 충전
    Point->>PointHistory: 잔액 충전 이력 저장
    Point->>User: 잔액 충전 결과 반환

2. [GET] /api/v1/points?userId={userId} : 잔액 조회 요청 및 결과 반환

요구사항 1번 잔액 조회

더보기
sequenceDiagram
    participant User
    participant Point

    User->>Point: [GET] /api/v1/points?userId={userId}<br>잔액 조회 요청
    Point->>Point: 잔액 조회
    Point->>User: 잔액 조회 결과 반환

3. [GET] /api/v1/products : 상품 조회 요청 및 결과 반환

요구사항 2번 상품 조회 (ID, 이름, 가격, 잔여수량)

더보기
sequenceDiagram
    participant User
    participant Product

    User->>Product: [GET] /api/v1/products<br>상품 조회 요청
    Product->>User: 상품 조회 결과 반환

4. [POST] /api/v1/coupons/issue : 선착순 쿠폰 발급 처리 및 결과 반환

요구사항 3번 선착순 쿠폰 기능

더보기
sequenceDiagram
    participant User
    participant CouponController
    participant CouponService
    participant CouponRepository
    participant UserCouponRepository

    User->>CouponController: [POST] /api/v1/coupons/issue<br>선착순 쿠폰 발급 요청
    CouponController->>CouponService: 쿠폰 발급
    CouponService->>CouponRepository: 잔여 쿠폰 수량 조회
    CouponRepository->>CouponService: 잔여 쿠폰 수량 반환

    alt 잔여 수량 = 0
        CouponService->>User: 쿠폰 발급 실패
    else 잔여 수량 > 0
        CouponService->>UserCouponRepository: 쿠폰 발급 이력 조회
        UserCouponRepository->>CouponService: 쿠폰 발급 이력 반환

        alt 발급 이력 존재
            CouponService->>User: 쿠폰 발급 실패
        else 발급 이력 없음
            CouponService->>CouponRepository: 잔여 쿠폰 수량 차감
            CouponService->>UserCouponRepository: 쿠폰 발급 이력 저장
            CouponService->>CouponController: 발급된 쿠폰 정보 반환
            CouponController->>User: 쿠폰 발급 결과 반환
        end
    end

5. [GET] /api/v1/coupons?userId={userId} : 보유 쿠폰 목록 조회

요구사항 3번 ㅡ> 선착순 쿠폰 발급 후 사용자가 발급받은 쿠폰 목록 조회 기능

더보기
sequenceDiagram
    participant User
    participant Coupon

    User->>Coupon: [GET] /api/v1/coupons?userId={userId} <br>보유 쿠폰 목록 조회 요청
    Coupon->>Coupon: 쿠폰 목록 조회
    Coupon->>User: 보유 쿠폰 목록 조회 결과 반환

6. [POST] /api/v1/orders : 주문 생성 및 재고/쿠폰 처리

요구사항 4번 주문 (사용자 식별자, 상품 ID, 수량)
재고가 충분할때 Order에서 Product로
재고 차감을 요청해야하는 것이 아닌가??? 

ㅡ> 주문 시에 재고 차감

더보기
sequenceDiagram
    participant User
    participant Order
    participant Product
    participant Coupon

    User->>Order: [POST] /api/v1/orders<br>주문 생성 요청
    Order->>Product: 재고 조회
    Product->>Order: 재고 반환

    alt 재고 부족
        Order->>User: 주문 실패
    else 재고 충분
        Order->>Product: 재고 차감요청
        Product->>Product: 재고 차감
        Order->>Coupon:쿠폰 적용 요청
        Coupon->>Coupon: 쿠폰 유효성 검증 및 쿠폰 적용
        Coupon->>Order: 쿠폰 적용 금액 반환
        Order->>Order: 주문 상태 업데이트 (NOT_PAID)
        Order->>Order: 주문 저장
        Order->>User: 주문 생성 결과 반환
    end

7. [POST] /api/v1/points/use : 포인트 결제 처리 및 주문 상태 업데이트

요구사항 4번 결제 (결제 성공시 -> 실시간으로 주문 정보를 데이터 플랫폼에 전송)

더보기
sequenceDiagram
    participant User
    participant Point
    participant Order
    participant DataPlatform

    User->>Point: [POST] /api/v1/points/use <br>결제 요청
    Point->>Point: 잔액 조회
    alt 잔액 부족
        Point->>User: 결제 실패
    else 잔액 충분
        Point->>Point: 포인트 차감
        Point->>Point: 포인트 차감 이력 저장
        Point->>Order: 주문 상태 변경
        Order->>Order: 주문 상태 업데이트 (PAID)
        Point->>DataPlatform: 주문 정보 전송
        Point->>User: 결제 성공 결과 반환
    end

8. 결제 실패 후 주문 상태 EXPIRED 처리 및 쿠폰 사용 취소
요구사항 4번 ㅡ> 결제 취소시 처리

더보기
sequenceDiagram
    participant FailOrderScheduler
    participant Order
    participant Product
    participant Coupon

    FailOrderScheduler->>Order: 결제가 안된 채로 5분이 지난 주문 조회 요청
    Order->>Order: 결제가 안된 채로 5분이 지난 주문 조회
    Order->>FailOrderScheduler: 결제가 안된 채로 5분이 지난 주문 반환
    FailOrderScheduler->>Order: 주문상태 EXPIRED처리 요청
    Order->>Order: 주문 상태 EXPIRED 처리
    FailOrderScheduler->>Product: 재고 롤백 요청
    Product->>Product: 재고 롤백 처리
    FailOrderScheduler->>Coupon: 쿠폰 사용 여부 조회 요청
    Coupon->>FailOrderScheduler: 쿠폰 사용 여부 반환

    alt 쿠폰 사용 취소
        FailOrderScheduler->>Coupon: 쿠폰 사용 false 처리 요청
        Coupon->>Coupon: 쿠폰 사용 여부 false
    end

9. 주문 완료된 상품 조회 및 일별 주문량 저장

요구사항 5번 ㅡ> 판매량 상위 5개 상품을 계산하는 데 필요한 통계 정보를 준비

더보기
sequenceDiagram
    participant BestSellerScheduler
    participant Order
    participant BestSeller

    BestSellerScheduler->>Order: 주문 완료된 상품 조회 요청
    Order->>Order: 주문 완료된 상품 조회
    Order->>BestSellerScheduler: 주문 완료된 상품 반환
    BestSellerScheduler->>BestSeller: 각 상품의 일별 주문량 저장

10. [GET] /api/v1/products/best : 판매량 상위 5개 상품 조회

요구사항 5번 상위 상품 조회 API (통계 정보)

더보기
sequenceDiagram
    participant User
    participant BestSeller

    User->>BestSeller: [GET] /api/v1/products/best<br>판매량 상위 5개 상품 조회 요청
    BestSeller->>User: 판매량 상위 5개 상품 반환

 

 

 

 

 

ERD

 

ERD 초기 시안

플랫폼 : https://excalidraw.com/

플랫폼 : https://dbdiagram.io/d

더보기

Table user {
  id integer [primary key]
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Table orders {
  id integer [primary key]
  user_id integer [not null] //유저ID
  user_coupon_id integer [ref: - user_coupon.id, unique] //유저쿠폰 ID - 사용 안하면 null
  is_coupon_applied boolean [not null] //쿠폰 사용 여부
  total_amount integer [not null] //총 주문금액
  status varchar [not null] //주문상태(NOT_PAID, CANCEL, PAID)
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Table order_product {
  id integer [primary key]
  product_id integer [not null] //상품ID
  orders_id integer [not null] //주문ID
  amount integer [not null] //주문금액
  quantity integer [not null] //주문수량
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Table product {
  id integer [primary key]
  product_name varchar [not null] //상품명
  description blob //상품 상세정보
  price integer [not null] // 상품 가격
  stock integer [not null] //상품 재고
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Table point {
  id integer [primary key]
  user_id integer [not null] //유저ID
  balance integer [not null] //잔고
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Table point_history {
  id integer [primary key]
  point_id integer [not null]
  amount integer [not null] //충전/사용액
  balance integer [not null] //충전/사용 당시 잔고
  type varchar [not null] //충전 or 사용
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Table coupon {
  id integer [primary key]
  coupon_name varchar [not null] //쿠폰 이름
  discount_value decimal [not null]// 퍼센트 or 값
  discount_type varchar [not null] // 할인 정책(정률/정액)
  start_date date [not null] //유효기간 시작일 
  end_date date [not null] //유효기간 종료일
  stock integer [not null] // 쿠폰 재고 
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Table user_coupon {
  id integer [primary key]
  user_id integer [not null] //유저ID 
  coupon_id integer [not null] //쿠폰ID
  is_used boolean [not null] //쿠폰 사용 여부 
  coupon_name varchar [not null] //쿠폰 이름
  issued_at date [not null] //유효기간 시작일
  expried_at date [not null] //유효기간 종료일
  created_at timestamp [not null]
  updated_at timestamp [not null]
}

Table best_seller {
  id integer [primary key]
  product_id integer [not null] //상품ID
  product_name varchar [not null] //상품명
  description blob [not null] //상품 상세정보
  stock integer [not null] //재고
  sales integer [not null] //판매량
  created_at timestamp [not null]
  updated_at timestamp [not null]
}
 
Ref product_order_item : order_product.product_id > product.id
Ref orders_order_item : order_product.orders_id > orders.id
Ref users_orders : orders.user_id > user.id
Ref point_point_history : point_history.point_id > point.id
Ref coupon_user_coupon : user_coupon.coupon_id > coupon.id
Ref user_user_coupon : user_coupon.user_id > user.id

Ref: "user"."id" - "point"."user_id"