From 43f4bb90b2df7ab394ad7f524a65e4e2d8b64ae9 Mon Sep 17 00:00:00 2001 From: KaylaBrady <31781298+KaylaBrady@users.noreply.github.com> Date: Thu, 7 Nov 2024 11:48:10 -0500 Subject: [PATCH] perf(ScheduleController): Only make requests async when more than 1 --- .../controllers/schedule_controller.ex | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/mobile_app_backend_web/controllers/schedule_controller.ex b/lib/mobile_app_backend_web/controllers/schedule_controller.ex index 09a825a..4bca2c2 100644 --- a/lib/mobile_app_backend_web/controllers/schedule_controller.ex +++ b/lib/mobile_app_backend_web/controllers/schedule_controller.ex @@ -14,10 +14,11 @@ defmodule MobileAppBackendWeb.ScheduleController do filters = Enum.map(stop_ids, &get_filter(&1, service_date)) - data = case filters do - [filter] -> fetch_schedules(filter) - filters -> fetch_schedules_parallel(filters) - end + data = + case filters do + [filter] -> fetch_schedules(filter) + filters -> fetch_schedules_parallel(filters) + end case data do :error -> @@ -93,12 +94,12 @@ defmodule MobileAppBackendWeb.ScheduleController do %{schedules: [MBTAV3API.Schedule.t()], trips: JsonApi.Object.trip_map()} | :error defp fetch_schedules(filter) do - with {:ok, %{data: schedules, included: %{trips: trips}}} <- - Repository.schedules(filter: filter, include: :trip, sort: {:departure_time, :asc}) do - %{schedules: schedules, trips: trips} - else - _ -> :error + case Repository.schedules(filter: filter, include: :trip, sort: {:departure_time, :asc}) do + {:ok, %{data: schedules, included: %{trips: trips}}} -> + %{schedules: schedules, trips: trips} + _ -> + :error end end end