refactor: 优化后端路由功能
- 扩展books.py路由,添加新接口 - 优化websocket.py路由,增强WebSocket功能
This commit is contained in:
@@ -55,6 +55,44 @@ async def clear_book_update(
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
class UpdateBookRequest(BaseModel):
|
||||
title: str
|
||||
subtitle: str | None = None # 目前数据库不支持subtitle,但保留字段以便将来扩展
|
||||
|
||||
|
||||
@router.put("/{book_id}")
|
||||
async def update_book(
|
||||
book_id: str,
|
||||
request: UpdateBookRequest = Body(...),
|
||||
current_user: UserModel = Depends(get_current_user),
|
||||
db: AsyncSession = Depends(get_async_db)
|
||||
):
|
||||
"""更新书籍标题(需要认证,只能更新自己的回忆录)"""
|
||||
book = await db.get(BookModel, book_id)
|
||||
if not book:
|
||||
raise HTTPException(status_code=404, detail="Book not found")
|
||||
|
||||
# 验证用户权限
|
||||
if book.user_id != current_user.id:
|
||||
raise HTTPException(status_code=403, detail="无权更新此回忆录")
|
||||
|
||||
# 更新标题
|
||||
book.title = request.title
|
||||
# subtitle字段目前数据库不支持,暂时忽略
|
||||
await db.commit()
|
||||
await db.refresh(book)
|
||||
|
||||
return {
|
||||
"id": book.id,
|
||||
"title": book.title,
|
||||
"total_pages": book.total_pages,
|
||||
"total_words": book.total_words,
|
||||
"cover_image_url": book.cover_image_url,
|
||||
"has_update": book.has_update,
|
||||
"last_update_chapter_id": book.last_update_chapter_id,
|
||||
}
|
||||
|
||||
|
||||
class ExportPdfRequest(BaseModel):
|
||||
book_id: str
|
||||
|
||||
|
||||
Reference in New Issue
Block a user