22 lines
772 B
Python
22 lines
772 B
Python
from typing import Sequence
|
|
|
|
from fooder.domain import Product
|
|
from fooder.repository.expression import fuzzy_match
|
|
from fooder.repository.base import RepositoryBase, DEFAULT_LIMIT
|
|
|
|
|
|
class ProductRepository(RepositoryBase[Product]):
|
|
async def get_by_id(self, product_id: int) -> Product:
|
|
return await self._get(Product.id == product_id)
|
|
|
|
async def get_by_barcode(self, barcode: str) -> Product:
|
|
return await self._get(Product.barcode == barcode)
|
|
|
|
async def list(
|
|
self,
|
|
q: str | None = None,
|
|
offset: int = 0,
|
|
limit: int | None = DEFAULT_LIMIT,
|
|
) -> Sequence[Product]:
|
|
expressions = (fuzzy_match(Product.name, q),) if q else ()
|
|
return await self._list(*expressions, offset=offset, limit=limit)
|