fix:优化数据
This commit is contained in:
@@ -16,9 +16,18 @@ router = APIRouter(prefix="/customers", tags=["customers"])
|
||||
|
||||
|
||||
@router.get("/", response_model=List[CustomerRead])
|
||||
async def list_customers(db: Session = Depends(get_db)):
|
||||
customers = db.query(models.Customer).order_by(models.Customer.created_at.desc()).all()
|
||||
return customers
|
||||
async def list_customers(
|
||||
q: str | None = None,
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
"""列表客户,支持 q 按名称、联系方式模糊搜索。"""
|
||||
query = db.query(models.Customer).order_by(models.Customer.created_at.desc())
|
||||
if q and q.strip():
|
||||
term = f"%{q.strip()}%"
|
||||
query = query.filter(
|
||||
(models.Customer.name.ilike(term)) | (models.Customer.contact_info.ilike(term))
|
||||
)
|
||||
return query.all()
|
||||
|
||||
|
||||
@router.post("/", response_model=CustomerRead, status_code=status.HTTP_201_CREATED)
|
||||
@@ -26,6 +35,7 @@ async def create_customer(payload: CustomerCreate, db: Session = Depends(get_db)
|
||||
customer = models.Customer(
|
||||
name=payload.name,
|
||||
contact_info=payload.contact_info,
|
||||
tags=payload.tags,
|
||||
)
|
||||
db.add(customer)
|
||||
db.commit()
|
||||
@@ -53,6 +63,8 @@ async def update_customer(
|
||||
customer.name = payload.name
|
||||
if payload.contact_info is not None:
|
||||
customer.contact_info = payload.contact_info
|
||||
if payload.tags is not None:
|
||||
customer.tags = payload.tags
|
||||
|
||||
db.commit()
|
||||
db.refresh(customer)
|
||||
|
||||
Reference in New Issue
Block a user