chore: minor fixes and formatting
This commit is contained in:
@@ -1,20 +1,17 @@
|
||||
from typing import Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.orm import aliased
|
||||
|
||||
from db.models import Category
|
||||
|
||||
|
||||
class CategoriesRepository:
|
||||
async def get_category_by_id(
|
||||
self, session: AsyncSession, category_id: int
|
||||
) -> Optional[Category]:
|
||||
async def get_category_by_id(self, session: AsyncSession, category_id: int) -> Category | None:
|
||||
stmt = select(Category).where(Category.id == category_id)
|
||||
return await session.scalar(stmt)
|
||||
|
||||
async def get_categories_by_parent_id(
|
||||
self, session: AsyncSession, parent_id: Optional[int] = None
|
||||
self, session: AsyncSession, parent_id: int | None = None
|
||||
) -> list[Category]:
|
||||
stmt = select(Category).where(Category.parent_id == parent_id)
|
||||
result = await session.scalars(stmt)
|
||||
@@ -31,9 +28,7 @@ class CategoriesRepository:
|
||||
parent = aliased(Category)
|
||||
|
||||
cte = cte.union_all(
|
||||
select(parent.id, parent.parent_id, parent.name).join(
|
||||
cte, cte.c.parent_id == parent.id
|
||||
)
|
||||
select(parent.id, parent.parent_id, parent.name).join(cte, cte.c.parent_id == parent.id)
|
||||
)
|
||||
|
||||
stmt = select(cte)
|
||||
@@ -45,7 +40,7 @@ class CategoriesRepository:
|
||||
return list(reversed(rows))
|
||||
|
||||
async def add_category(
|
||||
self, session: AsyncSession, *, name: str, parent_id: Optional[int]
|
||||
self, session: AsyncSession, *, name: str, parent_id: int | None
|
||||
) -> Category:
|
||||
category = Category(name=name, parent_id=parent_id)
|
||||
|
||||
@@ -54,9 +49,7 @@ class CategoriesRepository:
|
||||
|
||||
return category
|
||||
|
||||
async def update_category_name(
|
||||
self, session: AsyncSession, category_id: int, value: str
|
||||
):
|
||||
async def update_category_name(self, session: AsyncSession, category_id: int, value: str):
|
||||
category = await self.get_category_by_id(session, category_id)
|
||||
category.name = value
|
||||
|
||||
|
||||
Reference in New Issue
Block a user