chore: minor fixes and formatting
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
from typing import Union, Optional
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from config import PAGE_SIZE
|
||||
from dto.catalogue import CatalogueType, CatalogueView
|
||||
from misc.mapper import parse_cat_id, serialize_cat_id
|
||||
from misc.texts import get_breadcrumps_path
|
||||
from repositories import ProductRepository, CategoriesRepository
|
||||
from config import PAGE_SIZE
|
||||
from repositories import CategoriesRepository, ProductRepository
|
||||
|
||||
|
||||
class CatalogueService:
|
||||
@@ -16,14 +15,17 @@ class CatalogueService:
|
||||
self.categories_repo = categories_repo
|
||||
|
||||
async def build_category_view(
|
||||
self, session: AsyncSession, cat_id: Optional[Union[int, str]], page: int = 0
|
||||
self,
|
||||
session: AsyncSession,
|
||||
cat_id: int | str | None,
|
||||
page: int = 0,
|
||||
*,
|
||||
include_hidden: bool = False,
|
||||
) -> CatalogueView:
|
||||
cat_id = parse_cat_id(cat_id)
|
||||
|
||||
view = CatalogueView(page=page)
|
||||
children = await self.categories_repo.get_categories_by_parent_id(
|
||||
session, cat_id
|
||||
)
|
||||
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
|
||||
@@ -35,7 +37,11 @@ class CatalogueService:
|
||||
|
||||
if not children: # Products
|
||||
products = await self.product_repo.get_product_by_category(
|
||||
session, cat_id, limit=PAGE_SIZE + 1, offset=view.page * PAGE_SIZE
|
||||
session,
|
||||
cat_id,
|
||||
limit=PAGE_SIZE + 1,
|
||||
offset=view.page * PAGE_SIZE,
|
||||
include_hidden=include_hidden,
|
||||
)
|
||||
if products:
|
||||
view.view_type = CatalogueType.PRODUCTS
|
||||
@@ -51,7 +57,7 @@ class CatalogueService:
|
||||
|
||||
return view
|
||||
|
||||
async def get_path(self, session: AsyncSession, cat_id: Union[int, str]):
|
||||
async def get_path(self, session: AsyncSession, cat_id: int | str):
|
||||
category = int(cat_id) if isinstance(cat_id, int) or cat_id.isdigit() else None
|
||||
products = await self.categories_repo.get_category_path(session, category)
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import logging
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from config import PAGE_SIZE
|
||||
from db.models.orders import OrderStatus
|
||||
from dto.cart import CartDTO, CartItemDTO
|
||||
@@ -9,13 +11,11 @@ from repositories.orders import OrderRepository
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OrderServiceException(Exception): ...
|
||||
class OrderServiceError(Exception): ...
|
||||
|
||||
|
||||
class OrderService:
|
||||
def __init__(
|
||||
self, order_repo: OrderRepository, order_items_repo: OrderItemRepository
|
||||
):
|
||||
def __init__(self, order_repo: OrderRepository, order_items_repo: OrderItemRepository):
|
||||
self.order_repo = order_repo
|
||||
self.order_items_repo = order_items_repo
|
||||
|
||||
@@ -34,9 +34,7 @@ class OrderService:
|
||||
order = await self.order_repo.get_draft_order_by_user(session, customer)
|
||||
|
||||
if not order:
|
||||
order = await self.order_repo.create_order(
|
||||
session, customer, OrderStatus.DRAFT
|
||||
)
|
||||
order = await self.order_repo.create_order(session, customer, OrderStatus.DRAFT)
|
||||
|
||||
order_item = await self.order_items_repo.get_item_by_order_and_product(
|
||||
session, order_id=order.id, product_id=product_id
|
||||
@@ -65,7 +63,7 @@ class OrderService:
|
||||
order = await self.order_repo.get_draft_order_by_user(session, customer)
|
||||
|
||||
if not order:
|
||||
raise OrderServiceException(
|
||||
raise OrderServiceError(
|
||||
"attempt of removing product from non-existing cart, potential callback_query exploit, ignoring..."
|
||||
)
|
||||
|
||||
@@ -74,7 +72,7 @@ class OrderService:
|
||||
)
|
||||
|
||||
if not order_item:
|
||||
raise OrderServiceException(
|
||||
raise OrderServiceError(
|
||||
"attempt of decreasing quantity of non-existing order_item, potential callback_query exploit, ignoring..."
|
||||
)
|
||||
|
||||
@@ -88,13 +86,11 @@ class OrderService:
|
||||
|
||||
return new_quantity
|
||||
|
||||
async def clear_from_cart(
|
||||
self, session: AsyncSession, *, customer: int, product_id: int
|
||||
):
|
||||
async def clear_from_cart(self, session: AsyncSession, *, customer: int, product_id: int):
|
||||
order = await self.order_repo.get_draft_order_by_user(session, customer)
|
||||
|
||||
if not order:
|
||||
raise OrderServiceException(
|
||||
raise OrderServiceError(
|
||||
"attempt of removing product from non-existing cart, potential callback_query exploit, ignoring..."
|
||||
)
|
||||
|
||||
@@ -103,7 +99,7 @@ class OrderService:
|
||||
)
|
||||
|
||||
if not order_item:
|
||||
raise OrderServiceException(
|
||||
raise OrderServiceError(
|
||||
"attempt of decreasing quantity of non-existing order_item, potential callback_query exploit, ignoring..."
|
||||
)
|
||||
|
||||
@@ -162,9 +158,7 @@ class OrderService:
|
||||
|
||||
return CartDTO(items=cart_items, total=total, order_id=order.id)
|
||||
|
||||
async def build_full_cart_dto(
|
||||
self, session: AsyncSession, customer: int
|
||||
) -> CartDTO:
|
||||
async def build_full_cart_dto(self, session: AsyncSession, customer: int) -> CartDTO:
|
||||
order = await self.order_repo.get_draft_order_by_user(session, customer)
|
||||
|
||||
cart_items = []
|
||||
|
||||
Reference in New Issue
Block a user