From 85a3478b61ba27a8e041d3d5d50c4e5a96313c37 Mon Sep 17 00:00:00 2001 From: albertkun Date: Wed, 24 Jan 2024 22:28:29 -0800 Subject: [PATCH 1/3] refactor: get_gtfs_route_stops_grouped to use async/await added error handling --- fastapi/app/crud.py | 12 +++++++++--- fastapi/app/main.py | 11 ++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/fastapi/app/crud.py b/fastapi/app/crud.py index 8a02d4b..3966da6 100644 --- a/fastapi/app/crud.py +++ b/fastapi/app/crud.py @@ -747,9 +747,15 @@ def get_gtfs_route_stops(db,route_code,daytype,agency_id): return result -def get_gtfs_route_stops_grouped(db,route_code,agency_id): - the_query = db.query(models.RouteStopsGrouped).filter(models.RouteStopsGrouped.route_code == route_code,models.RouteStopsGrouped.agency_id == agency_id).all() - return the_query +async def get_gtfs_route_stops_grouped(async_session: Session, route_code: str, agency_id: str): + data = await get_data_async( + async_session, + models.RouteStopsGrouped, + agency_id, + field_name="route_code", + field_value=route_code, + ) + return data # generic function to get the gtfs static data def get_gtfs_static_data(db, tablename,column_name,query,agency_id): aliased_table = aliased(tablename) diff --git a/fastapi/app/main.py b/fastapi/app/main.py index 71bd01f..f8f8fd7 100644 --- a/fastapi/app/main.py +++ b/fastapi/app/main.py @@ -715,9 +715,14 @@ async def populate_route_stops(agency_id: AgencyIdEnum,route_code:str, daytype: @app.get("/{agency_id}/route_stops_grouped/{route_code}",tags=["Static data"]) async def populate_route_stops_grouped(agency_id: AgencyIdEnum,route_code:str, db: Session = Depends(get_db)): - result = crud.get_gtfs_route_stops_grouped(db,route_code,agency_id.value) - json_compatible_item_data = jsonable_encoder(result[0]) - return JSONResponse(content=json_compatible_item_data) + result = await crud.get_gtfs_route_stops_grouped(db,route_code,agency_id.value) + if result: + data_dict = result[0].__dict__ + data_dict.pop('_sa_instance_state', None) + json_compatible_item_data = jsonable_encoder(data_dict) + return JSONResponse(content=json_compatible_item_data) + else: + return JSONResponse(content={"error": f"No data found for the given route code {route_code} and agency id {agency_id.value}"}, status_code=404) @app.get("/calendar_dates",tags=["Static data"]) async def get_calendar_dates_from_db(db: Session = Depends(get_db)): From f8c4ed4265401a067ac43c78b9e9c7a7ed73a890 Mon Sep 17 00:00:00 2001 From: albertkun Date: Wed, 24 Jan 2024 22:45:39 -0800 Subject: [PATCH 2/3] fefactor: route stops grouped endpoint in main.py --- fastapi/app/main.py | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/fastapi/app/main.py b/fastapi/app/main.py index f8f8fd7..c2ccb91 100644 --- a/fastapi/app/main.py +++ b/fastapi/app/main.py @@ -713,16 +713,24 @@ async def populate_route_stops(agency_id: AgencyIdEnum,route_code:str, daytype: json_compatible_item_data = jsonable_encoder(result) return JSONResponse(content=json_compatible_item_data) -@app.get("/{agency_id}/route_stops_grouped/{route_code}",tags=["Static data"]) -async def populate_route_stops_grouped(agency_id: AgencyIdEnum,route_code:str, db: Session = Depends(get_db)): - result = await crud.get_gtfs_route_stops_grouped(db,route_code,agency_id.value) - if result: - data_dict = result[0].__dict__ - data_dict.pop('_sa_instance_state', None) - json_compatible_item_data = jsonable_encoder(data_dict) - return JSONResponse(content=json_compatible_item_data) +@app.get("/{agency_id}/route_stops_grouped/{route_code}", tags=["Static data"]) +async def get_route_stops_grouped_by_route_code(agency_id: AgencyIdEnum, route_code: str, async_db: AsyncSession = Depends(get_async_db)): + """ + Get route stops grouped data by route code. + """ + model = models.RouteStopsGrouped + if route_code.lower() == 'all': + # Return all routes + result = await crud.get_all_data_async(async_db, model, agency_id.value) + elif route_code.lower() == 'list': + # Return a list of route codes + result = await crud.get_list_of_unique_values_async(async_db, model, 'route_code', agency_id.value) else: - return JSONResponse(content={"error": f"No data found for the given route code {route_code} and agency id {agency_id.value}"}, status_code=404) + # Return data for a specific route code + result = await crud.get_data_async(async_db, model, agency_id.value, 'route_code', route_code) + if result is None: + raise HTTPException(status_code=404, detail=f"Data not found for route code {route_code}") + return result @app.get("/calendar_dates",tags=["Static data"]) async def get_calendar_dates_from_db(db: Session = Depends(get_db)): From a7ab895a8bd2a70230a6077c0bb87cad9104b0fe Mon Sep 17 00:00:00 2001 From: albertkun Date: Wed, 24 Jan 2024 22:53:17 -0800 Subject: [PATCH 3/3] refactor: RouteStopsGrouped class to inherit from BaseModel --- fastapi/app/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/app/models.py b/fastapi/app/models.py index 461e2d8..bf98e6c 100644 --- a/fastapi/app/models.py +++ b/fastapi/app/models.py @@ -154,7 +154,7 @@ class RouteStops(Base): # longitude = Column(Float) agency_id = Column(String) -class RouteStopsGrouped(Base): +class RouteStopsGrouped(BaseModel): __tablename__ = "route_stops_grouped" route_code = Column(String,primary_key=True, index=True) payload = Column(JSON)