documents.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from typing import Literal, Optional
  2. from fastapi import APIRouter, Depends, Query
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from app.core.dependencies import get_db
  5. from app.schemas.document import (
  6. CreateDocumentRequest,
  7. DocumentListItem,
  8. DocumentResponse,
  9. Pagination,
  10. UpdateDocumentRequest,
  11. )
  12. from app.services.document_service import DocumentService
  13. router = APIRouter(prefix="/documents", tags=["Documents"])
  14. def _ok(data: dict) -> dict:
  15. return {"code": 0, "message": "Success", "data": data}
  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. import math
  55. items = [
  56. DocumentListItem.model_validate(d).model_dump(by_alias=True) for d in docs
  57. ]
  58. pagination = Pagination(
  59. page=page,
  60. page_size=page_size,
  61. total=total,
  62. total_pages=math.ceil(total / page_size) if page_size else 1,
  63. ).model_dump(by_alias=True)
  64. return _ok({"documents": items, "pagination": pagination})
  65. # ------------------------------------------------------------------ #
  66. # GET /documents/{documentId} 获取文档详情
  67. # ------------------------------------------------------------------ #
  68. @router.get("/{document_id}", summary="获取文档详情")
  69. async def get_document(
  70. document_id: str,
  71. db: AsyncSession = Depends(get_db),
  72. ) -> dict:
  73. svc = DocumentService(db)
  74. doc = await svc.get_document(document_id)
  75. return _ok(DocumentResponse.model_validate(doc).model_dump(by_alias=True))
  76. # ------------------------------------------------------------------ #
  77. # PUT /documents/{documentId} 更新文档
  78. # ------------------------------------------------------------------ #
  79. @router.put("/{document_id}", summary="更新文档")
  80. async def update_document(
  81. document_id: str,
  82. body: UpdateDocumentRequest,
  83. db: AsyncSession = Depends(get_db),
  84. ) -> dict:
  85. svc = DocumentService(db)
  86. doc = await svc.update_document(document_id, body)
  87. return _ok(
  88. {
  89. "documentId": doc.id,
  90. "updatedAt": int(doc.updated_at.timestamp() * 1000),
  91. }
  92. )
  93. # ------------------------------------------------------------------ #
  94. # DELETE /documents/{documentId} 删除文档
  95. # ------------------------------------------------------------------ #
  96. @router.delete("/{document_id}", summary="删除文档")
  97. async def delete_document(
  98. document_id: str,
  99. db: AsyncSession = Depends(get_db),
  100. ) -> dict:
  101. svc = DocumentService(db)
  102. await svc.delete_document(document_id)
  103. return {"code": 0, "message": "Document deleted successfully"}