feat: org dependencies

Org endpoints use query/body model dependencies to perform initial db lookups.

Issue #6

Org ID path params have been replaced with either query params (get endpoints) or body values.

Resolves #10

Endpoints in other modules that rely on an org model lookup have also been updated.
This commit is contained in:
Chris Milne 2026-05-27 12:21:03 +01:00
parent c6a2b301dc
commit 657f91d73d
9 changed files with 106 additions and 74 deletions

View file

@ -11,18 +11,33 @@ Functions:
"""
from typing import Annotated
from fastapi import HTTPException, Depends
from fastapi import Depends, Query
from src.database import db_dependency
from src.organisation.schemas import OrgIDMixin
from src.organisation.models import Organisation as Org
from src.organisation.exceptions import OrgNotFoundException
def get_org_model(db: db_dependency, org_id: int) -> type[Org]:
org_model = db.query(Org).filter(Org.id == org_id).first()
def get_org_model_query(db: db_dependency, org_id: Annotated[int, Query(gt=0)]) -> type[Org]:
org_model = db.get(Org, org_id)
if org_model is None:
raise HTTPException(status_code=404, detail="Organisation not found")
raise OrgNotFoundException(org_id)
return org_model
org_model_dependency = Annotated[type[Org], Depends(get_org_model)]
org_model_query_dependency = Annotated[type[Org], Depends(get_org_model_query)]
def get_org_model_body(db: db_dependency, request_model: OrgIDMixin) -> type[Org]:
org_id = getattr(request_model, "organisation_id", None)
if org_id is None:
raise OrgNotFoundException
org_model = db.get(Org, org_id)
if org_model is None:
raise OrgNotFoundException(org_id)
return org_model
org_model_body_dependency = Annotated[type[Org], Depends(get_org_model_body)]