Skip to content

Product API

Build a RESTful API for an online store. The API is organized around two core resources: product and order.

flowchart LR
    subgraph api [Trusted Layer]
        gateway --> account
        gateway --> auth
        account --> db@{ shape: cyl, label: "Database" }
        auth --> account
        gateway e5@==> product:::red
        gateway e6@==> order
        product e2@==> db
        order e3@==> db
        order e4@==> product
    end
    internet e1@==>|request| gateway
    e1@{ animate: true }
    e2@{ animate: true }
    e3@{ animate: true }
    e4@{ animate: true }
    e5@{ animate: true }
    e6@{ animate: true }
    classDef red fill:#fcc
    click product "#product-api" "Product API"

Attention

To consume the API, the user must be authenticated.

Product API

The Product API must expose the following endpoints:

POST /products

Create a new product.

1
2
3
4
5
{
    "name": "Tomato",
    "price": 10.12,
    "unit": "kg"
}

1
2
3
4
5
6
{
    "id": "0195abfb-7074-73a9-9d26-b4b9fbaab0a8",
    "name": "Tomato",
    "price": 10.12,
    "unit": "kg"
}
Response code: 201 (created)

GET /products

Get all products.

[
    {
        "id": "0195abfb-7074-73a9-9d26-b4b9fbaab0a8",
        "name": "Tomato",
        "price": 10.12,
        "unit": "kg"
    },
    {
        "id": "0195abfe-e416-7052-be3b-27cdaf12a984",
        "name": "Cheese",
        "price": 0.62,
        "unit": "slice"
    }
]
Response code: 200 (ok)

GET /products/{id}

Get a product by its ID.

1
2
3
4
5
6
{
    "id": "0195abfb-7074-73a9-9d26-b4b9fbaab0a8",
    "name": "Tomato",
    "price": 10.12,
    "unit": "kg"
}
Response code: 200 (ok)

DELETE /products/{id}

Delete a product by its ID.

Response code: 204 (no content)

GET /products/health-check

Answers 200 while the service is up. The orchestrator calls it (the compose healthcheck, the Kubernetes probe) straight on the service, not through the gateway, so it needs no token. Return nothing internal: never System.getenv(), which carries passwords and keys.

Response code: 200 (ok)

Additionals

Additional features are welcome, such as:

  • Search products by "like" name;
  • Authorization by role (admin, user):
    • Admin can create, update, and delete products;
    • User can only create orders;
  • Input validations;
  • Error handling.

Nice to have

  • Observability (metrics, logs), see Prometheus and Grafana;
  • Database In-Memory (suggestion: Product microservice), see Redis;
  • Swagger documentation, see SpringDoc.