f37dc9fa76
FastAPI + Postgres backend with fastapi-users auth, games/characters CRUD, and a WebSocket chat endpoint where an x.ai Grok DM narrates play, rolls dice, and maintains character sheets via tool calls. React + Tailwind frontend covers registration/login, game list and creation, participation-code join flow, character viewer, and a real-time chat session view. Docker Compose skeleton (Postgres + backend) included; Caddy/frontend container wiring is deferred to the deploy milestone. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
43 lines
1.4 KiB
Python
43 lines
1.4 KiB
Python
import uuid
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.auth.users import current_active_user
|
|
from app.db import get_async_session
|
|
from app.models.character import Character
|
|
from app.models.user import User
|
|
from app.schemas.character import CharacterRead
|
|
|
|
router = APIRouter(prefix="/api/characters", tags=["characters"])
|
|
|
|
|
|
@router.get("", response_model=list[CharacterRead])
|
|
async def list_my_characters(
|
|
session: AsyncSession = Depends(get_async_session),
|
|
user: User = Depends(current_active_user),
|
|
) -> list[Character]:
|
|
characters = (
|
|
await session.execute(
|
|
select(Character)
|
|
.where(Character.owner_id == user.id)
|
|
.order_by(Character.created_at.desc())
|
|
)
|
|
).scalars().all()
|
|
return list(characters)
|
|
|
|
|
|
@router.get("/{character_id}", response_model=CharacterRead)
|
|
async def get_character(
|
|
character_id: uuid.UUID,
|
|
session: AsyncSession = Depends(get_async_session),
|
|
user: User = Depends(current_active_user),
|
|
) -> Character:
|
|
character = await session.get(Character, character_id)
|
|
if character is None:
|
|
raise HTTPException(status_code=404, detail="Character not found")
|
|
if character.owner_id != user.id:
|
|
raise HTTPException(status_code=403, detail="Not your character")
|
|
return character
|