27 lines
852 B
Python
27 lines
852 B
Python
"""Price repository for database queries."""
|
|
|
|
from sqlalchemy import desc, select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from models import PriceHistory
|
|
from price_fetcher import PAIR_BTC_EUR, SOURCE_BITFINEX
|
|
|
|
|
|
class PriceRepository:
|
|
"""Repository for price-related database queries."""
|
|
|
|
def __init__(self, db: AsyncSession):
|
|
self.db = db
|
|
|
|
async def get_latest(
|
|
self, source: str = SOURCE_BITFINEX, pair: str = PAIR_BTC_EUR
|
|
) -> PriceHistory | None:
|
|
"""Get the most recent price from the database."""
|
|
query = (
|
|
select(PriceHistory)
|
|
.where(PriceHistory.source == source, PriceHistory.pair == pair)
|
|
.order_by(desc(PriceHistory.timestamp))
|
|
.limit(1)
|
|
)
|
|
result = await self.db.execute(query)
|
|
return result.scalar_one_or_none()
|