Skip to content

Commit

Permalink
Merge pull request #455 from LACMTA:dev
Browse files Browse the repository at this point in the history
Refactor get_gtfs_route_stops_grouped to use async/await
  • Loading branch information
albertkun authored Jan 25, 2024
2 parents d961ebc + f7c6020 commit 87bf2d4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
12 changes: 9 additions & 3 deletions fastapi/app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 18 additions & 5 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +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 = 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)
@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 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)):
Expand Down
2 changes: 1 addition & 1 deletion fastapi/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 87bf2d4

Please sign in to comment.