feat: product search
This commit is contained in:
		@@ -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(
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user