This commit is contained in:
2024-04-11 14:25:40 +03:00
parent 7f302acdb5
commit 5c81af05d5
9 changed files with 63 additions and 17 deletions

View File

@@ -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():