2025-12-25 18:27:59 +01:00
|
|
|
"""Availability repository for database queries."""
|
|
|
|
|
|
|
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
|
|
from sqlalchemy import and_, delete, select
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from models import Availability
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AvailabilityRepository:
|
|
|
|
|
"""Repository for availability-related database queries."""
|
|
|
|
|
|
|
|
|
|
def __init__(self, db: AsyncSession):
|
|
|
|
|
self.db = db
|
|
|
|
|
|
|
|
|
|
async def get_by_date_range(
|
|
|
|
|
self, from_date: date, to_date: date
|
|
|
|
|
) -> list[Availability]:
|
|
|
|
|
"""Get availability slots for a date range."""
|
|
|
|
|
result = await self.db.execute(
|
|
|
|
|
select(Availability)
|
|
|
|
|
.where(and_(Availability.date >= from_date, Availability.date <= to_date))
|
|
|
|
|
.order_by(Availability.date, Availability.start_time)
|
|
|
|
|
)
|
|
|
|
|
return list(result.scalars().all())
|
|
|
|
|
|
|
|
|
|
async def get_by_date(self, target_date: date) -> list[Availability]:
|
|
|
|
|
"""Get availability slots for a specific date."""
|
|
|
|
|
result = await self.db.execute(
|
|
|
|
|
select(Availability)
|
|
|
|
|
.where(Availability.date == target_date)
|
|
|
|
|
.order_by(Availability.start_time)
|
|
|
|
|
)
|
|
|
|
|
return list(result.scalars().all())
|
|
|
|
|
|
|
|
|
|
async def delete_by_date(self, target_date: date) -> None:
|
|
|
|
|
"""Delete all availability for a specific date."""
|
|
|
|
|
await self.db.execute(
|
|
|
|
|
delete(Availability).where(Availability.date == target_date)
|
|
|
|
|
)
|
2025-12-25 18:51:55 +01:00
|
|
|
|
|
|
|
|
async def create(self, availability: Availability) -> Availability:
|
|
|
|
|
"""
|
|
|
|
|
Create a new availability record.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
availability: Availability instance to persist
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Created Availability record (flushed to get ID)
|
|
|
|
|
"""
|
|
|
|
|
self.db.add(availability)
|
|
|
|
|
await self.db.flush()
|
|
|
|
|
return availability
|
|
|
|
|
|
|
|
|
|
async def create_multiple(self, availabilities: list[Availability]) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Create multiple availability records in a single transaction.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
availabilities: List of Availability instances to persist
|
|
|
|
|
"""
|
|
|
|
|
for availability in availabilities:
|
|
|
|
|
self.db.add(availability)
|
|
|
|
|
await self.db.flush()
|
2025-12-25 18:57:55 +01:00
|
|
|
|
|
|
|
|
async def commit(self) -> None:
|
|
|
|
|
"""Commit the current transaction."""
|
|
|
|
|
await self.db.commit()
|