Files
life-echo/api/app/features/memoir/router.py

149 lines
5.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.
"""
回忆录 feature — books / chapters / memoir-state 合并路由
"""
from app.core.logging import get_logger
from typing import List, Optional
from fastapi import APIRouter, Body, Depends, Query
from app.core.dependencies import get_current_user
from app.features.memoir.deps import get_memoir_service
from app.features.memoir.schemas import ExportPdfRequest, UpdateBookRequest
from app.features.memoir.service import MemoirService
from app.features.user.models import User
router = APIRouter(
prefix="/api",
tags=["memoir"],
responses={
401: {"description": "认证失败"},
403: {"description": "权限不足"},
404: {"description": "资源不存在"},
},
)
logger = get_logger(__name__)
# ===========================================================================
# Books endpoints
# ===========================================================================
@router.get("/books/current")
async def get_current_book(
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""获取当前回忆录(需要认证)"""
return await service.get_current_book(current_user.id)
@router.post("/books/clear-update")
async def clear_book_update(
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""清除回忆录更新标记"""
return await service.clear_book_update(current_user.id)
@router.put("/books/{book_id}")
async def update_book(
book_id: str,
request: UpdateBookRequest = Body(...),
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""更新书籍标题(需要认证,只能更新自己的回忆录)"""
return await service.update_book(book_id, current_user.id, request.title)
@router.post("/books/export-pdf")
async def export_pdf(
request: ExportPdfRequest = Body(...),
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""导出 PDF需要认证只能导出自己的回忆录"""
return await service.export_pdf(current_user.id, request.book_id)
# ===========================================================================
# Chapters endpoints
# ===========================================================================
@router.get("/chapters", response_model=List[dict])
async def get_chapters(
current_user: User = Depends(get_current_user),
is_new: Optional[bool] = Query(None, description="仅返回未读章节"),
service: MemoirService = Depends(get_memoir_service),
):
"""
获取用户所有章节(需要认证,仅返回 active 章节)。
始终返回全部 8 个预定义类别,没有内容的类别用占位符返回。
"""
return await service.get_chapters(current_user.id, is_new=is_new)
@router.get("/chapters/{chapter_id}", response_model=dict)
async def get_chapter(
chapter_id: str,
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""获取章节详情(需要认证,只能访问自己的章节)"""
return await service.get_chapter(chapter_id, current_user.id)
@router.delete("/chapters/{chapter_id}")
async def disable_chapter(
chapter_id: str,
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""清除章节(将章节标记为 disabled需要认证只能操作自己的章节"""
return await service.disable_chapter(chapter_id, current_user.id)
@router.post("/chapters/{chapter_id}/regenerate")
async def regenerate_chapter(
chapter_id: str,
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""重新整理章节(需要认证,只能操作自己的章节)"""
return await service.regenerate_chapter(chapter_id, current_user.id)
# ===========================================================================
# Memoir-state endpoints
# ===========================================================================
@router.get("/memoir-state")
async def get_memoir_state(
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""获取当前用户回忆录状态"""
return await service.get_memoir_state(current_user.id)
@router.get("/memoir-state/next-question")
async def get_next_question_context(
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""获取下一步问题的上下文(当前阶段与空 slot"""
return await service.get_next_question_context(current_user.id)
@router.post("/memoir-state/mark-read")
async def mark_memoir_read(
current_user: User = Depends(get_current_user),
service: MemoirService = Depends(get_memoir_service),
):
"""标记回忆录更新已读"""
return await service.mark_memoir_read(current_user.id)