refactor: 更新API路由
- 更新books路由以支持用户认证 - 更新chapters路由以支持用户认证 - 更新conversations路由以支持用户认证 - 更新websocket路由以支持用户认证和连接管理
This commit is contained in:
@@ -1,25 +1,27 @@
|
||||
"""
|
||||
章节相关 API 路由
|
||||
"""
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database import get_async_db
|
||||
from database.models import Chapter as ChapterModel
|
||||
from database.models import User as UserModel
|
||||
from middleware.auth import get_current_user
|
||||
|
||||
router = APIRouter(prefix="/api/chapters", tags=["chapters"])
|
||||
|
||||
|
||||
@router.get("", response_model=List[dict])
|
||||
async def get_chapters(
|
||||
user_id: str,
|
||||
current_user: UserModel = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_async_db)
|
||||
):
|
||||
"""获取用户所有章节"""
|
||||
stmt = select(ChapterModel).where(ChapterModel.user_id == user_id).order_by(ChapterModel.order_index)
|
||||
"""获取用户所有章节(需要认证)"""
|
||||
stmt = select(ChapterModel).where(ChapterModel.user_id == current_user.id).order_by(ChapterModel.order_index)
|
||||
result = await db.execute(stmt)
|
||||
chapters = result.scalars().all()
|
||||
|
||||
@@ -40,13 +42,18 @@ async def get_chapters(
|
||||
@router.get("/{chapter_id}", response_model=dict)
|
||||
async def get_chapter(
|
||||
chapter_id: str,
|
||||
current_user: UserModel = Depends(get_current_user),
|
||||
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")
|
||||
|
||||
# 验证用户权限
|
||||
if chapter.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="无权访问此章节")
|
||||
|
||||
return {
|
||||
"id": chapter.id,
|
||||
"title": chapter.title,
|
||||
@@ -61,9 +68,18 @@ async def get_chapter(
|
||||
@router.post("/{chapter_id}/regenerate")
|
||||
async def regenerate_chapter(
|
||||
chapter_id: str,
|
||||
current_user: UserModel = Depends(get_current_user),
|
||||
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")
|
||||
|
||||
# 验证用户权限
|
||||
if chapter.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="无权操作此章节")
|
||||
|
||||
# TODO: 实现重新整理逻辑
|
||||
return {"status": "ok", "message": "Chapter regeneration triggered"}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user