refactors
This commit is contained in:
parent
139a5fbef3
commit
f46d2ae8b3
12 changed files with 734 additions and 536 deletions
6
backend/repositories/__init__.py
Normal file
6
backend/repositories/__init__.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
"""Repository layer for database queries."""
|
||||
|
||||
from repositories.price import PriceRepository
|
||||
from repositories.user import UserRepository
|
||||
|
||||
__all__ = ["PriceRepository", "UserRepository"]
|
||||
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()
|
||||
23
backend/repositories/user.py
Normal file
23
backend/repositories/user.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
"""User repository for database queries."""
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from models import User
|
||||
|
||||
|
||||
class UserRepository:
|
||||
"""Repository for user-related database queries."""
|
||||
|
||||
def __init__(self, db: AsyncSession):
|
||||
self.db = db
|
||||
|
||||
async def get_by_email(self, email: str) -> User | None:
|
||||
"""Get a user by email."""
|
||||
result = await self.db.execute(select(User).where(User.email == email))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
async def get_by_id(self, user_id: int) -> User | None:
|
||||
"""Get a user by ID."""
|
||||
result = await self.db.execute(select(User).where(User.id == user_id))
|
||||
return result.scalar_one_or_none()
|
||||
Loading…
Add table
Add a link
Reference in a new issue