Skip to content

Commit bb97bd5

Browse files
committed
Add 's' search across name, description, categories
Refactor GET /orders to accept an aliased query param `s` (instead of `search`) and perform a case-insensitive partial match against name, description, and categories. Builds SQL WHERE clauses using LOWER(... ) LIKE %s, constructs a single lowered wildcard param and extends the query params accordingly. Also includes the provided search string in the response under `search.searchStr` and removes extraneous blank lines and an outdated comment about non-existent first_name/last_name columns.
1 parent c713e85 commit bb97bd5

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

app/api/orders/orders.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@
77
router = APIRouter()
88
base_url = os.getenv("BASE_URL", "http://localhost:8000")
99

10-
11-
12-
1310
# Refactored GET /orders endpoint to return paginated, filtered, and ordered results
1411
@router.get("/orders")
1512
def get_orders(
1613
page: int = Query(1, ge=1, description="Page number (1-based)"),
1714
limit: int = Query(100, ge=1, le=500, description="Records per page (default 100, max 500)"),
18-
search: str = Query(None, description="Search string (case-insensitive, partial match)"),
15+
s: str = Query(None, alias="s", description="Search string (case-insensitive, partial match)"),
1916
hideflagged: bool = Query(False, description="If true, flagged records are excluded")
2017
) -> dict:
2118
"""Return paginated, filtered, and ordered records, filtered by search if provided."""
@@ -30,7 +27,17 @@ def get_orders(
3027
params = []
3128
if hideflagged:
3229
where_clauses.append("flag IS NOT TRUE")
33-
# No first_name/last_name search, as those columns do not exist
30+
if s:
31+
# Search in name, description, or categories (case-insensitive, partial match)
32+
where_clauses.append("(" +
33+
" OR ".join([
34+
"LOWER(name) LIKE %s",
35+
"LOWER(description) LIKE %s",
36+
"LOWER(categories) LIKE %s"
37+
]) + ")"
38+
)
39+
search_param = f"%{s.lower()}%"
40+
params.extend([search_param, search_param, search_param])
3441
where_sql = " AND ".join(where_clauses)
3542

3643
# Count query
@@ -60,13 +67,17 @@ def get_orders(
6067
cur.close()
6168
conn.close()
6269
return {
70+
6371
"meta": meta,
6472
"pagination": {
6573
"page": page,
6674
"limit": limit,
6775
"total": total,
6876
"pages": (total // limit) + (1 if total % limit else 0)
6977
},
78+
"search": {
79+
"searchStr": s
80+
},
7081
"data": data,
7182
}
7283

0 commit comments

Comments
 (0)