Initial project scaffold
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>
This commit is contained in:
95
backend/app/routers/logs.py
Normal file
95
backend/app/routers/logs.py
Normal file
@@ -0,0 +1,95 @@
|
||||
from datetime import date
|
||||
|
||||
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.activity import ActivityLog
|
||||
from app.models.child import Child
|
||||
from app.models.user import User
|
||||
from app.schemas.activity import ActivityLogCreate, ActivityLogOut, ActivityLogUpdate
|
||||
|
||||
router = APIRouter(prefix="/api/logs", tags=["logs"])
|
||||
|
||||
|
||||
@router.get("", response_model=list[ActivityLogOut])
|
||||
async def list_logs(
|
||||
child_id: int | None = None,
|
||||
log_date: date | None = None,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
query = (
|
||||
select(ActivityLog)
|
||||
.join(Child)
|
||||
.where(Child.user_id == current_user.id)
|
||||
.order_by(ActivityLog.log_date.desc(), ActivityLog.created_at.desc())
|
||||
)
|
||||
if child_id:
|
||||
query = query.where(ActivityLog.child_id == child_id)
|
||||
if log_date:
|
||||
query = query.where(ActivityLog.log_date == log_date)
|
||||
|
||||
result = await db.execute(query)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@router.post("", response_model=ActivityLogOut, status_code=status.HTTP_201_CREATED)
|
||||
async def create_log(
|
||||
body: ActivityLogCreate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
child_result = await db.execute(
|
||||
select(Child).where(Child.id == body.child_id, Child.user_id == current_user.id)
|
||||
)
|
||||
if not child_result.scalar_one_or_none():
|
||||
raise HTTPException(status_code=404, detail="Child not found")
|
||||
|
||||
log = ActivityLog(**body.model_dump())
|
||||
db.add(log)
|
||||
await db.commit()
|
||||
await db.refresh(log)
|
||||
return log
|
||||
|
||||
|
||||
@router.patch("/{log_id}", response_model=ActivityLogOut)
|
||||
async def update_log(
|
||||
log_id: int,
|
||||
body: ActivityLogUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(ActivityLog)
|
||||
.join(Child)
|
||||
.where(ActivityLog.id == log_id, Child.user_id == current_user.id)
|
||||
)
|
||||
log = result.scalar_one_or_none()
|
||||
if not log:
|
||||
raise HTTPException(status_code=404, detail="Log not found")
|
||||
|
||||
for field, value in body.model_dump(exclude_none=True).items():
|
||||
setattr(log, field, value)
|
||||
await db.commit()
|
||||
await db.refresh(log)
|
||||
return log
|
||||
|
||||
|
||||
@router.delete("/{log_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_log(
|
||||
log_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(ActivityLog)
|
||||
.join(Child)
|
||||
.where(ActivityLog.id == log_id, Child.user_id == current_user.id)
|
||||
)
|
||||
log = result.scalar_one_or_none()
|
||||
if not log:
|
||||
raise HTTPException(status_code=404, detail="Log not found")
|
||||
await db.delete(log)
|
||||
await db.commit()
|
||||
Reference in New Issue
Block a user