refactor: 优化后端路由功能

- 扩展books.py路由,添加新接口
- 优化websocket.py路由,增强WebSocket功能
This commit is contained in:
iammm0
2026-01-26 11:54:05 +08:00
parent 5314077f3b
commit dae4a176fd
2 changed files with 112 additions and 23 deletions

View File

@@ -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