A REST API and CLI tool for creating and managing orders.
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
cp .env.example .env.env holds the database connection URL. The defaults in .env.example match the Docker Compose dev database — edit only if you're connecting to a different Postgres instance.
Run all commands from the project root.
Ensure .env is in place (see Setup), then start the development database:
docker compose up -d postgresApply migrations:
alembic upgrade headuvicorn src.api.main:app --reloadThe API will be available at http://localhost:8000.
Interactive docs (Swagger UI) at http://localhost:8000/docs.
| Method | Path | Description |
|---|---|---|
POST |
/orders |
Create a new order |
GET |
/orders |
List all orders (optional ?status= filter) |
GET |
/orders/{id} |
Get a single order by ID |
PATCH |
/orders/{id} |
Update an order's status |
DELETE |
/orders/{id} |
Soft-delete an order |
201— order created204— order deleted404— order not found409— order is in a terminal state (cancelledordelivered)422— request body failed validation
Unit tests need no setup:
pytest tests/orders/unitIntegration tests run against a real Postgres. Start the test database first:
docker compose up -d postgres-test
pytestWithout the test database, integration tests are skipped with a hint (pytest -rs shows the reason). In CI they always run.
Strict mode is set in pyproject.toml:
mypy .The CLI communicates with the API over HTTP. Make sure the API is running before using it.
python -m cli.main create --customer "Alice" --item "Widget" --quantity 3python -m cli.main list
python -m cli.main list --status shippedpython -m cli.main get 3f2a1b4c-8e9d-4f0a-b1c2-d3e4f5a6b7c8python -m cli.main update <order_id> <status>Example:
python -m cli.main update 3f2a1b4c-8e9d-4f0a-b1c2-d3e4f5a6b7c8 shippedpending, confirmed, shipped, delivered, cancelled, disputed
Two bounded contexts, orders and auth, each with the same layered layout. Only orders is implemented. auth is a placeholder for a future service: it has a user model and repository, and its API only serves GET /health. There's no registration, login or token handling, and the orders API isn't protected.
ordering-system/
├── src/
│ ├── orders/
│ │ ├── domain/ # Pure business logic, no I/O
│ │ │ ├── order.py # Order, OrderItem, OrderStatus
│ │ │ ├── state_machine.py # Allowed status transitions, terminal statuses
│ │ │ ├── exceptions.py # OrderNotFoundError, InvalidTransitionError
│ │ │ └── repository.py # OrderRepositoryProtocol (abstract interface)
│ │ ├── application/
│ │ │ └── services/
│ │ │ └── order_service.py # Use cases, depends only on domain
│ │ ├── infrastructure/
│ │ │ └── db/
│ │ │ ├── connection.py # SQLAlchemy engine and session
│ │ │ ├── models.py # ORM models (OrderModel, OrderItemModel)
│ │ │ └── repositories/
│ │ │ └── order_repository.py # Concrete repository implementation
│ │ ├── api/
│ │ │ ├── main.py # FastAPI routes, dependency wiring
│ │ │ └── schemas.py # Pydantic request/response schemas
│ │ ├── alembic/ # Orders migrations (alembic.ini)
│ │ └── config.py # DATABASE_URL from environment
│ └── auth/ # Placeholder, not built yet (see above)
│ ├── domain/ # User, UserRepositoryProtocol
│ ├── infrastructure/db/ # UserModel, UserRepository
│ ├── api/ # FastAPI app with GET /health
│ ├── alembic/ # Auth migrations (alembic-auth.ini)
│ └── config.py # AUTH_DATABASE_URL from environment
├── cli/
│ └── main.py # CLI client (communicates with API via HTTP)
└── tests/
├── conftest.py # Default DATABASE_URL for a fresh clone
└── orders/
├── unit/ # No database needed
│ ├── test_order.py # Order aggregate and status transitions
│ ├── test_order_service.py # Service layer (mocked repository)
│ ├── test_schemas.py # Pydantic schema validation
│ └── test_cli.py # CLI client
└── integration/ # Real Postgres (postgres-test)
├── conftest.py # Engine, per-test transaction rollback, skip guard
├── test_order_repository.py # Repository against the database
└── test_orders.py # Full stack through the API