41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, update
|
|
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)
|
|
|
|
async def add_product_file_id(
|
|
self, session: AsyncSession, product_id: int, *, file_id: str
|
|
) -> None:
|
|
stmt = (
|
|
update(Product.__table__)
|
|
.where(Product.id == product_id)
|
|
.where(Product.file_id is None)
|
|
.values(file_id=file_id)
|
|
)
|
|
await session.execute(stmt)
|
|
await session.commit()
|