""" 章节相关 API 路由 """ from typing import List from fastapi import APIRouter, Depends, HTTPException from sqlalchemy import select from sqlalchemy.ext.asyncio import AsyncSession from database import get_async_db from database.models import Chapter as ChapterModel router = APIRouter(prefix="/api/chapters", tags=["chapters"]) @router.get("", response_model=List[dict]) async def get_chapters( user_id: str, db: AsyncSession = Depends(get_async_db) ): """获取用户所有章节""" stmt = select(ChapterModel).where(ChapterModel.user_id == user_id).order_by(ChapterModel.order_index) result = await db.execute(stmt) chapters = result.scalars().all() return [ { "id": ch.id, "title": ch.title, "content": ch.content, "order_index": ch.order_index, "status": ch.status, "category": ch.category, "images": ch.images or [] } for ch in chapters ] @router.get("/{chapter_id}", response_model=dict) async def get_chapter( chapter_id: str, db: AsyncSession = Depends(get_async_db) ): """获取章节详情""" chapter = await db.get(ChapterModel, chapter_id) if not chapter: raise HTTPException(status_code=404, detail="Chapter not found") return { "id": chapter.id, "title": chapter.title, "content": chapter.content, "order_index": chapter.order_index, "status": chapter.status, "category": chapter.category, "images": chapter.images or [] } @router.post("/{chapter_id}/regenerate") async def regenerate_chapter( chapter_id: str, db: AsyncSession = Depends(get_async_db) ): """重新整理章节""" # TODO: 实现重新整理逻辑 return {"status": "ok", "message": "Chapter regeneration triggered"}