Files
ai-rpg/backend/app/api/misc.py
2026-06-19 11:28:04 +03:00

66 lines
2.3 KiB
Python

"""Glossary + Triggers routes."""
from __future__ import annotations
from typing import List
from uuid import UUID
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import APIRouter, Depends, HTTPException
from app.db import get_db_dep
from app.deps import get_current_user
from app.models import DeferredTrigger, GlossaryEntry, Session, User, World
from app.schemas import GlossaryEntryOut, TriggerOut
router = APIRouter(prefix="/api", tags=["misc"])
@router.get("/worlds/{world_id}/glossary", response_model=List[GlossaryEntryOut])
async def list_glossary(
world_id: UUID,
kind: str | None = None,
db: AsyncSession = Depends(get_db_dep),
user: User = Depends(get_current_user),
):
w_result = await db.execute(select(World).where(World.id == world_id))
world = w_result.scalars().first()
if not world:
raise HTTPException(status_code=404, detail="world_not_found")
if world.owner_id != user.id and not user.is_admin:
raise HTTPException(status_code=403, detail="forbidden")
q = select(GlossaryEntry).where(GlossaryEntry.world_id == world_id)
if kind:
q = q.where(GlossaryEntry.kind == kind)
q = q.order_by(GlossaryEntry.created_at.desc())
result = await db.execute(q)
return result.scalars().all()
@router.get("/sessions/{session_id}/triggers", response_model=List[TriggerOut])
async def list_triggers(
session_id: UUID,
include_fired: bool = True,
db: AsyncSession = Depends(get_db_dep),
user: User = Depends(get_current_user),
):
s_result = await db.execute(
select(Session).join(World, Session.world_id == World.id).where(Session.id == session_id)
)
session = s_result.scalars().first()
if not session:
raise HTTPException(status_code=404, detail="session_not_found")
w_result = await db.execute(select(World).where(World.id == session.world_id))
world = w_result.scalars().first()
if not world or (world.owner_id != user.id and not user.is_admin):
raise HTTPException(status_code=403, detail="forbidden")
q = select(DeferredTrigger).where(DeferredTrigger.session_id == session_id)
if not include_fired:
q = q.where(DeferredTrigger.fired.is_(False))
q = q.order_by(DeferredTrigger.fire_at)
result = await db.execute(q)
return result.scalars().all()