88router = APIRouter ()
99base_url = os .getenv ("BASE_URL" , "http://localhost:8000" )
1010
11- @router .get ("/prospects" )
12- def root () -> dict :
13- """GET /prospects endpoint."""
14- meta = make_meta ("success" , "Prospects endpoint" )
15- data = [
16- {"init" : f"{ base_url } /prospects/init" },
17- {"search" : f"{ base_url } /prospects/search/?query=karen" },
18- ]
19- return {"meta" : meta , "data" : data }
2011
21- # endpoint: /prospects/read
22- @router .get ("/prospects/read " )
23- def prospects_read (
12+ # Refactored GET /prospects endpoint to return paginated, filtered, and ordered results
13+ @router .get ("/prospects" )
14+ def get_prospects (
2415 page : int = Query (1 , ge = 1 , description = "Page number (1-based)" ),
2516 limit : int = Query (50 , ge = 1 , le = 500 , description = "Records per page (default 50, max 500)" )
2617) -> dict :
27- """Read and return paginated rows from the prospects table, excluding hidden ."""
18+ """Return paginated, filtered, and ordered prospects (flagged first, then alphabetical by first_name) ."""
2819 meta = make_meta ("success" , "Read paginated prospects" )
2920 conn_gen = get_db_connection ()
3021 conn = next (conn_gen )
@@ -34,7 +25,13 @@ def prospects_read(
3425 cur .execute ('SELECT COUNT(*) FROM prospects WHERE hide IS NOT TRUE;' )
3526 count_row = cur .fetchone () if cur .description is not None else None
3627 total = count_row [0 ] if count_row is not None else 0
37- cur .execute (f'SELECT * FROM prospects WHERE hide IS NOT TRUE OFFSET %s LIMIT %s;' , (offset , limit ))
28+ # Order: flagged first (flag DESC NULLS LAST), then first_name ASC
29+ cur .execute ('''
30+ SELECT * FROM prospects
31+ WHERE hide IS NOT TRUE
32+ ORDER BY COALESCE(flag, FALSE) DESC, first_name ASC
33+ OFFSET %s LIMIT %s;
34+ ''' , (offset , limit ))
3835 if cur .description is not None :
3936 columns = [desc [0 ] for desc in cur .description ]
4037 rows = cur .fetchall ()
@@ -61,6 +58,7 @@ def prospects_read(
6158
6259
6360
61+
6462# Schema for update
6563from pydantic import BaseModel
6664from typing import Optional
0 commit comments