From 2b79e0deba6155609774c3764345993c05451f91 Mon Sep 17 00:00:00 2001 From: admin Date: Sat, 24 Aug 2024 05:20:49 +0300 Subject: [PATCH] feat: product search --- routers/product.py | 4 ++-- services/product.py | 40 ++++++++++++++++++++++++++++++++-------- test.py | 25 +------------------------ 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/routers/product.py b/routers/product.py index 268e330..041946a 100644 --- a/routers/product.py +++ b/routers/product.py @@ -67,9 +67,10 @@ async def delete_product( async def get_product( client_id: int, pagination: Annotated[PaginationSchema, Depends(utils.dependecies.pagination_parameters)], + search_input: str, session: Annotated[AsyncSession, Depends(get_session)] ): - return await ProductService(session).get_by_client_id(client_id, pagination) + return await ProductService(session).get_by_client_id(client_id, pagination, search_input) @product_router.get('/get-by-id', @@ -150,7 +151,6 @@ async def get_product_barcode_pdf( ) - @product_router.post( '/images/upload/{product_id}', response_model=ProductUploadImageResponse, diff --git a/services/product.py b/services/product.py index 1c3873f..135feeb 100644 --- a/services/product.py +++ b/services/product.py @@ -1,8 +1,8 @@ from typing import Union from fastapi import HTTPException -from sqlalchemy import select, func, Integer, update -from sqlalchemy.orm import selectinload +from sqlalchemy import select, func, Integer, update, or_ +from sqlalchemy.orm import selectinload, Query import utils.barcodes from backend import config @@ -94,17 +94,41 @@ class ProductService(BaseService): await self.session.commit() return ProductUpdateResponse(ok=True, message='Товар успешно обновлен') - async def get_by_client_id(self, client_id: int, pagination: PaginationSchema) -> ProductGetResponse: + async def get_by_client_id( + self, + client_id: int, + pagination: PaginationSchema, + search_input: str + ) -> ProductGetResponse: + is_pagination_valid = is_valid_pagination(pagination) total_pages = 0 total_items = 0 - stmt = ( - select(Product) - .options(selectinload(Product.barcodes) - .noload(ProductBarcode.product)) - .where(Product.client_id == client_id) + stmt: Query = ( + select( + Product + ) + .options( + selectinload(Product.barcodes) + .noload(ProductBarcode.product) + ) + .where( + Product.client_id == client_id + ) .order_by(Product.id) ) + search_input = search_input.strip() + if search_input: + stmt = ( + stmt.where( + or_( + Product.name.ilike(f'%{search_input}%'), + Product.barcodes.any(ProductBarcode.barcode == search_input), + Product.article == search_input + ) + ) + ) + if is_pagination_valid: total_products_query = await self.session.execute( select( diff --git a/test.py b/test.py index 3bcfe44..693cb1a 100644 --- a/test.py +++ b/test.py @@ -11,30 +11,7 @@ from models import User, PaymentRecord async def main(): - session: AsyncSession = session_maker() - try: - deal_id = 133 - source_product_id = 253 - source_services_stmt = ( - select( - models.DealProductService - ) - .where( - models.DealProductService.product_id == source_product_id, - models.DealProductService.deal_id == deal_id, - ) - ) - result = (await session.scalars(source_services_stmt)).all() - services = [d.service for d in result] - for service in services: - print( - service.price_ranges - - ) - except Exception as e: - print(e) - await session.close() - + pass if __name__ == '__main__': loop = asyncio.get_event_loop()