catalogue operations, db migr and so on.

pre release version.
This commit is contained in:
2026-03-11 20:26:38 +07:00
parent 1af0cf1826
commit 7f4f8a883d
17 changed files with 284 additions and 63 deletions

View File

@@ -1,6 +1,7 @@
from typing import Optional
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select
from typing import Optional
from sqlalchemy.orm import aliased
from db.models import Category
@@ -19,3 +20,36 @@ class CategoriesRepository:
result = await session.scalars(stmt)
return list(result)
async def get_category_path(self, session: AsyncSession, category_id: int):
base = select(Category.id, Category.parent_id, Category.name).where(
Category.id == category_id
)
cte = base.cte(name="path", recursive=True)
parent = aliased(Category)
cte = cte.union_all(
select(parent.id, parent.parent_id, parent.name).join(
cte, cte.c.parent_id == parent.id
)
)
stmt = select(cte)
result = await session.execute(stmt)
rows = result.all()
return list(reversed(rows))
async def add_category(
self, session: AsyncSession, *, name: str, parent_id: Optional[int]
) -> Category:
category = Category(name=name, parent_id=parent_id)
session.add(category)
await session.commit()
return category

View File

@@ -1,5 +1,5 @@
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select, update
from sqlalchemy import select
from sqlalchemy.sql.expression import func
from typing import Optional
@@ -32,13 +32,9 @@ class ProductRepository:
async def add_product_file_id(
self, session: AsyncSession, product_id: int, *, file_id: str
) -> None:
stmt = (
update(Product)
.where(Product.id == product_id)
.where(Product.file_id is None)
.values(file_id=file_id)
)
await session.execute(stmt)
product = await self.get_product_by_id(session, product_id)
product.file_id = file_id
await session.commit()
async def search(