from fastapi import APIRouter, Depends from motor.motor_asyncio import AsyncIOMotorDatabase from app.dependencies.auth import get_current_owner_id from app.db.mongo import get_database from app.schemas.records import ChildCreateRequest, ChildRecordResponse, ChildUpdateRequest from app.services.records import create_child, delete_child, get_child, list_children, update_child router = APIRouter(prefix="/parents/{parent_id}/children", tags=["children"]) @router.post("", response_model=ChildRecordResponse) async def create_child_record( parent_id: str, payload: ChildCreateRequest, owner_id: str = Depends(get_current_owner_id), database: AsyncIOMotorDatabase = Depends(get_database), ) -> ChildRecordResponse: if payload.parentId != parent_id: payload = ChildCreateRequest(parentId=parent_id, iv=payload.iv, ciphertext=payload.ciphertext) return await create_child(database, owner_id, payload) @router.get("", response_model=list[ChildRecordResponse]) async def list_child_records( parent_id: str, owner_id: str = Depends(get_current_owner_id), database: AsyncIOMotorDatabase = Depends(get_database), ) -> list[ChildRecordResponse]: return await list_children(database, owner_id, parent_id) @router.get("/{child_id}", response_model=ChildRecordResponse) async def get_child_record( parent_id: str, child_id: str, owner_id: str = Depends(get_current_owner_id), database: AsyncIOMotorDatabase = Depends(get_database), ) -> ChildRecordResponse: return await get_child(database, owner_id, parent_id, child_id) @router.put("/{child_id}", response_model=ChildRecordResponse) async def update_child_record( parent_id: str, child_id: str, payload: ChildUpdateRequest, owner_id: str = Depends(get_current_owner_id), database: AsyncIOMotorDatabase = Depends(get_database), ) -> ChildRecordResponse: return await update_child(database, owner_id, parent_id, child_id, payload) @router.delete("/{child_id}", status_code=204) async def delete_child_record( parent_id: str, child_id: str, owner_id: str = Depends(get_current_owner_id), database: AsyncIOMotorDatabase = Depends(get_database), ) -> None: await delete_child(database, owner_id, parent_id, child_id)