arbret/backend/repositories/pricing.py
counterweight 32ce27180d
Step 1: Add PricingConfig model and PricingRepository
- Create PricingConfig model with all required fields (premium settings, trade limits)
- Implement PricingRepository with singleton pattern (get_current, create_or_update)
- Add comprehensive tests for repository functionality
- Export model and repository in __init__.py files
2025-12-26 20:08:35 +01:00

83 lines
2.9 KiB
Python

"""Pricing repository for database queries."""
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from models import PricingConfig
class PricingRepository:
"""Repository for pricing configuration database queries."""
def __init__(self, db: AsyncSession):
self.db = db
async def get_current(self) -> PricingConfig | None:
"""Get the current pricing configuration (singleton).
Returns:
PricingConfig if exists, None otherwise
"""
result = await self.db.execute(select(PricingConfig).limit(1))
return result.scalar_one_or_none()
async def create_or_update(
self,
premium_buy: int,
premium_sell: int,
small_trade_threshold_eur: int,
small_trade_extra_premium: int,
eur_min_buy: int,
eur_max_buy: int,
eur_min_sell: int,
eur_max_sell: int,
) -> PricingConfig:
"""Create or update pricing configuration (singleton pattern).
If no config exists, creates a new one.
If config exists, updates the existing record.
Args:
premium_buy: Premium percentage for BUY direction (-100 to 100)
premium_sell: Premium percentage for SELL direction (-100 to 100)
small_trade_threshold_eur: Threshold in EUR cents for small trade
extra premium
small_trade_extra_premium: Extra premium percentage for small trades
(-100 to 100)
eur_min_buy: Minimum trade amount in EUR cents for BUY
eur_max_buy: Maximum trade amount in EUR cents for BUY
eur_min_sell: Minimum trade amount in EUR cents for SELL
eur_max_sell: Maximum trade amount in EUR cents for SELL
Returns:
Created or updated PricingConfig record
"""
config = await self.get_current()
if config is None:
# Create new config
config = PricingConfig(
premium_buy=premium_buy,
premium_sell=premium_sell,
small_trade_threshold_eur=small_trade_threshold_eur,
small_trade_extra_premium=small_trade_extra_premium,
eur_min_buy=eur_min_buy,
eur_max_buy=eur_max_buy,
eur_min_sell=eur_min_sell,
eur_max_sell=eur_max_sell,
)
self.db.add(config)
else:
# Update existing config
config.premium_buy = premium_buy
config.premium_sell = premium_sell
config.small_trade_threshold_eur = small_trade_threshold_eur
config.small_trade_extra_premium = small_trade_extra_premium
config.eur_min_buy = eur_min_buy
config.eur_max_buy = eur_max_buy
config.eur_min_sell = eur_min_sell
config.eur_max_sell = eur_max_sell
await self.db.commit()
await self.db.refresh(config)
return config