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:
parent
280c1e5687
commit
4cb561d54f
2 changed files with 16 additions and 4 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue