55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
from typing import Union, Optional
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from dto.catalogue import CatalogueType, CatalogueView
|
|
from repositories import ProductRepository, CategoriesRepository
|
|
from config import PAGE_SIZE
|
|
|
|
|
|
class CatalogueService:
|
|
def __init__(
|
|
self, product_repo: ProductRepository, categories_repo: CategoriesRepository
|
|
) -> None:
|
|
self.product_repo = product_repo
|
|
self.categories_repo = categories_repo
|
|
|
|
async def build_category_view(
|
|
self, session: AsyncSession, cat_id: Optional[Union[int, str]], page: int = 0
|
|
) -> CatalogueView:
|
|
cat_id = (
|
|
int(cat_id)
|
|
if isinstance(cat_id, (int, str)) and str(cat_id).isdigit()
|
|
else None
|
|
)
|
|
|
|
view = CatalogueView(page=page)
|
|
children = await self.categories_repo.get_categories_by_parent_id(
|
|
session, cat_id
|
|
)
|
|
category = await self.categories_repo.get_category_by_id(session, cat_id)
|
|
|
|
if not category: # Root category
|
|
view.category = "root"
|
|
view.show_menu = True
|
|
else: # Subcategories
|
|
view.category = cat_id
|
|
view.parent_id = category.parent_id or "root"
|
|
|
|
if not children: # Products
|
|
view.view_type = CatalogueType.PRODUCTS
|
|
products = await self.product_repo.get_product_by_category(
|
|
session, cat_id, limit=PAGE_SIZE + 1, offset=view.page * PAGE_SIZE
|
|
)
|
|
|
|
if not products:
|
|
view.view_type = CatalogueType.EMPTY
|
|
|
|
view.products = products[:PAGE_SIZE]
|
|
view.has_next = len(products) > PAGE_SIZE
|
|
return view
|
|
|
|
view.view_type = CatalogueType.CATEGORIES
|
|
view.children = children[:PAGE_SIZE]
|
|
|
|
return view
|