Add break time feature to schedule blocks
- Admin: per-block "Break Time" checkbox + duration (min) setting; new Break Activities section (global list, same pattern as Morning Routine) - Dashboard: break timer section appears on blocks with break enabled; Start/Pause/Resume/Reset controls work independently of the main timer - TV: left column switches to amber break badge + countdown during break; center column shows configurable Break Activities list - Backend: break_time_enabled/break_time_minutes columns on schedule_blocks (auto-migrated on startup); break_activity_items table + CRUD router; break timer events (break_start/pause/resume/reset) stored as TimerEvents and broadcast via WebSocket; break_activities included in dashboard snapshot and session_update broadcast Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
97
backend/app/routers/break_activity.py
Normal file
97
backend/app/routers/break_activity.py
Normal file
@@ -0,0 +1,97 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.dependencies import get_db, get_current_user
|
||||
from app.models.break_activity import BreakActivityItem
|
||||
from app.models.user import User
|
||||
|
||||
router = APIRouter(prefix="/api/break-activities", tags=["break-activities"])
|
||||
|
||||
|
||||
class BreakActivityItemOut(BaseModel):
|
||||
id: int
|
||||
text: str
|
||||
order_index: int
|
||||
model_config = {"from_attributes": True}
|
||||
|
||||
|
||||
class BreakActivityItemCreate(BaseModel):
|
||||
text: str
|
||||
order_index: int = 0
|
||||
|
||||
|
||||
class BreakActivityItemUpdate(BaseModel):
|
||||
text: str | None = None
|
||||
order_index: int | None = None
|
||||
|
||||
|
||||
@router.get("", response_model=list[BreakActivityItemOut])
|
||||
async def list_items(
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(BreakActivityItem)
|
||||
.where(BreakActivityItem.user_id == current_user.id)
|
||||
.order_by(BreakActivityItem.order_index, BreakActivityItem.id)
|
||||
)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@router.post("", response_model=BreakActivityItemOut, status_code=status.HTTP_201_CREATED)
|
||||
async def create_item(
|
||||
body: BreakActivityItemCreate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
item = BreakActivityItem(user_id=current_user.id, text=body.text, order_index=body.order_index)
|
||||
db.add(item)
|
||||
await db.commit()
|
||||
await db.refresh(item)
|
||||
return item
|
||||
|
||||
|
||||
@router.patch("/{item_id}", response_model=BreakActivityItemOut)
|
||||
async def update_item(
|
||||
item_id: int,
|
||||
body: BreakActivityItemUpdate,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(BreakActivityItem).where(
|
||||
BreakActivityItem.id == item_id,
|
||||
BreakActivityItem.user_id == current_user.id,
|
||||
)
|
||||
)
|
||||
item = result.scalar_one_or_none()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
if body.text is not None:
|
||||
item.text = body.text
|
||||
if body.order_index is not None:
|
||||
item.order_index = body.order_index
|
||||
await db.commit()
|
||||
await db.refresh(item)
|
||||
return item
|
||||
|
||||
|
||||
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||
async def delete_item(
|
||||
item_id: int,
|
||||
current_user: User = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_db),
|
||||
):
|
||||
result = await db.execute(
|
||||
select(BreakActivityItem).where(
|
||||
BreakActivityItem.id == item_id,
|
||||
BreakActivityItem.user_id == current_user.id,
|
||||
)
|
||||
)
|
||||
item = result.scalar_one_or_none()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Item not found")
|
||||
await db.delete(item)
|
||||
await db.commit()
|
||||
Reference in New Issue
Block a user