Skip to content

Commit

Permalink
Enhance :> HDX endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Jan 29, 2024
1 parent f540d65 commit 3b27844
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 3 deletions.
62 changes: 61 additions & 1 deletion API/hdx.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,34 @@ async def read_hdx_list(
return hdx_list


@router.get("/search/", response_model=List[dict])
@limiter.limit(f"{RATE_LIMIT_PER_MIN}/minute")
@version(1)
async def search_hdx(
request: Request,
dataset_title: str = Query(
..., description="The title of the dataset to search for."
),
skip: int = Query(0, description="Number of entries to skip."),
limit: int = Query(10, description="Maximum number of entries to retrieve."),
):
"""
Search for HDX entries by dataset title.
Args:
request (Request): The request object.
dataset_title (str): The title of the dataset to search for.
skip (int): Number of entries to skip.
limit (int): Maximum number of entries to retrieve.
Returns:
List[dict]: List of HDX entries matching the dataset title.
"""
hdx_instance = HDX()
hdx_list = hdx_instance.search_hdx_by_dataset_title(dataset_title, skip, limit)
return hdx_list


@router.get("/{hdx_id}", response_model=dict)
@limiter.limit(f"{RATE_LIMIT_PER_MIN}/minute")
@version(1)
Expand Down Expand Up @@ -120,8 +148,40 @@ async def update_hdx(
existing_hdx = hdx_instance.get_hdx_by_id(hdx_id)
if not existing_hdx:
raise HTTPException(status_code=404, detail="HDX not found")
hdx_instance_update = HDX()
return hdx_instance_update.update_hdx(hdx_id, hdx_data)


@router.patch("/{hdx_id}", response_model=Dict)
@limiter.limit(f"{RATE_LIMIT_PER_MIN}/minute")
@version(1)
async def patch_hdx(
request: Request,
hdx_id: int,
hdx_data: Dict,
user_data: AuthUser = Depends(staff_required),
):
"""
Partially update an existing HDX entry.
Args:
request (Request): The request object.
hdx_id (int): ID of the HDX entry to update.
hdx_data (Dict): Data for partially updating the HDX entry.
user_data (AuthUser): User authentication data.
Returns:
Dict: Result of the HDX update process.
return hdx_instance.update_hdx(hdx_id, hdx_data)
Raises:
HTTPException: If the HDX entry is not found.
"""
hdx_instance = HDX()
existing_hdx = hdx_instance.get_hdx_by_id(hdx_id)
if not existing_hdx:
raise HTTPException(status_code=404, detail="HDX not found")
patch_instance = HDX()
return patch_instance.patch_hdx(hdx_id, hdx_data)


@router.delete("/{hdx_id}", response_model=dict)
Expand Down
69 changes: 67 additions & 2 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,32 @@ def get_hdx_list_with_filters(
self.d_b.close_conn()
return [orjson.loads(item[0]) for item in result]

def search_hdx_by_dataset_title(
self, dataset_title: str, skip: int = 0, limit: int = 10
):
"""
Search for HDX entries by dataset title.
Args:
dataset_title (str): The title of the dataset to search for.
skip (int): Number of entries to skip.
limit (int): Maximum number of entries to retrieve.
Returns:
List[dict]: List of HDX entries matching the dataset title.
"""
search_query = sql.SQL(
"""
SELECT ST_AsGeoJSON(c.*) FROM public.hdx c
WHERE c.dataset->>'dataset_title' ILIKE %s
OFFSET %s LIMIT %s
"""
)
self.cur.execute(search_query, ("%" + dataset_title + "%", skip, limit))
result = self.cur.fetchall()
self.d_b.close_conn()
return [orjson.loads(item[0]) for item in result]

def get_hdx_by_id(self, hdx_id: int):
"""
Retrieve a specific HDX entry by its ID.
Expand All @@ -1990,14 +2016,13 @@ def get_hdx_by_id(self, hdx_id: int):
"""
select_query = sql.SQL(
"""
SELECT ST_AsGeoJSON(c.*) FROM public.hdx
SELECT ST_AsGeoJSON(c.*) FROM public.hdx c
WHERE id = %s
"""
)
self.cur.execute(select_query, (hdx_id,))
result = self.cur.fetchone()
self.d_b.close_conn()
result = self.cur.fetchone()
if result:
return orjson.loads(result[0])
raise HTTPException(status_code=404, detail="Item not found")
Expand Down Expand Up @@ -2038,8 +2063,48 @@ def update_hdx(self, hdx_id: int, hdx_data):
),
)
self.con.commit()
result = self.cur.fetchone()
self.d_b.close_conn()
if result:
return {"update": True}
raise HTTPException(status_code=404, detail="Item not found")

def patch_hdx(self, hdx_id: int, hdx_data: dict):
"""
Partially update an existing HDX entry in the database.
Args:
hdx_id (int): ID of the HDX entry to update.
hdx_data (dict): Data for partially updating the HDX entry.
Returns:
dict: Result of the HDX update process.
Raises:
HTTPException: If the HDX entry is not found.
"""
if not hdx_data:
raise ValueError("No data provided for update")

set_clauses = []
params = []
for field, value in hdx_data.items():
set_clauses.append(sql.SQL("{} = %s").format(sql.Identifier(field)))
if isinstance(value, dict):
params.append(json.dumps(value))
else:
params.append(value)

query = sql.SQL("UPDATE public.hdx SET {} WHERE id = %s RETURNING *").format(
sql.SQL(", ").join(set_clauses)
)
params.append(hdx_id)

self.cur.execute(query, tuple(params))
self.con.commit()
result = self.cur.fetchone()
self.d_b.close_conn()

if result:
return {"update": True}
raise HTTPException(status_code=404, detail="Item not found")
Expand Down

0 comments on commit 3b27844

Please sign in to comment.