Skip to content

Commit

Permalink
Merge pull request #542 from LACMTA:dev
Browse files Browse the repository at this point in the history
Refactor get_route_overview function to group and sort results by route_type
  • Loading branch information
albertkun authored May 29, 2024
2 parents 7f479bb + 2ba843b commit aaac8f3
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions fastapi/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,10 +978,26 @@ async def get_route_overview(agency_id: AllAgencyIdEnum, async_db: AsyncSession
for agency in AllAgencyIdEnum:
result = await crud.get_all_data_async(async_db, model, agency.value)
if result is not None:
results.append(result)
results.extend(result)
if not results:
raise HTTPException(status_code=404, detail="Data not found")
return results

# Group results by route_type
grouped_results = {}
for result in results:
route_type = result.get('route_type')
if route_type not in grouped_results:
grouped_results[route_type] = []
grouped_results[route_type].append(result)

# Sort routes by display_order within each route_type
for route_type, routes in grouped_results.items():
grouped_results[route_type] = sorted(routes, key=lambda x: x.get('display_order', 0))

# Convert grouped_results to list of dictionaries
final_results = [{'route_type': k, 'routes': v} for k, v in grouped_results.items()]

return final_results
else:
result = await crud.get_all_data_async(async_db, model, agency_id.value)
if result is None:
Expand Down

0 comments on commit aaac8f3

Please sign in to comment.