Delegate price persistence to PriceRepository

- Add create() method to PriceRepository
- Update PriceService to use repository.create() instead of direct db operations
This commit is contained in:
counterweight 2025-12-25 18:51:24 +01:00
parent 280c1e5687
commit 4cb561d54f
Signed by: counterweight
GPG key ID: 883EDBAA726BD96C
2 changed files with 16 additions and 4 deletions

View file

@ -56,3 +56,18 @@ class PriceRepository:
) )
) )
return result.scalar_one_or_none() return result.scalar_one_or_none()
async def create(self, record: PriceHistory) -> PriceHistory:
"""
Create a new price history record.
Args:
record: PriceHistory instance to persist
Returns:
Created PriceHistory record (refreshed from database)
"""
self.db.add(record)
await self.db.commit()
await self.db.refresh(record)
return record

View file

@ -52,12 +52,9 @@ class PriceService:
price=price_value, price=price_value,
timestamp=timestamp, timestamp=timestamp,
) )
self.db.add(record)
try: try:
await self.db.commit() return await self.price_repo.create(record)
await self.db.refresh(record)
return record
except IntegrityError: except IntegrityError:
# Duplicate timestamp - return the existing record # Duplicate timestamp - return the existing record
await self.db.rollback() await self.db.rollback()