feat: product search

This commit is contained in:
2024-08-24 05:20:49 +03:00
parent bfc4b4f25d
commit 2b79e0deba
3 changed files with 35 additions and 34 deletions

View File

@@ -67,9 +67,10 @@ async def delete_product(
async def get_product( async def get_product(
client_id: int, client_id: int,
pagination: Annotated[PaginationSchema, Depends(utils.dependecies.pagination_parameters)], pagination: Annotated[PaginationSchema, Depends(utils.dependecies.pagination_parameters)],
search_input: str,
session: Annotated[AsyncSession, Depends(get_session)] 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', @product_router.get('/get-by-id',
@@ -150,7 +151,6 @@ async def get_product_barcode_pdf(
) )
@product_router.post( @product_router.post(
'/images/upload/{product_id}', '/images/upload/{product_id}',
response_model=ProductUploadImageResponse, response_model=ProductUploadImageResponse,

View File

@@ -1,8 +1,8 @@
from typing import Union from typing import Union
from fastapi import HTTPException from fastapi import HTTPException
from sqlalchemy import select, func, Integer, update from sqlalchemy import select, func, Integer, update, or_
from sqlalchemy.orm import selectinload from sqlalchemy.orm import selectinload, Query
import utils.barcodes import utils.barcodes
from backend import config from backend import config
@@ -94,17 +94,41 @@ class ProductService(BaseService):
await self.session.commit() await self.session.commit()
return ProductUpdateResponse(ok=True, message='Товар успешно обновлен') 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) is_pagination_valid = is_valid_pagination(pagination)
total_pages = 0 total_pages = 0
total_items = 0 total_items = 0
stmt = ( stmt: Query = (
select(Product) select(
.options(selectinload(Product.barcodes) Product
.noload(ProductBarcode.product)) )
.where(Product.client_id == client_id) .options(
selectinload(Product.barcodes)
.noload(ProductBarcode.product)
)
.where(
Product.client_id == client_id
)
.order_by(Product.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: if is_pagination_valid:
total_products_query = await self.session.execute( total_products_query = await self.session.execute(
select( select(

25
test.py
View File

@@ -11,30 +11,7 @@ from models import User, PaymentRecord
async def main(): async def main():
session: AsyncSession = session_maker() pass
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()
if __name__ == '__main__': if __name__ == '__main__':
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()