othr
This commit is contained in:
@@ -172,7 +172,12 @@ class DealService(BaseService):
|
||||
joinedload(Deal.client),
|
||||
selectinload(Deal.services)
|
||||
.joinedload(models.secondary.DealService.service)
|
||||
.joinedload(Service.category))
|
||||
.joinedload(Service.category),
|
||||
selectinload(Deal.products)
|
||||
.joinedload(models.secondary.DealProduct.product)
|
||||
.joinedload(models.Product.client)
|
||||
.selectinload(models.Product.barcodes)
|
||||
)
|
||||
.where(Deal.id == deal_id)
|
||||
)
|
||||
if not deal:
|
||||
|
||||
@@ -6,9 +6,11 @@ from models.product import Product, ProductBarcode
|
||||
from schemas.base import PaginationSchema
|
||||
from services.base import BaseService
|
||||
from schemas.product import *
|
||||
from utils.dependecies import is_valid_pagination
|
||||
|
||||
|
||||
class ProductService(BaseService):
|
||||
|
||||
async def create(self, request: ProductCreateRequest) -> ProductCreateResponse:
|
||||
# Unique article validation
|
||||
existing_product_query = await self.session.execute(
|
||||
@@ -85,23 +87,39 @@ class ProductService(BaseService):
|
||||
async def get_by_client_id(self, client_id: int, pagination: PaginationSchema) -> ProductGetResponse:
|
||||
stmt = (
|
||||
select(Product)
|
||||
.options(selectinload(Product.barcodes))
|
||||
.options(selectinload(Product.barcodes)
|
||||
.noload(ProductBarcode.product))
|
||||
.where(Product.client_id == client_id)
|
||||
.order_by(Product.id)
|
||||
)
|
||||
total_products_query = await self.session.execute(
|
||||
select(
|
||||
func.cast(func.ceil(func.count() / pagination.items_per_page), Integer),
|
||||
func.count()
|
||||
if is_valid_pagination(pagination):
|
||||
total_products_query = await self.session.execute(
|
||||
select(
|
||||
func.cast(func.ceil(func.count() / pagination.items_per_page), Integer),
|
||||
func.count()
|
||||
)
|
||||
.select_from(stmt.subquery())
|
||||
)
|
||||
.select_from(stmt.subquery())
|
||||
)
|
||||
total_pages, total_items = total_products_query.first()
|
||||
total_pages, total_items = total_products_query.first()
|
||||
else:
|
||||
total_items_query = await self.session.execute(
|
||||
select(func.count())
|
||||
.select_from(stmt.subquery())
|
||||
)
|
||||
total_items = total_items_query.scalar()
|
||||
total_pages = 1
|
||||
pagination_info = PaginationInfoSchema(total_pages=total_pages, total_items=total_items)
|
||||
|
||||
if is_valid_pagination(pagination):
|
||||
stmt = (
|
||||
stmt
|
||||
.offset(pagination.page * pagination.items_per_page)
|
||||
.limit(pagination.items_per_page)
|
||||
)
|
||||
|
||||
query = await self.session.execute(
|
||||
stmt
|
||||
.offset(pagination.page * pagination.items_per_page)
|
||||
.limit(pagination.items_per_page)
|
||||
.order_by(Product.id)
|
||||
)
|
||||
products: list[ProductSchema] = []
|
||||
for product in query.scalars().all():
|
||||
|
||||
Reference in New Issue
Block a user