product management, unification of kb and so on.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.sql.expression import func
|
||||
from typing import Optional
|
||||
|
||||
from config import PAGE_SIZE
|
||||
from db.models import Product
|
||||
|
||||
|
||||
@@ -31,10 +33,59 @@ class ProductRepository:
|
||||
self, session: AsyncSession, product_id: int, *, file_id: str
|
||||
) -> None:
|
||||
stmt = (
|
||||
update(Product.__table__)
|
||||
update(Product)
|
||||
.where(Product.id == product_id)
|
||||
.where(Product.file_id is None)
|
||||
.values(file_id=file_id)
|
||||
)
|
||||
await session.execute(stmt)
|
||||
await session.commit()
|
||||
|
||||
async def search(
|
||||
self,
|
||||
session: AsyncSession,
|
||||
query: str,
|
||||
*,
|
||||
limit: int = PAGE_SIZE + 1,
|
||||
offset: int = 0,
|
||||
) -> list[Optional[Product]]:
|
||||
ts_query = func.plainto_tsquery("simple", query)
|
||||
|
||||
stmt = (
|
||||
select(Product)
|
||||
.where(Product.search_vector.op("@@")(ts_query))
|
||||
.order_by(func.ts_rank(Product.search_vector, ts_query).desc())
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
)
|
||||
|
||||
res = await session.scalars(stmt)
|
||||
|
||||
return list(res)
|
||||
|
||||
async def update_product_name_by_id(
|
||||
self, session: AsyncSession, product_id: int, name: str
|
||||
):
|
||||
product = await self.get_product_by_id(session, product_id=product_id)
|
||||
product.name = name
|
||||
|
||||
await session.commit()
|
||||
return product
|
||||
|
||||
async def update_product_description_by_id(
|
||||
self, session: AsyncSession, product_id: int, description: str
|
||||
):
|
||||
product = await self.get_product_by_id(session, product_id=product_id)
|
||||
product.description = description
|
||||
|
||||
await session.commit()
|
||||
return product
|
||||
|
||||
async def update_product_price_by_id(
|
||||
self, session: AsyncSession, product_id: int, price: str
|
||||
):
|
||||
product = await self.get_product_by_id(session, product_id=product_id)
|
||||
product.price = price
|
||||
|
||||
await session.commit()
|
||||
return product
|
||||
|
||||
Reference in New Issue
Block a user