- Add ruff as dev dependency - Configure ruff in pyproject.toml with strict 88-char line limit - Ignore B008 (FastAPI Depends pattern is standard) - Allow longer lines in tests for readability - Fix all lint issues in source files - Add Makefile targets: lint-backend, format-backend, fix-backend
53 lines
1.6 KiB
Python
53 lines
1.6 KiB
Python
"""Counter routes."""
|
|
|
|
from fastapi import APIRouter, Depends
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from auth import require_permission
|
|
from database import get_db
|
|
from models import Counter, CounterRecord, Permission, User
|
|
|
|
router = APIRouter(prefix="/api/counter", tags=["counter"])
|
|
|
|
|
|
async def get_or_create_counter(db: AsyncSession) -> Counter:
|
|
"""Get the singleton counter, creating it if it doesn't exist."""
|
|
result = await db.execute(select(Counter).where(Counter.id == 1))
|
|
counter = result.scalar_one_or_none()
|
|
if not counter:
|
|
counter = Counter(id=1, value=0)
|
|
db.add(counter)
|
|
await db.commit()
|
|
await db.refresh(counter)
|
|
return counter
|
|
|
|
|
|
@router.get("")
|
|
async def get_counter(
|
|
db: AsyncSession = Depends(get_db),
|
|
_current_user: User = Depends(require_permission(Permission.VIEW_COUNTER)),
|
|
) -> dict[str, int]:
|
|
"""Get the current counter value."""
|
|
counter = await get_or_create_counter(db)
|
|
return {"value": counter.value}
|
|
|
|
|
|
@router.post("/increment")
|
|
async def increment_counter(
|
|
db: AsyncSession = Depends(get_db),
|
|
current_user: User = Depends(require_permission(Permission.INCREMENT_COUNTER)),
|
|
) -> dict[str, int]:
|
|
"""Increment the counter and record the action."""
|
|
counter = await get_or_create_counter(db)
|
|
value_before = counter.value
|
|
counter.value += 1
|
|
|
|
record = CounterRecord(
|
|
user_id=current_user.id,
|
|
value_before=value_before,
|
|
value_after=counter.value,
|
|
)
|
|
db.add(record)
|
|
await db.commit()
|
|
return {"value": counter.value}
|