---
# System prepended metadata

title: 東毅中 Cart Service - SDD
tags: [software-docs]

---

# 東毅中 Cart Service - SDD

## A. Version

| 版本  | 內容                                  | 日期   | 負責人 |
| ---- | ------------------------------------- | ----- | ----- |
| 1.0  | Tech Stack                            | 12/05 | Ateto |
| 1.1  | Model Design                          | 12/08 | Ateto |
| 1.2  | Arch Design                           | 12/10 | Ateto |
| 1.3  | Test Cases                            | 12/12 | Ateto |
| 1.4  | API Design                            | 12/14 | Ateto |
| 2.0  | Upd `cart-04`, `cart-06`              | 12/17 | Ateto |
| 2.1  | Accecpt batch in `cart-04`, `cart-06` | 12/21 | Ateto |

## B. Tech Stack
- Spring Boot (Java)
- MySQL
- Redis (Good to Have)
- Kafka (Good to Have)

## C. Model

### 1. Cart
- id: string
- product: []Product
- create_at: string
- update_at: string

### 2. Product
- product: string
    - product id
- spec: map
    - key: size
    - quantity: int

## D. API
### 1. SaveCart
- ID: `cart-01`
- Description: 使用用戶 ID aka email 作為購物車 ID 創建購物車。
- Method: `POST`
- URI: `/cart/api/cart-create`
- Status Code: 
    - `200` : OK
    - `400` : Bad Request - 參數錯誤
    - `409` : Conflict - 購物車已存在
- Body
    - Request
        ```json
        {
            "id":"demo@gmail.com"
        }
        ```
    - Response
        ```json
        {
            "id": "demo2@gmail.com",
            "products": [],
            "create_at": "2024-11-24T11:09:06.544936+08:00",
            "update_at": "2024-11-24T11:09:06.544936+08:00"
        }
        ```

### 2. GetCart
- ID: `cart-02`
- Description: 使用用戶 ID aka email 作為購物車 ID 取得購物車資訊。
- Method: `POST`
- URI: `/cart/api/cart-get`
- Status Code: 
    - `200`: OK
    - `400`: Bad Request - 參數錯誤
    - `404`: Not Found - 購物車不存在
- Body
    - Request
        ```json
        {
            "id":"demo@gmail.com"
        }
        ```
    - Response
        ```json
        {
            "id": "demo2@gmail.com",
            "products": [],
            "create_at": "2024-11-24T11:09:06.544936+08:00",
            "update_at": "2024-11-24T11:09:06.544936+08:00"
        }
        ```
        
### 3. DeleteCart
- ID: `cart-03`
- Description: 使用用戶 ID aka email 作為購物車 ID 刪除購物車。
- Method: `Delete`
- URI: `/cart/api/cart-del`
- Status Code: 
    - `200`: OK
    - `400`: Bad Request - 參數錯誤
    - `404`: Not Found - 購物車不存在
- Body
    - Request
        ```json
        {
            "id":"demo@gmail.com"
        }
        ```
    - Response
        ```json
        {
            "id": "demo2@gmail.com",
            "products": [],
            "create_at": "2024-11-24T11:09:06.544936+08:00",
            "update_at": "2024-11-24T11:09:06.544936+08:00"
        }
        ```
        
### 4. UpdCartItem
- ID: `cart-04`
- Description
    - 使用用戶 ID aka email 作為購物車 ID 更新購物車商品。
    - 如果購物車沒有該商品會自動新增。
    - 如果該商品數量少於等於 0 則自動刪除。
    - 欲新增數量若超出商品庫存則回報錯誤。
    - 接受 batch request。
- Method: `PATCH`
- URI: `/cart/api/item-upd`
- Status Code
    - `200`: OK
    - `400`: Bad Request - 參數錯誤
    - `404`: Not Found - 購物車不存在
- Body
    - Request
        ```json
        [
            {
                "id": "demo@gmail.com",
                "product": "demo-product-id",
                "size": "L",
                "delta": 2,
                "remaining": 10
            }
        ]
        ```
    - Response
        ```json
        {
            "id": "demo@gmail.com",
            "products": [
                {
                    "product": "demo-product-id",
                    "spec": {
                        "L": 2
                    }
                }
            ],
            "create_at": "2024-11-23T15:49:17.644+08:00",
            "update_at": "2024-11-23T15:49:17.644+08:00"
        }
        ```

### 5. ClearCart
- ID: `cart-05`
- Description: 清空購物車所有商品，用於 Place Order 後續動作。
- Method: `PATCH`
- URI: `/cart/api/cart-clear`
- Status Code
    - `200`: OK
    - `400`: Bad Request
    - `404`: Not Found
    - `500`: Internal Error
- Body
    - Request
    ```json
    {
        "id": "demo@example.com"
    }
    ```
    - Response
    ```json
    {
        "message": "result of clear request"
    }
    ```

### 6. RemoveItem
- ID: `cart-06`
- Description: 
    - 移除購物車單品
    - 接受 batch request
- Method: `PATCH`
- URI: `/cart/api/item-remove`
- Status Code
    - `200`: OK
    - `400`: Bad Request
    - `404`: Not Found
    - `500`: Internal Error
- Body
    - Request
    ```json
    {
        "id": "demo@gmail.com",
        "items": [
            "product-1",
            "product-2"
        ]
    }
    ```
    - Response
    ```json
    {
        "message": "result of remove item request"
    }
    ```
