Guide For Beginners: Add POST /api/v1/contract

Canonical path: docs/howto/0004-how-to-add-post-contract.html (moved from docs/developer/0004-how-to-add-post-contract.html; the old URL redirects here).

This guide explains what to add, where to add it, and in what order.

The endpoint itself is not implemented here; this is an execution plan.


Goal and scope

Target endpoint:

Expected result:


Prepare domain model and DB (if needed)

If contract requires new table/columns:

  1. Add/adjust ORM model in app/models/core/ (for example contract.py).
  2. Project uses package-level imports for models (like from app.models.core import *), make sure to register your new model in app/models/core/__init__.py.
  3. Generate migration:
    • make migration name=add_contract_table
  4. Apply migration:
    • make migrate

If DB schema does not change, skip migration steps.


Add repository layer

Create repository for persistence operations:

Typical methods:

Repository rules:


Add service layer (business logic)

Create service:

Service responsibilities:

Error key convention for this entity:


Add request/response schemas

Create schemas:

Define:

Schema rules:


Add validation mapping for entity

Create validation mapper:

Include:

Example naming:

Then wire mapper in centralized validation entrypoint.

Current project can evolve with one of two options:


Add OpenAPI examples

Add examples:

Add:


Add API router

Create router:

Router should:

Then include router in app:


Add tests

Create tests:

Minimum scenarios:

  1. success (201)
  2. business failure (400)
  3. validation failure (422) with expected entity-prefixed code/key
  4. idempotent replay: same key + same payload returns same success result
  5. idempotency conflict: same key + different payload returns conflict

If DB changed, update test fixtures if needed:


Update docs

Run docs sync:


Run quality gates

Before commit:

  1. make format-fix
  2. make lint-check
  3. make type-check
  4. make test
  5. make pre-commit-check

Before PR/deploy:

  1. make verify
  2. make release-check

Final checklist

Page history

Date Change Author
Added Page history section (repository baseline). Ivan Boyarkin