4 API DOC
zelda edited this page 2025-08-12 14:25:54 +10:00

Products API

1. Get Product List (Paginated)

Endpoint:
GET /products

Description:
Lấy danh sách sản phẩm có phân trang.

Query Parameters (Pagination):

Parameter Type Required Description Example
page number No Trang hiện tại (mặc định 1) 1
limit number No Số bản ghi mỗi trang (mặc định 10) 20
search string No Từ khóa tìm kiếm (tùy chọn) "áo thun"
sortBy string No Sắp xếp theo field "price"
order string No Thứ tự sắp xếp (ASC hoặc DESC) "DESC"

Example Request:

GET /products?page=1&limit=20&search=áo thun&sortBy=price&order=DESC

Response (200 OK):

{
  "message": "Success!",
  "status_code": 200,
  "data": [
    {
      "created_at": "2025-08-11T21:09:14.453Z",
      "updated_at": "2025-08-11T21:09:14.453Z",
      "id": 14,
      "images": [
        "https://example.com/images/product1.jpg",
        "https://example.com/images/product2.jpg"
      ],
      "title": "MacBook Pro 14-inch 2023",
      "price": "2499.99",
      "category": "Laptops",
      "condition": "New",
      "brand": "Apple",
      "description": "MacBook Pro 14-inch với chip M2 Pro, 16GB RAM, 512GB SSD. Bảo hành chính hãng 12 tháng.",
      "tags": ["macbook", "laptop", "apple"],
      "sku": "MBP14-2023-512GBs",
      "location": "Hanoi, Vietnam",
      "histories": []
    }
  ],
  "timestamp": "2025-08-12T04:21:50.385Z",
  "current_page": 1,
  "from": 1,
  "to": 1,
  "last_page": 1,
  "per_page": 10,
  "total": 1
}


2. Create Product

Endpoint:
POST /products

Description:
Tạo mới một sản phẩm với đầy đủ thông tin chi tiết.

Request Body:

Field Type Required Description Example
images string[] Yes Danh sách URL ảnh sản phẩm ["https://example.com/img1.jpg"]
title string Yes Tên sản phẩm "Áo thun nam"
price number Yes Giá sản phẩm 199000
category string Yes Danh mục sản phẩm "Clothes"
condition string Yes Tình trạng sản phẩm "new"
brand string No Thương hiệu sản phẩm "Nike"
description string Yes Mô tả sản phẩm "Áo thun nam thể thao"
tags string[] No Danh sách tags ["thể thao", "hàng mới"]
sku string Yes SKU sản phẩm (duy nhất) "SKU001"
location string No Địa điểm lưu kho hoặc nơi bán "Hà Nội"

Example Request:

{
  "images": [
    "https://example.com/images/product1.jpg",
    "https://example.com/images/product2.jpg"
  ],
  "title": "MacBook Pro 14-inch 2023",
  "price": 2499.99,
  "category": "Laptops",
  "condition": "New",
  "brand": "Apple",
  "description": "MacBook Pro 14-inch với chip M2 Pro, 16GB RAM, 512GB SSD. Bảo hành chính hãng 12 tháng.",
  "tags": ["macbook", "laptop", "apple"],
  "sku": "MBP14-2023-512GBs",
  "location": "Hanoi, Vietnam"
}

Response (201 Created):

{
    "message": "Success!",
    "status_code": 200,
    "data": {
        "images": [
            "https://example.com/images/product1.jpg",
            "https://example.com/images/product2.jpg"
        ],
        "title": "MacBook Pro 14-inch 2023",
        "price": 2499.99,
        "category": "Laptops",
        "condition": "New",
        "brand": "Apple",
        "description": "MacBook Pro 14-inch với chip M2 Pro, 16GB RAM, 512GB SSD. Bảo hành chính hãng 12 tháng.",
        "tags": [
            "macbook",
            "laptop",
            "apple"
        ],
        "sku": "MBP14-2023-512GBs",
        "location": "Hanoi, Vietnam",
        "created_at": "2025-08-11T21:09:14.453Z",
        "updated_at": "2025-08-11T21:09:14.453Z",
        "id": 14
    },
    "timestamp": "2025-08-12T04:09:14.461Z"
}

3. Publish Product

Endpoint:
POST /products/publist/:id

Description:
Đưa sản phẩm lên trạng thái public để hiển thị cho khách hàng.

Path Parameters:

Parameter Type Required Description Example
id number Yes ID của sản phẩm cần publish 1

Example Request:

POST /products/publist/1

Response (200 OK):

{
    "message": "Success!",
    "status_code": 200,
    "data": {
        "created_at": "2025-08-11T18:26:23.580Z",
        "updated_at": "2025-08-11T18:26:23.580Z",
        "id": 11,
        "images": [
            "http://localhost:4000/api/v1/medias/products/Tesstsees/product-1754961983572.png",
            "http://localhost:4000/api/v1/medias/products/Tesstsees/product-1754961983579.png"
        ],
        "title": "Tesstsees",
        "price": "12",
        "category": "tools",
        "condition": "New",
        "brand": "cisco",
        "description": "gwegewgewgewgwegewgewgwegewgwegwegw",
        "tags": [
            "cisco",
            "abc",
            "cs",
            "css"
        ],
        "sku": "cisco102290",
        "location": ""
    },
    "timestamp": "2025-08-12T03:21:28.051Z"
}

4. Delete Product

Endpoint:
DELETE /products/:id

Description:
Xóa một sản phẩm dựa trên ID.

Path Parameters:

Parameter Type Required Description Example
id number Yes ID của sản phẩm cần xóa 1

Example Request:

DELETE /products/1

Response (200 OK):

{
    "message": "Delete success!",
    "status_code": 200,
    "data": true,
    "timestamp": "2025-08-12T04:23:57.382Z"
}

Notes

  • sku trong Create Product phải là duy nhất trong cơ sở dữ liệu.
  • Giá (price) được giới hạn 2 chữ số thập phân.
  • API publist chỉ thay đổi trạng thái sản phẩm, không chỉnh sửa dữ liệu khác.