22from app import __version__
33import os
44from app .utils .make_meta import make_meta
5- from fastapi import APIRouter , Query , Path
5+ from fastapi import APIRouter , Query , Path , Body , HTTPException
66from app .utils .db import get_db_connection
77
88router = APIRouter ()
@@ -61,6 +61,13 @@ def prospects_read(
6161
6262from 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" )
6673def 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}" )
199207def 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