first implementation
This commit is contained in:
parent
1eb4641ed9
commit
a56a4c076a
14 changed files with 898 additions and 729 deletions
54
backend/routes/counter.py
Normal file
54
backend/routes/counter.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
"""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, User, CounterRecord, Permission
|
||||
|
||||
|
||||
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}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue