---
# System prepended metadata

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

---

# 東毅中 Product Service - SDD

## Tech Stack
- FastAPI (Python)
- MySQL

## Model

### 1. Product
- id: int
- name: str
- price: int
- size: dict
    - key 是 str 儲存尺寸
    - value 是 int 儲存該尺寸剩餘數量
- description: str
- categories: str
- discount: int
- image_url: str

## API

- prefix: `product`

### 1. Create Product
方法: `POST`

URL: `/api/product/`

請求參數:
* `product` (`Product`, Body): 要新增的產品資料

回應:
* `200 OK`: 新增成功，返回新增產品的 id (整數型態)
```json=
 {
  "id": 1,
  "name": "Product Name",
  "price": 100,
  "color": "color",
  "size": {},
  "remain_amount": 50
}
```
### 2. Delete Product
方法: `DELETE`

URL: `/api/product/{product_id}`

請求參數:
* `product_id` (路徑參數): 要刪除的產品 ID

回應:

* 200 OK: 刪除成功，返回 true
* 404 Not Found: 若產品不存在，返回錯誤訊息
```json=
{
  "result": true
}
```
### 3. Update Product
方法: `PATCH`

URL: `/api/product/{product_id}`

請求參數:

* `product_id` (路徑參數): 要更新的產品 ID
* `update_data` (`UpdateProduct`, Body): 要更新的產品資料，允許部分更新

回應:

* `200 OK`: 更新成功，返回 `true`
* `404 Not Found`: 若產品不存在，返回錯誤訊息

```json=
{
  "price": 150,
  "color": "blue"
}
```
### 4. Get Product
方法: `GET`

URL: `/api/product/{product_id}`

請求參數:

* `product_id` (路徑參數): 要查詢的產品 ID

回應:

* `200 OK`: 返回產品資訊，資料型態為 `Product`
* `404 Not Found`: 若產品不存在，返回錯誤訊息

```json=
{
  "id": 1,
  "name": "Product Name",
  "price": 100,
  "color": "red",
  "size": "M",
  "remain_amount": 50
}
```

### 5. Get All Product
方法: `GET`

URL: `/api/product`

請求參數: 無

回應:

`200 OK`: 返回所有產品列表，資料型態為 `List[Product]`

```json=
[
  {
    "id": 1,
    "name": "Product Name",
    "price": 100,
    "color": "red",
    "size": {"S":50, "M":30},
    "remain_amount": 50
  },
  {
    "id": 2,
    "name": "Another Product",
    "price": 150,
    "color": "blue",
    "size": {"S":50, "M":30, "L":20},
    "remain_amount": 30
  }
]
```

### 6. Get By Category
方法: `GET`

URL: `/api/product/categories/{category}`

請求參數: 
* `category` (路徑參數): 要查詢的商品種類

回應:

`200 OK`: 返回該分類產品列表，資料型態為 `List[Product]`

```json=
[
  {
    "id": 1,
    "name": "string",
    "price": 0,
    "color": "string",
    "size": {"S":50, "M":30, "L":20},
    "remain_amount": 0,
    "description": "string",
    "categories": "string",
    "discount": 0,
    "image_url": "2820a182-151c-4eb0-89e7-86127e46f7e1.jpeg"
  },
  {
    "id": 2,
    "name": "string",
    "price": 0,
    "color": "string",
    "size": {"S":50, "M":30, "L":20},
    "remain_amount": 0,
    "description": "string",
    "categories": "string",
    "discount": 0,
    "image_url": "ca00f852-cd6d-40bf-880b-2d67104b29b2.jpeg"
  }
]
```

### 7. Get By Name
方法: `GET`

URL: `/api/productname/{name}`

請求參數: 
* `name` (路徑參數): 要查詢的商品名稱

回應:

`200 OK`: 返回該分類產品列表，資料型態為 `List[Product]`

```json=
[
  {
    "id": 10,
    "name": "我要全部",
    "price": 1000,
    "color": "Blue",
    "size": {"S":50, "M":30, "L":20},
    "remain_amount": 200,
    "description": "Nope",
    "categories": "Glove",
    "discount": 5,
    "image_url": null
  },
  {
    "id": 11,
    "name": "我要全部",
    "price": 1000,
    "color": "Blue",
    "size": {"S":50, "M":30, "L":20},
    "remain_amount": 200,
    "description": "Nope",
    "categories": "Glove",
    "discount": 5,
    "image_url": null
  }
]
```


### 8. Upload Product Image
URL: `/api/product/upload_image`

方法: `PATCH`

請求參數: 
* `product_id`
* `image file`

回應:
```jsonld!
{
    "message" : "Image uploaded successfully",
    "image_url" : ["image_url"]
}
```

### 9. Get Product Image

URL:`/api/product/get_image`

方法: `GET`

請求參數: 
* `product_id`

回應:
```jsonld!
{
    "image_urls" : ["urls"]
}
```

### 10. Upd Stock
- Description: 確認庫存夠不夠，不夠回傳 `false`，夠的話就扣掉然後回傳 `true`。
- URL: `/api/product/stock-upd`
- Method: `PUT`
- Body: 
    - Request
    ```json
    {
        [
            {
                "id": "demo-product-id",
                "spec": {
                    "M": 2,
                    "L": 1
                }
            },
            {
                "id": "demo-product-id2",
                "spec": {
                    "L": 3,
                    "XL": 1
                }
            }
        ]
    }
    ```
    - Response
    ```json
    {
        boolean // true or false
    }
    ```
### 11. delete image
- Description: 刪除該商品的所有圖片
- URL: `api/product/delete_image/{product_id}`
- Method: `DELETE`
- 請求參數:
    - `product_id` : `int`
- Body:
    - Response
    ```JSON
        true
    ```