Delegate availability persistence to AvailabilityRepository
- Add create() and create_multiple() methods to AvailabilityRepository - Update AvailabilityService to use repository methods instead of direct db operations
This commit is contained in:
parent
4cb561d54f
commit
17aead2e21
2 changed files with 41 additions and 12 deletions
|
|
@ -39,3 +39,28 @@ class AvailabilityRepository:
|
||||||
await self.db.execute(
|
await self.db.execute(
|
||||||
delete(Availability).where(Availability.date == target_date)
|
delete(Availability).where(Availability.date == target_date)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
|
||||||
|
|
@ -109,14 +109,15 @@ class AvailabilityService:
|
||||||
await self.availability_repo.delete_by_date(target_date)
|
await self.availability_repo.delete_by_date(target_date)
|
||||||
|
|
||||||
# Create new availability slots
|
# Create new availability slots
|
||||||
for slot in slots:
|
availabilities = [
|
||||||
availability = Availability(
|
Availability(
|
||||||
date=target_date,
|
date=target_date,
|
||||||
start_time=slot.start_time,
|
start_time=slot.start_time,
|
||||||
end_time=slot.end_time,
|
end_time=slot.end_time,
|
||||||
)
|
)
|
||||||
self.db.add(availability)
|
for slot in slots
|
||||||
|
]
|
||||||
|
await self.availability_repo.create_multiple(availabilities)
|
||||||
await self.db.commit()
|
await self.db.commit()
|
||||||
|
|
||||||
return AvailabilityDay(date=target_date, slots=slots)
|
return AvailabilityDay(date=target_date, slots=slots)
|
||||||
|
|
@ -165,19 +166,22 @@ class AvailabilityService:
|
||||||
|
|
||||||
# Copy slots
|
# Copy slots
|
||||||
target_slots: list[TimeSlot] = []
|
target_slots: list[TimeSlot] = []
|
||||||
for source_slot in source_slots:
|
new_availabilities = [
|
||||||
new_availability = Availability(
|
Availability(
|
||||||
date=target_date,
|
date=target_date,
|
||||||
start_time=source_slot.start_time,
|
start_time=source_slot.start_time,
|
||||||
end_time=source_slot.end_time,
|
end_time=source_slot.end_time,
|
||||||
)
|
)
|
||||||
self.db.add(new_availability)
|
for source_slot in source_slots
|
||||||
target_slots.append(
|
]
|
||||||
TimeSlot(
|
await self.availability_repo.create_multiple(new_availabilities)
|
||||||
start_time=source_slot.start_time,
|
target_slots = [
|
||||||
end_time=source_slot.end_time,
|
TimeSlot(
|
||||||
)
|
start_time=slot.start_time,
|
||||||
|
end_time=slot.end_time,
|
||||||
)
|
)
|
||||||
|
for slot in source_slots
|
||||||
|
]
|
||||||
|
|
||||||
copied_days.append(
|
copied_days.append(
|
||||||
AvailabilityDay(date=target_date, slots=target_slots)
|
AvailabilityDay(date=target_date, slots=target_slots)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue