slot_snapshot_service.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # app/services/slot_snapshot_service.py
  2. from sqlalchemy.ext.asyncio import AsyncSession
  3. from sqlalchemy import select
  4. from app.models.slot_snapshot import VasSlotSnapshot
  5. from app.schemas.slot_snapshot import SlotSnapshotCreate
  6. class SlotSnapshotService:
  7. @staticmethod
  8. async def create(
  9. db: AsyncSession,
  10. data: SlotSnapshotCreate
  11. ) -> VasSlotSnapshot:
  12. rec = VasSlotSnapshot(**data.dict())
  13. db.add(rec)
  14. await db.commit()
  15. await db.refresh(rec)
  16. return rec
  17. @staticmethod
  18. async def latest_for(
  19. db: AsyncSession,
  20. country: str,
  21. city: str,
  22. visa_type: str
  23. ) -> VasSlotSnapshot:
  24. stmt = (
  25. select(VasSlotSnapshot)
  26. .where(
  27. VasSlotSnapshot.country == country,
  28. VasSlotSnapshot.city == city,
  29. VasSlotSnapshot.visa_type == visa_type,
  30. )
  31. .order_by(VasSlotSnapshot.snapshot_at.desc())
  32. .limit(1)
  33. )
  34. return await db.scalar(stmt)