29 lines
802 B
Python
29 lines
802 B
Python
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select
|
|
from typing import Optional
|
|
|
|
from db.models import Product
|
|
|
|
|
|
class ProductRepository:
|
|
async def get_product_by_id(
|
|
self, session: AsyncSession, product_id: int
|
|
) -> Optional[Product]:
|
|
stmt = select(Product).where(Product.id == product_id)
|
|
return await session.scalar(stmt)
|
|
|
|
async def get_product_by_category(
|
|
self, session: AsyncSession, category_id: int, *, limit: int, offset: int
|
|
) -> list[Product]:
|
|
stmt = (
|
|
select(Product)
|
|
.where(Product.category_id == category_id)
|
|
.order_by(Product.id)
|
|
.offset(offset)
|
|
.limit(limit)
|
|
)
|
|
|
|
result = await session.scalars(stmt)
|
|
|
|
return list(result)
|