Orders API
Orders API
Endpoints สำหรับสร้าง ดู และจัดการ Orders
สร้าง Order ใหม่
POST /orders
Request
Headers
Authorization: Bearer <token>
Content-Type: application/json
Body
{
"product_id": "prod_abc123",
"quantity": 2,
"note": "กรุณาส่งด่วน"
}
| Field | Type | Required | คำอธิบาย |
|---|---|---|---|
product_id |
string | ✓ | ID ของสินค้า |
quantity |
number | ✓ | จำนวน (≥ 1) |
note |
string | หมายเหตุเพิ่มเติม |
Response
201 Created
{
"success": true,
"data": {
"id": "ord_xyz789",
"product_id": "prod_abc123",
"quantity": 2,
"status": "pending",
"created_at": "2026-05-12T10:00:00Z"
}
}
400 Bad Request
{
"success": false,
"error": {
"code": "VALIDATION_ERROR",
"message": "quantity ต้องมีค่ามากกว่า 0"
}
}
Sequence Diagram
sequenceDiagram
participant Client
participant API
participant DB
participant Queue
Client->>API: POST /orders { product_id, quantity }
API->>API: Validate request body
alt Validation failed
API-->>Client: 400 Bad Request { error }
else Validation passed
API->>DB: Check product stock
DB-->>API: stock: 10
alt Out of stock
API-->>Client: 400 { error: "OUT_OF_STOCK" }
else In stock
API->>DB: INSERT order (status: pending)
DB-->>API: order_id: ord_xyz789
API->>Queue: Publish "order.created" event
API-->>Client: 201 Created { data: { id, status } }
end
end
ดู Order ตาม ID
GET /orders/:id
Request
Authorization: Bearer <token>
Response
200 OK
{
"success": true,
"data": {
"id": "ord_xyz789",
"product_id": "prod_abc123",
"quantity": 2,
"status": "processing",
"created_at": "2026-05-12T10:00:00Z"
}
}
404 Not Found
{
"success": false,
"error": {
"code": "ORDER_NOT_FOUND",
"message": "ไม่พบ Order นี้"
}
}
Order Status
| Status | ความหมาย |
|---|---|
pending |
รอดำเนินการ |
processing |
กำลังดำเนินการ |
completed |
เสร็จสิ้น |
cancelled |
ยกเลิก |