Files
life-echo/api/routers/memoir_state.py
penghanyuan 44bd478c1e agent init
2026-01-21 22:31:09 +01:00

62 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
回忆录状态 API
"""
from fastapi import APIRouter, Depends
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from database import get_async_db
from database.models import Book as BookModel
from database.models import Chapter as ChapterModel
from database.models import User as UserModel
from middleware.auth import get_current_user
from services.memoir_state_service import get_or_create_state
router = APIRouter(prefix="/api/memoir-state", tags=["memoir-state"])
@router.get("")
async def get_memoir_state(
current_user: UserModel = Depends(get_current_user),
db: AsyncSession = Depends(get_async_db),
):
"""获取当前用户回忆录状态"""
state = await get_or_create_state(current_user.id, db)
return state.model_dump()
@router.get("/next-question")
async def get_next_question_context(
current_user: UserModel = Depends(get_current_user),
db: AsyncSession = Depends(get_async_db),
):
"""获取下一步问题的上下文(当前阶段与空 slot"""
state = await get_or_create_state(current_user.id, db)
return {
"current_stage": state.current_stage,
"empty_slots": state.empty_slots_for_current_stage(),
"covered_stages": state.covered_stages,
}
@router.post("/mark-read")
async def mark_memoir_read(
current_user: UserModel = Depends(get_current_user),
db: AsyncSession = Depends(get_async_db),
):
"""标记回忆录更新已读"""
stmt = select(ChapterModel).where(ChapterModel.user_id == current_user.id, ChapterModel.is_new == True)
result = await db.execute(stmt)
chapters = result.scalars().all()
for chapter in chapters:
chapter.is_new = False
stmt_book = select(BookModel).where(BookModel.user_id == current_user.id).order_by(BookModel.updated_at.desc())
result_book = await db.execute(stmt_book)
book = result_book.scalar_one_or_none()
if book:
book.has_update = False
await db.commit()
return {"status": "ok"}