arbret/backend/routes/audit.py
counterweight badb45da59
Extract price logic to PriceService
- Create PriceService with get_recent_prices() and fetch_and_store_price()
- Update routes/audit.py to use PriceService instead of direct queries
- Use PriceHistoryMapper consistently
- Update test to patch services.price.fetch_btc_eur_price
2025-12-25 18:30:26 +01:00

42 lines
1.5 KiB
Python

"""Audit routes for price history."""
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from auth import require_permission
from database import get_db
from mappers import PriceHistoryMapper
from models import Permission, User
from schemas import PriceHistoryResponse
from services.price import PriceService
router = APIRouter(prefix="/api/audit", tags=["audit"])
# =============================================================================
# Price History Endpoints
# =============================================================================
@router.get("/price-history", response_model=list[PriceHistoryResponse])
async def get_price_history(
db: AsyncSession = Depends(get_db),
_current_user: User = Depends(require_permission(Permission.VIEW_AUDIT)),
) -> list[PriceHistoryResponse]:
"""Get the 20 most recent price history records."""
service = PriceService(db)
records = await service.get_recent_prices()
return [PriceHistoryMapper.to_response(record) for record in records]
@router.post("/price-history/fetch", response_model=PriceHistoryResponse)
async def fetch_price_now(
db: AsyncSession = Depends(get_db),
_current_user: User = Depends(require_permission(Permission.FETCH_PRICE)),
) -> PriceHistoryResponse:
"""Manually trigger a price fetch from Bitfinex."""
service = PriceService(db)
record = await service.fetch_and_store_price()
return PriceHistoryMapper.to_response(record)