Skip to content

Commit

Permalink
feat(ScheduleController): small schedules_for_stop
Browse files Browse the repository at this point in the history
reduce response size by removing data that's unused by the frontend
  • Loading branch information
thecristen committed Nov 6, 2023
1 parent f6b7e82 commit 673758b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
9 changes: 9 additions & 0 deletions apps/site/lib/site_web/controllers/schedule_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ defmodule SiteWeb.ScheduleController do
data
|> future_departures(conn, params)
|> omit_last_stop_departures(params)
|> trim_response()

case schedules do
[%Schedule{} | _] ->
Expand Down Expand Up @@ -102,4 +103,12 @@ defmodule SiteWeb.ScheduleController do
end

defp omit_last_stop_departures(schedules, _), do: schedules

defp trim_response(schedules) do
schedules
|> Enum.map(&Map.drop(&1, [:stop]))
|> Enum.map(fn %Schedule{route: route} = schedule ->
%Schedule{schedule | route: Map.take(route, [:id])}
end)
end
end
16 changes: 11 additions & 5 deletions apps/site/test/site_web/controllers/schedule_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,12 @@ defmodule SiteWeb.ScheduleControllerTest do

describe "schedules_for_stop/2" do
test "should return an array of schedules", %{conn: conn} do
with_mock(Schedules.Repo,
with_mock(Schedules.Repo, [:passthrough],
schedules_for_stop: fn
"TEST 1234", [] ->
[
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2219-05-18 22:25:06.098765Z]
}
Expand All @@ -324,24 +325,27 @@ defmodule SiteWeb.ScheduleControllerTest do
conn = ScheduleController.schedules_for_stop(conn, %{"stop_id" => "TEST 1234"})
body = json_response(conn, 200)
assert Kernel.length(body) == 1
assert %{"stop" => %{"id" => "TEST 1234"}} = Enum.at(body, 0)
assert %{"departure_time" => "2219-05-18T22:25:06.098765Z"} = Enum.at(body, 0)
end
end

test "should not return past schedules", %{conn: conn} do
with_mock(Schedules.Repo,
with_mock(Schedules.Repo, [:passthrough],
schedules_for_stop: fn
"TEST 1234", [] ->
[
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2019-05-18 21:25:06.098765Z]
},
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2219-05-18 22:25:06.098765Z]
},
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2219-05-18 23:25:06.098765Z]
}
Expand All @@ -359,27 +363,29 @@ defmodule SiteWeb.ScheduleControllerTest do

body = json_response(conn, 200)
assert Kernel.length(body) == 2
assert %{"stop" => %{"id" => "TEST 1234"}} = Enum.at(body, 0)
assert %{"departure_time" => "2219-05-18T22:25:06.098765Z"} = Enum.at(body, 0)
end
end

test "should not return schedules that are the last stop on its route", %{conn: conn} do
with_mock(Schedules.Repo,
with_mock(Schedules.Repo, [:passthrough],
schedules_for_stop: fn
"TEST 1234", [] ->
[
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2219-05-18 22:25:06.098765Z],
last_stop?: false
},
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2219-05-18 22:25:06.098765Z],
last_stop?: false
},
%Schedules.Schedule{
route: %Routes.Route{id: "route"},
stop: %Stops.Stop{id: "TEST 1234"},
departure_time: ~U[2219-05-18 22:25:06.098765Z],
last_stop?: true
Expand Down

0 comments on commit 673758b

Please sign in to comment.