Full-stack homeschool web app with FastAPI backend, Vue 3 frontend, MySQL database, and Docker Compose orchestration. Includes JWT auth, WebSocket real-time TV dashboard, schedule builder, activity logging, and multi-child support. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
87 lines
2.6 KiB
Python
87 lines
2.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
|
|
from app.dependencies import get_db, get_current_user
|
|
from app.models.child import Child
|
|
from app.models.user import User
|
|
from app.schemas.child import ChildCreate, ChildOut, ChildUpdate
|
|
|
|
router = APIRouter(prefix="/api/children", tags=["children"])
|
|
|
|
|
|
@router.get("", response_model=list[ChildOut])
|
|
async def list_children(
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
result = await db.execute(
|
|
select(Child).where(Child.user_id == current_user.id).order_by(Child.name)
|
|
)
|
|
return result.scalars().all()
|
|
|
|
|
|
@router.post("", response_model=ChildOut, status_code=status.HTTP_201_CREATED)
|
|
async def create_child(
|
|
body: ChildCreate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
child = Child(**body.model_dump(), user_id=current_user.id)
|
|
db.add(child)
|
|
await db.commit()
|
|
await db.refresh(child)
|
|
return child
|
|
|
|
|
|
@router.get("/{child_id}", response_model=ChildOut)
|
|
async def get_child(
|
|
child_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
result = await db.execute(
|
|
select(Child).where(Child.id == child_id, Child.user_id == current_user.id)
|
|
)
|
|
child = result.scalar_one_or_none()
|
|
if not child:
|
|
raise HTTPException(status_code=404, detail="Child not found")
|
|
return child
|
|
|
|
|
|
@router.patch("/{child_id}", response_model=ChildOut)
|
|
async def update_child(
|
|
child_id: int,
|
|
body: ChildUpdate,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
result = await db.execute(
|
|
select(Child).where(Child.id == child_id, Child.user_id == current_user.id)
|
|
)
|
|
child = result.scalar_one_or_none()
|
|
if not child:
|
|
raise HTTPException(status_code=404, detail="Child not found")
|
|
|
|
for field, value in body.model_dump(exclude_none=True).items():
|
|
setattr(child, field, value)
|
|
await db.commit()
|
|
await db.refresh(child)
|
|
return child
|
|
|
|
|
|
@router.delete("/{child_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_child(
|
|
child_id: int,
|
|
current_user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
result = await db.execute(
|
|
select(Child).where(Child.id == child_id, Child.user_id == current_user.id)
|
|
)
|
|
child = result.scalar_one_or_none()
|
|
if not child:
|
|
raise HTTPException(status_code=404, detail="Child not found")
|
|
await db.delete(child)
|
|
await db.commit()
|