Delegate exchange persistence to ExchangeRepository
- Add create() and update() methods to ExchangeRepository - Update ExchangeService to use repository methods instead of direct db operations - All persistence operations now go through repositories consistently - Fix indentation errors in ExchangeService
This commit is contained in:
parent
c4594a3f73
commit
33aa8ad13b
2 changed files with 33 additions and 18 deletions
|
|
@ -161,3 +161,32 @@ class ExchangeRepository:
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return {row[0] for row in result.all()}
|
return {row[0] for row in result.all()}
|
||||||
|
|
||||||
|
async def create(self, exchange: Exchange) -> Exchange:
|
||||||
|
"""
|
||||||
|
Create a new exchange record.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
exchange: Exchange instance to persist
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Created Exchange record (committed and refreshed)
|
||||||
|
"""
|
||||||
|
self.db.add(exchange)
|
||||||
|
await self.db.commit()
|
||||||
|
await self.db.refresh(exchange)
|
||||||
|
return exchange
|
||||||
|
|
||||||
|
async def update(self, exchange: Exchange) -> Exchange:
|
||||||
|
"""
|
||||||
|
Update an existing exchange record.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
exchange: Exchange instance to update
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Updated Exchange record (committed and refreshed)
|
||||||
|
"""
|
||||||
|
await self.db.commit()
|
||||||
|
await self.db.refresh(exchange)
|
||||||
|
return exchange
|
||||||
|
|
|
||||||
|
|
@ -267,11 +267,8 @@ class ExchangeService:
|
||||||
status=ExchangeStatus.BOOKED,
|
status=ExchangeStatus.BOOKED,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.db.add(exchange)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
await self.db.commit()
|
return await self.exchange_repo.create(exchange)
|
||||||
await self.db.refresh(exchange)
|
|
||||||
except IntegrityError as e:
|
except IntegrityError as e:
|
||||||
await self.db.rollback()
|
await self.db.rollback()
|
||||||
# This should rarely happen now since we check explicitly above,
|
# This should rarely happen now since we check explicitly above,
|
||||||
|
|
@ -280,8 +277,6 @@ class ExchangeService:
|
||||||
"Database constraint violation. Please try again."
|
"Database constraint violation. Please try again."
|
||||||
) from e
|
) from e
|
||||||
|
|
||||||
return exchange
|
|
||||||
|
|
||||||
async def get_exchange_by_public_id(
|
async def get_exchange_by_public_id(
|
||||||
self, public_id: uuid.UUID, user: User | None = None
|
self, public_id: uuid.UUID, user: User | None = None
|
||||||
) -> Exchange:
|
) -> Exchange:
|
||||||
|
|
@ -331,10 +326,7 @@ class ExchangeService:
|
||||||
)
|
)
|
||||||
exchange.cancelled_at = datetime.now(UTC)
|
exchange.cancelled_at = datetime.now(UTC)
|
||||||
|
|
||||||
await self.db.commit()
|
return await self.exchange_repo.update(exchange)
|
||||||
await self.db.refresh(exchange)
|
|
||||||
|
|
||||||
return exchange
|
|
||||||
|
|
||||||
async def complete_exchange(self, exchange: Exchange) -> Exchange:
|
async def complete_exchange(self, exchange: Exchange) -> Exchange:
|
||||||
"""
|
"""
|
||||||
|
|
@ -354,10 +346,7 @@ class ExchangeService:
|
||||||
exchange.status = ExchangeStatus.COMPLETED
|
exchange.status = ExchangeStatus.COMPLETED
|
||||||
exchange.completed_at = datetime.now(UTC)
|
exchange.completed_at = datetime.now(UTC)
|
||||||
|
|
||||||
await self.db.commit()
|
return await self.exchange_repo.update(exchange)
|
||||||
await self.db.refresh(exchange)
|
|
||||||
|
|
||||||
return exchange
|
|
||||||
|
|
||||||
async def mark_no_show(self, exchange: Exchange) -> Exchange:
|
async def mark_no_show(self, exchange: Exchange) -> Exchange:
|
||||||
"""
|
"""
|
||||||
|
|
@ -379,10 +368,7 @@ class ExchangeService:
|
||||||
exchange.status = ExchangeStatus.NO_SHOW
|
exchange.status = ExchangeStatus.NO_SHOW
|
||||||
exchange.completed_at = datetime.now(UTC)
|
exchange.completed_at = datetime.now(UTC)
|
||||||
|
|
||||||
await self.db.commit()
|
return await self.exchange_repo.update(exchange)
|
||||||
await self.db.refresh(exchange)
|
|
||||||
|
|
||||||
return exchange
|
|
||||||
|
|
||||||
def _expand_availability_to_slots(
|
def _expand_availability_to_slots(
|
||||||
self, avail: Availability, slot_date: date, booked_starts: set[datetime]
|
self, avail: Availability, slot_date: date, booked_starts: set[datetime]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue