routes.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from core_domain import ServiceHealth
  2. from core_events import EventDeliveryStatus
  3. from fastapi import APIRouter, Depends, HTTPException, Query
  4. from sqlalchemy import text
  5. from sqlalchemy.orm import Session
  6. from app.application.services import EventApplicationService
  7. from app.db.session import get_db
  8. from app.domain.repositories import EventRecordRepository
  9. from app.schemas.event import (
  10. EventBatchPublishRequest,
  11. EventBatchPublishResponse,
  12. EventDeliveryStatusUpdateRequest,
  13. EventPublishRequest,
  14. EventRecordResponse,
  15. EventStatsResponse,
  16. PendingEventClaimRequest,
  17. )
  18. router = APIRouter()
  19. def get_event_application_service(db: Session = Depends(get_db)) -> EventApplicationService:
  20. return EventApplicationService(event_repository=EventRecordRepository(db))
  21. @router.get("/health", response_model=ServiceHealth)
  22. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  23. db.execute(text("SELECT 1"))
  24. return ServiceHealth(service="event-service", status="ok", database="ok")
  25. @router.post("", response_model=EventRecordResponse)
  26. def publish_event(
  27. payload: EventPublishRequest,
  28. service: EventApplicationService = Depends(get_event_application_service)) -> EventRecordResponse:
  29. return EventRecordResponse.from_entity(service.publish_event(payload))
  30. @router.post("/batch", response_model=EventBatchPublishResponse)
  31. def publish_batch(
  32. payload: EventBatchPublishRequest,
  33. service: EventApplicationService = Depends(get_event_application_service)) -> EventBatchPublishResponse:
  34. events = service.publish_batch(payload)
  35. return EventBatchPublishResponse(
  36. events=[EventRecordResponse.from_entity(item) for item in events],
  37. count=len(events))
  38. @router.get("", response_model=list[EventRecordResponse])
  39. def list_events(
  40. event_type: str | None = Query(default=None),
  41. source_service: str | None = Query(default=None),
  42. aggregate_type: str | None = Query(default=None),
  43. aggregate_id: str | None = Query(default=None),
  44. correlation_id: str | None = Query(default=None),
  45. status: EventDeliveryStatus | None = Query(default=None),
  46. limit: int = Query(default=100, ge=1, le=500),
  47. service: EventApplicationService = Depends(get_event_application_service)) -> list[EventRecordResponse]:
  48. return [
  49. EventRecordResponse.from_entity(item)
  50. for item in service.list_events(
  51. event_type=event_type,
  52. source_service=source_service,
  53. aggregate_type=aggregate_type,
  54. aggregate_id=aggregate_id,
  55. correlation_id=correlation_id,
  56. status=status,
  57. limit=limit)
  58. ]
  59. @router.post("/claim-pending", response_model=list[EventRecordResponse])
  60. def claim_pending_events(
  61. payload: PendingEventClaimRequest,
  62. service: EventApplicationService = Depends(get_event_application_service)) -> list[EventRecordResponse]:
  63. return [
  64. EventRecordResponse.from_entity(item)
  65. for item in service.claim_pending_events(payload)
  66. ]
  67. @router.post("/{event_record_id}/delivery-status", response_model=EventRecordResponse)
  68. def update_delivery_status(
  69. event_record_id: str,
  70. payload: EventDeliveryStatusUpdateRequest,
  71. service: EventApplicationService = Depends(get_event_application_service)) -> EventRecordResponse:
  72. entity = service.update_delivery_status(
  73. event_record_id=event_record_id,
  74. payload=payload)
  75. if entity is None:
  76. raise HTTPException(status_code=404, detail=f"event not found: {event_record_id}")
  77. return EventRecordResponse.from_entity(entity)
  78. @router.get("/stats", response_model=EventStatsResponse)
  79. def event_stats(
  80. service: EventApplicationService = Depends(get_event_application_service)) -> EventStatsResponse:
  81. return EventStatsResponse(
  82. counts_json=service.build_stats())