diff --git a/API/hdx.py b/API/hdx.py index a4a3bd6f..f9a6c12c 100644 --- a/API/hdx.py +++ b/API/hdx.py @@ -21,8 +21,18 @@ async def create_hdx( request: Request, hdx_data: dict, user_data: AuthUser = Depends(staff_required) ): - hdx_instance = HDX() + """ + Create a new HDX entry. + + Args: + request (Request): The request object. + hdx_data (dict): Data for creating the HDX entry. + user_data (AuthUser): User authentication data. + Returns: + dict: Result of the HDX creation process. + """ + hdx_instance = HDX() return hdx_instance.create_hdx(hdx_data) @@ -34,23 +44,27 @@ async def read_hdx_list( skip: int = 0, limit: int = 10, ): + """ + Retrieve a list of HDX entries based on provided filters. + + Args: + request (Request): The request object. + skip (int): Number of entries to skip. + limit (int): Maximum number of entries to retrieve. + + Returns: + List[dict]: List of HDX entries. + """ hdx_instance = HDX() filters = {} for key, values in request.query_params.items(): if key not in ["skip", "limit"]: if key in ["iso_3", "queue", "meta", "hdx_upload", "cid"]: - # if isinstance(values, list): - # filters[f"{key} IN %s"] = tuple(values) - # continue filters[f"{key} = %s"] = values continue - # if isinstance(values, list): - # filters[f"dataset->>'{key}' IN %s"] = tuple(values) - # else: filters[f"dataset->>'{key}' = %s"] = values hdx_list = hdx_instance.get_hdx_list_with_filters(skip, limit, filters) - return hdx_list @@ -58,8 +72,20 @@ async def read_hdx_list( @limiter.limit(f"{RATE_LIMIT_PER_MIN}/minute") @version(1) async def read_hdx(request: Request, hdx_id: int): - hdx_instance = HDX() + """ + Retrieve a specific HDX entry by its ID. + + Args: + request (Request): The request object. + hdx_id (int): ID of the HDX entry to retrieve. + + Returns: + dict: Details of the requested HDX entry. + Raises: + HTTPException: If the HDX entry is not found. + """ + hdx_instance = HDX() hdx = hdx_instance.get_hdx_by_id(hdx_id) if hdx: return hdx @@ -75,8 +101,22 @@ async def update_hdx( hdx_data: dict, user_data: AuthUser = Depends(staff_required), ): - hdx_instance = HDX() + """ + 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 updating the HDX entry. + user_data (AuthUser): User authentication data. + Returns: + dict: Result of the HDX update process. + + 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") @@ -90,8 +130,21 @@ async def update_hdx( async def delete_hdx( request: Request, hdx_id: int, user_data: AuthUser = Depends(admin_required) ): - hdx_instance = HDX() + """ + Delete an existing HDX entry. + + Args: + request (Request): The request object. + hdx_id (int): ID of the HDX entry to delete. + user_data (AuthUser): User authentication data. + Returns: + dict: Result of the HDX deletion process. + + 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") diff --git a/src/app.py b/src/app.py index a9a76857..02639c59 100644 --- a/src/app.py +++ b/src/app.py @@ -1904,6 +1904,15 @@ def __init__(self) -> None: self.con, self.cur = self.d_b.connect() def create_hdx(self, hdx_data): + """ + Create a new HDX entry in the database. + + Args: + hdx_data (dict): Data for creating the HDX entry. + + Returns: + dict: Result of the HDX creation process. + """ insert_query = sql.SQL( """ INSERT INTO public.hdx (iso_3, hdx_upload, dataset, queue, meta, categories, geometry) @@ -1930,6 +1939,17 @@ def create_hdx(self, hdx_data): def get_hdx_list_with_filters( self, skip: int = 0, limit: int = 10, filters: dict = {} ): + """ + Retrieve a list of HDX entries based on provided filters. + + Args: + skip (int): Number of entries to skip. + limit (int): Maximum number of entries to retrieve. + filters (dict): Filtering criteria. + + Returns: + List[dict]: List of HDX entries. + """ filter_conditions = [] filter_values = [] @@ -1952,6 +1972,18 @@ def get_hdx_list_with_filters( 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. + + Args: + hdx_id (int): ID of the HDX entry to retrieve. + + Returns: + dict: Details of the requested HDX entry. + + Raises: + HTTPException: If the HDX entry is not found. + """ select_query = sql.SQL( """ SELECT ST_AsGeoJSON(c.*) FROM public.hdx @@ -1966,6 +1998,19 @@ def get_hdx_by_id(self, hdx_id: int): raise HTTPException(status_code=404, detail="Item not found") def update_hdx(self, hdx_id: int, hdx_data): + """ + Update an existing HDX entry in the database. + + Args: + hdx_id (int): ID of the HDX entry to update. + hdx_data (dict): Data for updating the HDX entry. + + Returns: + dict: Result of the HDX update process. + + Raises: + HTTPException: If the HDX entry is not found. + """ update_query = sql.SQL( """ UPDATE public.hdx @@ -1994,6 +2039,18 @@ def update_hdx(self, hdx_id: int, hdx_data): raise HTTPException(status_code=404, detail="Item not found") def delete_hdx(self, hdx_id: int): + """ + Delete an existing HDX entry from the database. + + Args: + hdx_id (int): ID of the HDX entry to delete. + + Returns: + dict: Result of the HDX deletion process. + + Raises: + HTTPException: If the HDX entry is not found. + """ delete_query = sql.SQL( """ DELETE FROM public.hdx