routes.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from core_domain import MemoryScopeType, MemoryStatus, ServiceHealth
  2. from fastapi import APIRouter, Depends, HTTPException, Query
  3. from sqlalchemy import text
  4. from sqlalchemy.orm import Session
  5. from app.application.services import MemoryApplicationService
  6. from app.db.session import get_db
  7. from app.domain.repositories import MemoryItemRepository
  8. from app.schemas.memory import (
  9. MemoryCreateRequest,
  10. MemoryResponse,
  11. MemorySearchRequest,
  12. MemorySearchResultResponse,
  13. MemoryStatusUpdateRequest,
  14. )
  15. router = APIRouter()
  16. def get_memory_application_service(db: Session = Depends(get_db)) -> MemoryApplicationService:
  17. return MemoryApplicationService(memory_repository=MemoryItemRepository(db))
  18. @router.get("/health", response_model=ServiceHealth)
  19. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  20. db.execute(text("SELECT 1"))
  21. return ServiceHealth(service="memory-service", status="ok", database="ok")
  22. @router.post("", response_model=MemoryResponse)
  23. def create_memory(
  24. payload: MemoryCreateRequest,
  25. service: MemoryApplicationService = Depends(get_memory_application_service)) -> MemoryResponse:
  26. entity = service.create_memory(payload)
  27. return MemoryResponse.from_entity(entity)
  28. @router.get("", response_model=list[MemoryResponse])
  29. def list_memories(
  30. scope_type: MemoryScopeType | None = Query(default=None),
  31. scope_id: str | None = Query(default=None),
  32. status: MemoryStatus | None = Query(default="active"),
  33. limit: int = Query(default=100, ge=1, le=500),
  34. service: MemoryApplicationService = Depends(get_memory_application_service)) -> list[MemoryResponse]:
  35. return [
  36. MemoryResponse.from_entity(item)
  37. for item in service.list_memories(
  38. scope_type=scope_type,
  39. scope_id=scope_id,
  40. status=status,
  41. limit=limit)
  42. ]
  43. @router.post("/search", response_model=list[MemorySearchResultResponse])
  44. def search_memories(
  45. payload: MemorySearchRequest,
  46. service: MemoryApplicationService = Depends(get_memory_application_service)) -> list[MemorySearchResultResponse]:
  47. return [
  48. MemorySearchResultResponse.from_entity(item, score=score, score_json=score_json)
  49. for item, score, score_json in service.search_memories(payload)
  50. ]
  51. @router.patch("/{memory_id}/status", response_model=MemoryResponse)
  52. def update_memory_status(
  53. memory_id: str,
  54. payload: MemoryStatusUpdateRequest,
  55. service: MemoryApplicationService = Depends(get_memory_application_service)) -> MemoryResponse:
  56. entity = service.update_memory_status(memory_id=memory_id, payload=payload)
  57. if entity is None:
  58. raise HTTPException(status_code=404, detail=f"memory not found: {memory_id}")
  59. return MemoryResponse.from_entity(entity)