documents.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. # ------------------------------------------------------------------ #
  17. # POST /documents 创建文档
  18. # ------------------------------------------------------------------ #
  19. @router.post("", summary="创建文档")
  20. async def create_document(
  21. body: CreateDocumentRequest,
  22. db: AsyncSession = Depends(get_db),
  23. ) -> dict:
  24. svc = DocumentService(db)
  25. doc = await svc.create_document(body)
  26. return ok(
  27. {
  28. "documentId": doc.id,
  29. "title": doc.title,
  30. "format": doc.format,
  31. "createdAt": int(doc.created_at.timestamp() * 1000),
  32. }
  33. )
  34. # ------------------------------------------------------------------ #
  35. # GET /documents 获取文档列表
  36. # ------------------------------------------------------------------ #
  37. @router.get("", summary="获取文档列表")
  38. async def list_documents(
  39. page: int = Query(1, ge=1),
  40. page_size: int = Query(20, ge=1, le=100, alias="pageSize"),
  41. session_id: Optional[str] = Query(None, alias="sessionId"),
  42. sort_by: Literal["created_at", "updated_at"] = Query("updated_at", alias="sortBy"),
  43. sort_order: Literal["asc", "desc"] = Query("desc", alias="sortOrder"),
  44. db: AsyncSession = Depends(get_db),
  45. ) -> dict:
  46. svc = DocumentService(db)
  47. docs, total = await svc.list_documents(
  48. page=page,
  49. page_size=page_size,
  50. session_id=session_id,
  51. sort_by=sort_by,
  52. sort_order=sort_order,
  53. )
  54. items = [
  55. DocumentListItem.model_validate(d).model_dump(by_alias=True) for d in docs
  56. ]
  57. pagination = Pagination(
  58. page=page,
  59. page_size=page_size,
  60. total=total,
  61. total_pages=math.ceil(total / page_size) if page_size else 1,
  62. ).model_dump(by_alias=True)
  63. return ok({"documents": items, "pagination": pagination})
  64. # ------------------------------------------------------------------ #
  65. # GET /documents/{documentId} 获取文档详情
  66. # ------------------------------------------------------------------ #
  67. @router.get("/{document_id}", summary="获取文档详情")
  68. async def get_document(
  69. document_id: str,
  70. db: AsyncSession = Depends(get_db),
  71. ) -> dict:
  72. svc = DocumentService(db)
  73. doc = await svc.get_document(document_id)
  74. return ok(DocumentResponse.model_validate(doc).model_dump(by_alias=True))
  75. # ------------------------------------------------------------------ #
  76. # PUT /documents/{documentId} 更新文档
  77. # ------------------------------------------------------------------ #
  78. @router.put("/{document_id}", summary="更新文档")
  79. async def update_document(
  80. document_id: str,
  81. body: UpdateDocumentRequest,
  82. db: AsyncSession = Depends(get_db),
  83. ) -> dict:
  84. svc = DocumentService(db)
  85. doc = await svc.update_document(document_id, body)
  86. return ok(
  87. {
  88. "documentId": doc.id,
  89. "updatedAt": int(doc.updated_at.timestamp() * 1000),
  90. }
  91. )
  92. # ------------------------------------------------------------------ #
  93. # DELETE /documents/{documentId} 删除文档
  94. # ------------------------------------------------------------------ #
  95. @router.delete("/{document_id}", summary="删除文档")
  96. async def delete_document(
  97. document_id: str,
  98. db: AsyncSession = Depends(get_db),
  99. ) -> dict:
  100. svc = DocumentService(db)
  101. await svc.delete_document(document_id)
  102. return {"code": 0, "message": "Document deleted successfully"}