Skip to content

Commit abaedb4

Browse files
Merge pull request #39 from goldlabelapps/staging
Add flag/hide columns and PATCH endpoint
2 parents c20f291 + e4ca286 commit abaedb4

4 files changed

Lines changed: 79 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__ = "2.0.1"
4+
__version__ = "2.0.2"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Migration: Add flag and hide columns to prospects table
2+
ALTER TABLE prospects ADD COLUMN IF NOT EXISTS flag BOOLEAN DEFAULT FALSE;
3+
ALTER TABLE prospects ADD COLUMN IF NOT EXISTS hide BOOLEAN DEFAULT FALSE;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Run SQL migration to add 'flag' and 'hide' columns to prospects table
2+
from app.utils.db import get_db_connection
3+
4+
SQL_PATH = "app/api/prospects/database/alter_add_flag_hide.sql"
5+
6+
def run_migration():
7+
with open(SQL_PATH, "r") as f:
8+
sql = f.read()
9+
conn_gen = get_db_connection()
10+
conn = next(conn_gen)
11+
cur = conn.cursor()
12+
try:
13+
cur.execute(sql)
14+
conn.commit()
15+
print("Migration successful: 'flag' and 'hide' columns added.")
16+
except Exception as e:
17+
print(f"Migration failed: {e}")
18+
conn.rollback()
19+
finally:
20+
cur.close()
21+
conn.close()
22+
23+
if __name__ == "__main__":
24+
run_migration()

app/api/prospects/prospects.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from app import __version__
33
import os
44
from app.utils.make_meta import make_meta
5-
from fastapi import APIRouter, Query, Path
5+
from fastapi import APIRouter, Query, Path, Body, HTTPException
66
from app.utils.db import get_db_connection
77

88
router = APIRouter()
@@ -61,6 +61,13 @@ def prospects_read(
6161

6262
from typing import Optional
6363

64+
# Schema for update
65+
from pydantic import BaseModel
66+
67+
class ProspectUpdate(BaseModel):
68+
flag: Optional[bool] = None
69+
hide: Optional[bool] = None
70+
6471
# endpoint: /prospects/search
6572
@router.get("/prospects/search")
6673
def prospects_search(query: Optional[str] = Query(None, description="Search query string"),
@@ -194,6 +201,7 @@ def slugify(text):
194201
return {"meta": meta, "data": data}
195202

196203

204+
# endpoint: /prospects/{id}
197205
# endpoint: /prospects/{id}
198206
@router.get("/prospects/{id}")
199207
def prospects_read_one(id: int = Path(..., description="ID of the prospect to retrieve")) -> dict:
@@ -221,4 +229,46 @@ def prospects_read_one(id: int = Path(..., description="ID of the prospect to re
221229
finally:
222230
cur.close()
223231
conn.close()
232+
return {"meta": meta, "data": data}
233+
234+
235+
# PATCH endpoint to update flag/hide
236+
@router.patch("/prospects/{id}")
237+
def update_prospect(id: int = Path(..., description="ID of the prospect to update"), update: ProspectUpdate = Body(...)) -> dict:
238+
"""Update flag and/or hide fields for a prospect by id."""
239+
meta = make_meta("success", f"Updated prospect with id {id}")
240+
conn_gen = get_db_connection()
241+
conn = next(conn_gen)
242+
cur = conn.cursor()
243+
fields = []
244+
values = []
245+
if update.flag is not None:
246+
fields.append("flag = %s")
247+
values.append(update.flag)
248+
if update.hide is not None:
249+
fields.append("hide = %s")
250+
values.append(update.hide)
251+
if not fields:
252+
raise HTTPException(status_code=400, detail="No fields to update.")
253+
values.append(id)
254+
try:
255+
cur.execute(f"UPDATE prospects SET {', '.join(fields)} WHERE id = %s RETURNING *;", tuple(values))
256+
if cur.description is not None:
257+
row = cur.fetchone()
258+
if row is not None:
259+
columns = [desc[0] for desc in cur.description]
260+
data = dict(zip(columns, row))
261+
else:
262+
data = None
263+
meta = make_meta("error", f"No prospect found with id {id}")
264+
else:
265+
data = None
266+
meta = make_meta("error", f"No prospect found with id {id}")
267+
conn.commit()
268+
except Exception as e:
269+
data = None
270+
meta = make_meta("error", f"Failed to update prospect: {str(e)}")
271+
finally:
272+
cur.close()
273+
conn.close()
224274
return {"meta": meta, "data": data}

0 commit comments

Comments
 (0)