|
3 | 3 | from fastapi import APIRouter, HTTPException, Query, status |
4 | 4 |
|
5 | 5 | from src.common.errors import AUTHZ_001, RES_001, raise_api_error |
6 | | -from src.lib.authorization import authorize_relation_access |
| 6 | +from src.common.models import PaginatedResponse, PaginationParams |
| 7 | +from src.lib.authorization import authorize_host_access, authorize_relation_access |
7 | 8 | from src.lib.dependencies import CurrentUser, DBSession |
8 | 9 | from src.relations import service |
9 | 10 | from src.relations.schemas import ( |
@@ -42,44 +43,56 @@ async def create_care_relation( |
42 | 43 | return CareRelationResponse.model_validate(relation) |
43 | 44 |
|
44 | 45 |
|
45 | | -@router.get("", response_model=list[CareRelationResponse]) |
| 46 | +@router.get("", response_model=PaginatedResponse[CareRelationResponse]) |
46 | 47 | async def list_care_relations( |
47 | 48 | db: DBSession, |
48 | 49 | user: CurrentUser, |
49 | 50 | host_id: uuid.UUID | None = Query(default=None), |
50 | 51 | caregiver_id: uuid.UUID | None = Query(default=None), |
51 | 52 | active_only: bool = Query(default=True), |
52 | | -) -> list[CareRelationResponse]: |
| 53 | + page: int = Query(default=1, ge=1), |
| 54 | + limit: int = Query(default=20, ge=1, le=100), |
| 55 | +) -> PaginatedResponse[CareRelationResponse]: |
53 | 56 | """List care relations filtered by host or caregiver. |
54 | 57 |
|
55 | 58 | Users can only list relations they participate in. |
56 | 59 | """ |
57 | 60 | user_uuid = uuid.UUID(user.id) |
| 61 | + params = PaginationParams(page=page, limit=limit) |
58 | 62 |
|
59 | 63 | if host_id: |
60 | 64 | if user_uuid != host_id: |
61 | | - # Verify caller is a caregiver for this host |
62 | | - relations = await service.list_relations_for_host( |
63 | | - db, host_id, active_only=active_only |
64 | | - ) |
65 | | - if not any(r.caregiver_id == user_uuid for r in relations): |
66 | | - raise_api_error(AUTHZ_001, status.HTTP_403_FORBIDDEN) |
67 | | - return [CareRelationResponse.model_validate(r) for r in relations] |
68 | | - relations = await service.list_relations_for_host( |
69 | | - db, host_id, active_only=active_only |
| 65 | + await authorize_host_access(db, user=user, host_id=host_id) |
| 66 | + relations, total = await service.list_relations_for_host( |
| 67 | + db, |
| 68 | + host_id, |
| 69 | + active_only=active_only, |
| 70 | + limit=params.limit, |
| 71 | + offset=params.offset, |
70 | 72 | ) |
71 | 73 | elif caregiver_id: |
72 | 74 | if user_uuid != caregiver_id: |
73 | 75 | raise_api_error(AUTHZ_001, status.HTTP_403_FORBIDDEN) |
74 | | - relations = await service.list_relations_for_caregiver( |
75 | | - db, caregiver_id, active_only=active_only |
| 76 | + relations, total = await service.list_relations_for_caregiver( |
| 77 | + db, |
| 78 | + caregiver_id, |
| 79 | + active_only=active_only, |
| 80 | + limit=params.limit, |
| 81 | + offset=params.offset, |
76 | 82 | ) |
77 | 83 | else: |
78 | 84 | raise HTTPException( |
79 | 85 | status_code=status.HTTP_400_BAD_REQUEST, |
80 | 86 | detail="Either host_id or caregiver_id is required", |
81 | 87 | ) |
82 | | - return [CareRelationResponse.model_validate(r) for r in relations] |
| 88 | + |
| 89 | + data = [CareRelationResponse.model_validate(r) for r in relations] |
| 90 | + return PaginatedResponse[CareRelationResponse].create( |
| 91 | + data=data, |
| 92 | + total=total, |
| 93 | + page=params.page, |
| 94 | + limit=params.limit, |
| 95 | + ) |
83 | 96 |
|
84 | 97 |
|
85 | 98 | @router.get("/{relation_id}", response_model=CareRelationResponse) |
|
0 commit comments