catalogue operations, db migr and so on.
pre release version.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user