Skip to content
Aabha AI Academy

Stage 4 · L18

Separate HTTP, business decisions and database queries

Core · original Session 4

The first vertical slice was intentionally direct. Now separate responsibilities without changing behavior. The router handles HTTP declarations, request validation, Location and error presentation. The service handles domain rules and transaction ownership. The repository contains selection/query mechanics. ORM models describe stored facts and Pydantic schemas describe public representations.

Read create_equipment in the router and service side by side. The router receives EquipmentIn, calls services.create_equipment, and sets Location from the returned EquipmentOut. The service opens one transaction, constructs/flushed the row, validates the output, and commits on successful context exit. The repository does not need a FastAPI Request or HTTPException.

DomainError carries a stable code, message and intended status. A missing item is equipment_missing and can be understood by HTTP clients and service tests. The application handler translates it into a JSON envelope. Do not use driver exception text as business vocabulary, and do not catch every exception and pretend it is a missing record.

Layers are useful boundaries, not a requirement to create one class per line. Here plain functions keep the call chain visible. Follow an operation across the three files and identify the one successful commit. A function that quietly commits inside a repository would break atomic composition later. Keep focused responsibilities before adding abstractions.

Router: HTTP input/status/Location; Service: rules and one transaction; Repository: stored selection; DTO: public output before commit
Router: HTTP input/status/Location; Service: rules and one transaction; Repository: stored selection; DTO: public output before commit

Follow the running code

Focused lesson example; see the end-of-stage capstone for the cumulative app · stage 04

HTTP: parse input, call service, present status/Location
Service: decide rules, own transaction, build valid public result
Repository: choose queries, return rows, never independently commit
Model: stored facts; Schema: accepted/public representations

Predict and observe this focused example using the concepts explained above. Its boundary is stated in the focused answer.

Guided lab

  1. Read the explanation and predict the focused example’s outcome.
  2. Place Location, select-by-id, operation commit and missing-resource rule in their intended layers.
  3. Compare the observed outcome with the focused answer and state its boundary.

Expected: Location belongs to HTTP presentation; select-by-id belongs to the repository; one successful business commit and missing-resource decision belong to the service. The repository must not commit a fragment. DomainError gives a stable operation code; the HTTP handler maps it without exposing driver text.

  • A hidden repository commit can persist half a multi-row operation; catching all exceptions hides programming errors.

Focused exercise and answer

Complete this focused exercise before reading its answer. The full native transfer is introduced only at the end of the stage.

Your transfer task: Place Location, select-by-id, operation commit and missing-resource rule in their intended layers.

  1. Place Location, select-by-id, operation commit and missing-resource rule in their intended layers.
Inspect the matching answer

This answer addresses the focused exercise above; the cumulative implementation is shown only after the stage prerequisites.

Location belongs to HTTP presentation; select-by-id belongs to the repository; one successful business commit and missing-resource decision belong to the service. The repository must not commit a fragment. DomainError gives a stable operation code; the HTTP handler maps it without exposing driver text.

Check your reasoning

Must the repository know the HTTP status code?

Show the explanation

No. It performs storage work; a service expresses the domain outcome and the HTTP boundary presents it.

Reading progress

54 lessons remain open to guests. Marking a lesson read records reading only; it does not award assessment credit or a certificate.

Device reading marks require browser storage. Reading is always available.

Sign in or create an account to save separate account progress. Your current page is kept.