From 91451eebc950be2738b7b95202b6726e82c76e7f Mon Sep 17 00:00:00 2001 From: tanshu Date: Fri, 2 Apr 2021 18:52:22 +0530 Subject: [PATCH] Feature: Guestbook phone autosearch also searches names. --- barker/barker/routers/customer.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/barker/barker/routers/customer.py b/barker/barker/routers/customer.py index 1fe2e84..3e8a6fe 100644 --- a/barker/barker/routers/customer.py +++ b/barker/barker/routers/customer.py @@ -5,6 +5,7 @@ from typing import List import barker.schemas.customer as schemas from fastapi import APIRouter, Depends, HTTPException, Security, status +from sqlalchemy import or_ from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session @@ -127,10 +128,13 @@ async def show_term( db: Session = Depends(get_db), current_user: UserToken = Depends(get_user), ) -> List[schemas.Customer]: - return [ - customer_info_for_list(item) - for item in db.query(Customer).filter(Customer.phone.ilike(f"%{q}%")).order_by(Customer.name).all() - ] + query = db.query(Customer) + if q is not None: + for item in q.split(): + query = query.filter(or_(Customer.name.ilike(f"%{item}%"), Customer.phone.ilike(f"%{item}%"))) + + query = query.order_by(Customer.name).all() + return [customer_info_for_list(item) for item in query] @router.get("/{id_}", response_model=schemas.Customer)