product management, unification of kb and so on.

This commit is contained in:
2026-02-24 20:47:52 +07:00
parent 36ffca460f
commit 035170e46f
23 changed files with 470 additions and 130 deletions

View File

@@ -1,6 +1,6 @@
from typing import Optional
from sqlalchemy import select, update
from sqlalchemy import select
from db.models import Invoice
from sqlalchemy.ext.asyncio import AsyncSession
@@ -37,19 +37,15 @@ class InvoiceRepository:
async def update_invoice_msg(
self, session: AsyncSession, *, invoice_id: int, inline_message_id: int
):
stmt = (
update(Invoice.__table__)
.where(Invoice.id == invoice_id)
.values(inline_message_id=inline_message_id)
)
await session.execute(stmt)
invoice = await self.get_invoice_by_id(session, invoice_id)
invoice.inline_message_id = inline_message_id
await session.commit()
async def update_invoice_status(
self, session: AsyncSession, *, invoice_id: int, status: InvoiceStatus
):
stmt = (
update(Invoice.__table__)
.where(Invoice.id == invoice_id)
.values(status=status)
)
await session.execute(stmt)
invoice = await self.get_invoice_by_id(session, invoice_id)
invoice.status = status
await session.commit()

View File

@@ -1,5 +1,5 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import delete, func, select, update
from sqlalchemy import delete, func, select
from typing import Optional
from db.models.orders import Order, OrderItem, OrderStatus
@@ -55,13 +55,9 @@ class OrderItemRepository:
async def update_order_item_quantity(
self, session: AsyncSession, order_item_id: int, *, quantity: int = 0
):
stmt = (
update(OrderItem.__table__)
.where(OrderItem.id == order_item_id)
.values(quantity=quantity)
)
item = await self.get_item_by_id(session, order_item_id)
item.quantity = quantity
await session.execute(stmt)
await session.commit()
async def create_order_item(
@@ -82,7 +78,7 @@ class OrderItemRepository:
return order_item
async def delete_item(self, session: AsyncSession, item_id: int):
stmt = delete(OrderItem.__table__).where(OrderItem.id == item_id)
stmt = delete(OrderItem).where(OrderItem.id == item_id)
await session.execute(stmt)
await session.commit()

View File

@@ -48,13 +48,21 @@ class OrderRepository:
return order
async def remove_order_by_id(self, session: AsyncSession, order_id: int):
stmt = delete(Order.__table__).where(Order.id == order_id)
stmt = delete(Order).where(Order.id == order_id)
await session.execute(stmt)
await session.commit()
async def remove_order_by_customer(self, session: AsyncSession, customer: int):
stmt = delete(Order.__table__).where(Order.customer == customer)
stmt = delete(Order).where(Order.customer == customer)
await session.execute(stmt)
await session.commit()
async def update_order_status(
self, session: AsyncSession, order_id: int, status: OrderStatus
):
order = await self.get_order_by_id(session, order_id)
order.status = status
await session.commit()

View File

@@ -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