Skip to content

Commit d11958d

Browse files
committed
Add /prospects/unique endpoint; bump version
Bump package version to 1.1.3 and add a new GET endpoint /prospects/unique that accepts a list of field names (Query param) and returns unique values and counts for each specified field from the prospects table. The handler queries the DB, aggregates counts, returns a meta object, data per field, and any field-specific errors, and ensures DB cursor/connection are closed. Also add a trailing newline in routes.py for consistency. Note: field names are interpolated into SQL for grouping; consider validating/sanitizing field names to avoid SQL issues.
1 parent 14c2187 commit d11958d

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""NX AI - FastAPI/Python/Postgres/tsvector"""
22

33
# Current Version
4-
__version__ = "1.1.2"
4+
__version__ = "1.1.3"

app/api/prospects/prospects.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,34 @@
66

77
router = APIRouter()
88

9+
10+
# Endpoint to get unique values for specified fields
11+
from fastapi import Query
12+
13+
@router.get("/prospects/unique")
14+
def get_unique_fields(fields: list[str] = Query(..., description="List of field names to get unique values for")) -> dict:
15+
"""Return lists of unique values and their counts for specified fields in the prospects table."""
16+
conn_gen = get_db_connection()
17+
conn = next(conn_gen)
18+
cur = conn.cursor()
19+
result = {}
20+
errors = {}
21+
try:
22+
for field in fields:
23+
try:
24+
cur.execute(f'SELECT "{field}", COUNT(*) FROM prospects WHERE "{field}" IS NOT NULL GROUP BY "{field}" ORDER BY COUNT(*) DESC;')
25+
values = [
26+
{"value": row[0], "count": row[1]} for row in cur.fetchall()
27+
]
28+
result[field] = values
29+
except Exception as e:
30+
errors[field] = str(e)
31+
meta = make_meta("success", f"Unique values and counts for fields: {fields}")
32+
return {"meta": meta, "data": result, "errors": errors if errors else None}
33+
finally:
34+
cur.close()
35+
conn.close()
36+
937
@router.get("/prospects")
1038
def root() -> dict:
1139
"""Return all prospects table records"""

app/api/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@
2525
router.include_router(prospects_router)
2626
router.include_router(prospects_seed_router)
2727
router.include_router(prospects_empty_router)
28-
router.include_router(prospects_process_router)
28+
router.include_router(prospects_process_router)

0 commit comments

Comments
 (0)