39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
import uuid
|
|
from repositories.pocketbase_client import pb_client
|
|
from repositories.repo_config import USE_MOCK_REPOSITORIES
|
|
|
|
COLLECTION = 'price_history'
|
|
|
|
|
|
class PriceHistoryRepository:
|
|
"""Data access for the `price_history` collection."""
|
|
|
|
def create(self, data):
|
|
return pb_client.create(COLLECTION, data)
|
|
|
|
def list_for_product(self, product_id, limit=50):
|
|
return pb_client.list(
|
|
COLLECTION, filter_str=f'product_id = "{product_id}"', sort='-scraped_at', per_page=limit
|
|
)
|
|
|
|
|
|
class MockPriceHistoryRepository:
|
|
"""In-memory stand-in for PriceHistoryRepository."""
|
|
|
|
def __init__(self):
|
|
self._records = {}
|
|
|
|
def create(self, data):
|
|
record_id = data.get('id') or str(uuid.uuid4())
|
|
record = {'id': record_id, **data}
|
|
self._records[record_id] = record
|
|
return record
|
|
|
|
def list_for_product(self, product_id, limit=50):
|
|
items = [r for r in self._records.values() if r.get('product_id') == product_id]
|
|
items.sort(key=lambda r: r.get('scraped_at') or '', reverse=True)
|
|
return items[:limit]
|
|
|
|
|
|
price_history_repository = MockPriceHistoryRepository() if USE_MOCK_REPOSITORIES else PriceHistoryRepository()
|