documents.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import math
  2. from typing import Literal, Optional
  3. from fastapi import APIRouter, Depends, Query
  4. from sqlalchemy.ext.asyncio import AsyncSession
  5. from app.api.v1 import ok
  6. from app.core.dependencies import get_db
  7. from app.schemas.document import (
  8. CreateDocumentRequest,
  9. DocumentListItem,
  10. DocumentResponse,
  11. Pagination,
  12. UpdateDocumentRequest,
  13. )
  14. from app.services.document_service import DocumentService
  15. router = APIRouter(prefix="/documents", tags=["Documents"])
  16. @router.post("", summary="创建文档(下载 Word 转 Markdown)")
  17. async def create_document(
  18. body: CreateDocumentRequest,
  19. db: AsyncSession = Depends(get_db),
  20. ) -> dict:
  21. svc = DocumentService(db)
  22. doc = await svc.create_document(body)
  23. return ok({
  24. "documentId": doc.id,
  25. "format": doc.format,
  26. "createdAt": int(doc.created_at.timestamp() * 1000),
  27. })
  28. @router.get("", summary="获取文档列表")
  29. async def list_documents(
  30. userId: str = Query(...),
  31. page: int = Query(1, ge=1),
  32. pageSize: int = Query(20, ge=1, le=100),
  33. sessionId: Optional[str] = Query(None),
  34. sortBy: Literal["created_at", "updated_at"] = Query("updated_at"),
  35. sortOrder: Literal["asc", "desc"] = Query("desc"),
  36. db: AsyncSession = Depends(get_db),
  37. ) -> dict:
  38. svc = DocumentService(db)
  39. docs, total = await svc.list_documents(
  40. user_id=userId,
  41. page=page,
  42. page_size=pageSize,
  43. session_id=sessionId,
  44. sort_by=sortBy,
  45. sort_order=sortOrder,
  46. )
  47. items = [DocumentListItem.model_validate(d).model_dump(by_alias=True) for d in docs]
  48. pagination = Pagination(
  49. page=page,
  50. page_size=pageSize,
  51. total=total,
  52. total_pages=math.ceil(total / pageSize) if pageSize else 1,
  53. ).model_dump(by_alias=True)
  54. return ok({"documents": items, "pagination": pagination})
  55. @router.get("/{documentId}", summary="获取文档详情")
  56. async def get_document(
  57. documentId: str,
  58. db: AsyncSession = Depends(get_db),
  59. ) -> dict:
  60. svc = DocumentService(db)
  61. doc = await svc.get_document(documentId)
  62. data = DocumentResponse.model_validate(doc).model_dump(by_alias=True)
  63. # 将 created_by 映射为 userId
  64. data["userId"] = data.pop("user_id", None)
  65. return ok(data)
  66. @router.put("/{documentId}", summary="更新文档")
  67. async def update_document(
  68. documentId: str,
  69. body: UpdateDocumentRequest,
  70. db: AsyncSession = Depends(get_db),
  71. ) -> dict:
  72. svc = DocumentService(db)
  73. doc = await svc.update_document(documentId, body)
  74. return ok({
  75. "documentId": doc.id,
  76. "updatedAt": int(doc.updated_at.timestamp() * 1000),
  77. })
  78. @router.delete("/{sessionId}", summary="删除会话的所有文档")
  79. async def delete_documents_by_session(
  80. sessionId: str,
  81. db: AsyncSession = Depends(get_db),
  82. ) -> dict:
  83. svc = DocumentService(db)
  84. deleted_count = await svc.delete_documents_by_session(sessionId)
  85. return {"code": 0, "message": f"Deleted {deleted_count} document(s) successfully"}