refactors
This commit is contained in:
parent
139a5fbef3
commit
f46d2ae8b3
12 changed files with 734 additions and 536 deletions
27
backend/repositories/price.py
Normal file
27
backend/repositories/price.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
"""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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue