2026-01-07 11:56:40 +08:00
|
|
|
|
"""
|
|
|
|
|
|
回忆录相关 API 路由
|
|
|
|
|
|
"""
|
2026-01-18 15:57:51 +08:00
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query, Body
|
|
|
|
|
|
from pydantic import BaseModel
|
2026-01-07 11:56:40 +08:00
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
|
|
from database import get_async_db
|
|
|
|
|
|
from database.models import Book as BookModel
|
2026-01-18 15:57:51 +08:00
|
|
|
|
from database.models import User as UserModel
|
2026-01-07 11:56:40 +08:00
|
|
|
|
from services.pdf_service import pdf_service
|
2026-01-18 15:57:51 +08:00
|
|
|
|
from middleware.auth import get_current_user
|
2026-01-07 11:56:40 +08:00
|
|
|
|
|
|
|
|
|
|
router = APIRouter(prefix="/api/books", tags=["books"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/current")
|
|
|
|
|
|
async def get_current_book(
|
2026-01-18 15:57:51 +08:00
|
|
|
|
current_user: UserModel = Depends(get_current_user),
|
2026-01-07 11:56:40 +08:00
|
|
|
|
db: AsyncSession = Depends(get_async_db)
|
|
|
|
|
|
):
|
2026-01-18 15:57:51 +08:00
|
|
|
|
"""获取当前回忆录(需要认证)"""
|
|
|
|
|
|
stmt = select(BookModel).where(BookModel.user_id == current_user.id).order_by(BookModel.updated_at.desc())
|
2026-01-07 11:56:40 +08:00
|
|
|
|
result = await db.execute(stmt)
|
|
|
|
|
|
book = result.scalar_one_or_none()
|
|
|
|
|
|
|
|
|
|
|
|
if not book:
|
|
|
|
|
|
return {"message": "No book found"}
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
"id": book.id,
|
|
|
|
|
|
"title": book.title,
|
|
|
|
|
|
"total_pages": book.total_pages,
|
|
|
|
|
|
"total_words": book.total_words,
|
|
|
|
|
|
"cover_image_url": book.cover_image_url
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-18 15:57:51 +08:00
|
|
|
|
class ExportPdfRequest(BaseModel):
|
|
|
|
|
|
book_id: str
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-07 11:56:40 +08:00
|
|
|
|
@router.post("/export-pdf")
|
|
|
|
|
|
async def export_pdf(
|
2026-01-18 15:57:51 +08:00
|
|
|
|
request: ExportPdfRequest = Body(...),
|
|
|
|
|
|
current_user: UserModel = Depends(get_current_user),
|
2026-01-07 11:56:40 +08:00
|
|
|
|
db: AsyncSession = Depends(get_async_db)
|
|
|
|
|
|
):
|
2026-01-18 15:57:51 +08:00
|
|
|
|
"""导出 PDF(需要认证,只能导出自己的回忆录)"""
|
|
|
|
|
|
book = await db.get(BookModel, request.book_id)
|
|
|
|
|
|
if not book:
|
2026-01-07 11:56:40 +08:00
|
|
|
|
raise HTTPException(status_code=404, detail="Book not found")
|
|
|
|
|
|
|
2026-01-18 15:57:51 +08:00
|
|
|
|
# 验证用户权限
|
|
|
|
|
|
if book.user_id != current_user.id:
|
|
|
|
|
|
raise HTTPException(status_code=403, detail="无权导出此回忆录")
|
|
|
|
|
|
|
2026-01-07 11:56:40 +08:00
|
|
|
|
# 获取所有章节
|
2026-01-18 15:57:51 +08:00
|
|
|
|
from database.models import Chapter
|
|
|
|
|
|
stmt = select(Chapter).where(Chapter.user_id == current_user.id).order_by(Chapter.order_index)
|
2026-01-07 11:56:40 +08:00
|
|
|
|
result = await db.execute(stmt)
|
|
|
|
|
|
chapters = result.scalars().all()
|
|
|
|
|
|
|
|
|
|
|
|
# 生成 PDF
|
|
|
|
|
|
pdf_bytes = await pdf_service.generate_pdf(book, chapters)
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
"pdf_base64": pdf_bytes.decode('latin1'), # 简化处理,实际应该用 base64
|
|
|
|
|
|
"filename": f"{book.title}.pdf"
|
|
|
|
|
|
}
|
|
|
|
|
|
|