Skip to content

Product Service

Image title

Domain Driven Hexagonal Architecture

You are the main dev of a big online shop. The old product service is written in an esoteric language, which cannot handle more than 10.000 requests per second. Now you want to try Go, so let's do this!

Create a RESTful service with the http web-framework Gin.

It has the following endpoints:

GET     /product - List all articles
GET     /product/{id} - Get a specific article
POST    /product - Create a product
PUT     /product/{id} - Update a product
DELETE  /product/{id} - Delete a product

The product struct uses the package https://pkg.go.dev/github.com/shopspring/decimal for prices:

core/domain/product.go
import (
    "github.com/shopspring/decimal"
)

type Product struct {
    ID       int64
    Name     string
    Category string
    Price    decimal.Decimal
}

The service should have two possible ways to store products and categories. It should store all data in memory or in json files called products.json. Use interfaces, domain driven hexagonal architecture and dependency injection to built the service properly.

Example Requests:

Create products

Request:

POST /products
{
    "name": "iPhone 14 Pro",
    "price": 99.99,
    "category": "smartphones"
}

Response:

POST /products: 200
1

Request:

POST /products
{
    "name": "iPhone 12 Pro",
    "price": 999.99,
    "category": "smartphones"
}

Response:

POST /products: 200
2

Request:

POST /products
{
    "price": 999.99,
    "category": "smartphones"
}

Response:

POST /products: 400
{
    "error": "Key: 'ProductBody.Name' Error:Field validation for 'Name' failed on the 'required' tag"
}

Get products

Request:

GET /products

Response:

GET /products: 200
[
    {
        "id": 1,
        "name": "iPhone 14 Pro",
        "category": "smartphones",
        "price": "99.99"
    },
    {
        "id": 2,
        "name": "iPhone 13 Pro",
        "category": "smartphones",
        "price": "999.99"
    }
]

Get a specific product

Request:

GET /products/1

Response:

GET /products/1: 200
{
    "id": 1,
    "name": "iPhone 14 Pro",
    "category": "smartphones",
    "price": "99.99"
}

Request:

GET /products/999

Response:

GET /products/999: 404
{
    "error": "could not find product with id: 999"
}

Update a product

Request:

PUT /products/1
{
    "name": "iPhone 11 Pro",
    "category": "smartphones",
    "price": "999.99"
}

Response:

PUT /products/1: 200
{
    "name": "iPhone 11 Pro",
    "category": "smartphones",
    "price": "999.99"
}

Delete a product

Request:

DELETE /products/1

Response:

DELETE /products/1: 204