91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
"""Child management router."""
|
|
|
|
from typing import Annotated
|
|
from fastapi import APIRouter, HTTPException, Depends
|
|
|
|
from baby_monitor.models.child import CreateChildRequest, ChildResponse
|
|
from baby_monitor.routers.auth import verify_token
|
|
from baby_monitor.repositories.child.sqlite_child import SQLiteChildRepository
|
|
from baby_monitor.repositories.dependencies.get_child_repository import (
|
|
get_child_repository,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/children", tags=["children"])
|
|
|
|
|
|
@router.post("", response_model=ChildResponse, status_code=201)
|
|
def create_child(
|
|
request: CreateChildRequest,
|
|
user_id: Annotated[int, Depends(verify_token)],
|
|
child_repo: Annotated[SQLiteChildRepository, Depends(get_child_repository)],
|
|
) -> ChildResponse:
|
|
"""Create a new child for the authenticated user."""
|
|
child = child_repo.create(
|
|
name=request.name,
|
|
birth_time=request.birth_time,
|
|
birth_weight=request.birth_weight,
|
|
user_id=user_id,
|
|
)
|
|
|
|
return ChildResponse(**child)
|
|
|
|
|
|
@router.get("", response_model=list[ChildResponse])
|
|
def get_user_children(
|
|
user_id: Annotated[int, Depends(verify_token)],
|
|
child_repo: Annotated[SQLiteChildRepository, Depends(get_child_repository)],
|
|
) -> list[ChildResponse]:
|
|
"""Get all children for the authenticated user."""
|
|
children = child_repo.get_by_user_id(user_id)
|
|
return [ChildResponse(**child) for child in children]
|
|
|
|
|
|
@router.get("/{child_id}", response_model=ChildResponse)
|
|
def get_child(
|
|
child_id: int,
|
|
user_id: Annotated[int, Depends(verify_token)],
|
|
child_repo: Annotated[SQLiteChildRepository, Depends(get_child_repository)],
|
|
) -> ChildResponse:
|
|
"""Get a specific child by ID."""
|
|
child = child_repo.get_by_id(child_id)
|
|
|
|
if not child:
|
|
raise HTTPException(status_code=404, detail="Child not found")
|
|
|
|
# Verify the child belongs to the authenticated user
|
|
if child["user_id"] != user_id:
|
|
raise HTTPException(status_code=403, detail="Access denied")
|
|
|
|
return ChildResponse(**child)
|
|
|
|
|
|
@router.put("/{child_id}", response_model=ChildResponse)
|
|
def update_child(
|
|
child_id: int,
|
|
request: CreateChildRequest,
|
|
user_id: Annotated[int, Depends(verify_token)],
|
|
child_repo: Annotated[SQLiteChildRepository, Depends(get_child_repository)],
|
|
) -> ChildResponse:
|
|
"""Update a child's information."""
|
|
# First check if child exists and belongs to user
|
|
child = child_repo.get_by_id(child_id)
|
|
|
|
if not child:
|
|
raise HTTPException(status_code=404, detail="Child not found")
|
|
|
|
if child["user_id"] != user_id:
|
|
raise HTTPException(status_code=403, detail="Access denied")
|
|
|
|
# Update the child
|
|
updated_child = child_repo.update(
|
|
child_id=child_id,
|
|
name=request.name,
|
|
birth_time=request.birth_time,
|
|
birth_weight=request.birth_weight,
|
|
)
|
|
|
|
if not updated_child:
|
|
raise HTTPException(status_code=500, detail="Update failed")
|
|
|
|
return ChildResponse(**updated_child)
|