From 8586aeaf713d4e97c40ec30d8d459447c8c57809 Mon Sep 17 00:00:00 2001 From: Brian Fauble Date: Tue, 17 Oct 2023 10:10:37 -0600 Subject: [PATCH 1/5] add date filter for route_pattern requests --- .../controllers/route_pattern_controller.ex | 66 +++++++-- .../route_pattern_controller_test.exs | 129 ++++++++++++++++++ 2 files changed, 186 insertions(+), 9 deletions(-) diff --git a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex index a85752cc..d78e6531 100644 --- a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex +++ b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex @@ -1,16 +1,20 @@ defmodule ApiWeb.RoutePatternController do @moduledoc """ - Controller for Routes. Filterable by: + Controller for Route Patterns. Filterable by: * id + * date + * direction_id * route_id + * stop + * canonical """ use ApiWeb.Web, :api_controller - alias State.RoutePattern + alias State.{RoutePattern, RoutesByService, ServiceByDate} plug(:ensure_path_matches_version) - @filters ~w(id canonical route direction_id stop) + @filters ~w(id canonical route direction_id stop date) @includes ~w(route representative_trip) @pagination_opts [:offset, :limit, :order_by] @description """ @@ -48,6 +52,7 @@ defmodule ApiWeb.RoutePatternController do filter_param(:direction_id) filter_param(:stop_id, includes_children: true) filter_param(:canonical) + filter_param(:date, description: "Filter by date that route pattern is active") consumes("application/vnd.api+json") produces("application/vnd.api+json") @@ -93,24 +98,67 @@ defmodule ApiWeb.RoutePatternController do @spec format_filters(%{optional(String.t()) => String.t()}) :: RoutePattern.filters() defp format_filters(filters) do - Map.new(filters, fn {key, value} -> + filters + |> Enum.flat_map(fn {key, value} -> case {key, value} do {"id", ids} -> - {:ids, Params.split_on_comma(ids)} + %{ids: Params.split_on_comma(ids)} {"route", route_ids} -> - {:route_ids, Params.split_on_comma(route_ids)} + %{route_ids: Params.split_on_comma(route_ids)} {"direction_id", direction_id} -> - {:direction_id, Params.direction_id(%{"direction_id" => direction_id})} + %{direction_id: Params.direction_id(%{"direction_id" => direction_id})} {"stop", stop_ids} -> - {:stop_ids, Params.split_on_comma(stop_ids)} + %{stop_ids: Params.split_on_comma(stop_ids)} {"canonical", canonical} -> - {:canonical, Params.canonical(canonical)} + %{canonical: Params.canonical(canonical)} + + {"date", date} -> + process_date(date) end end) + |> Enum.into(%{}) + |> consolidate_route_ids() + end + + defp consolidate_route_ids( + %{ + route_ids: [_ | _] = route_ids, + date_specific_route_ids: [_ | _] = date_specific_route_ids + } = params + ) do + filtered_route_ids = + Enum.filter(route_ids, fn id -> Enum.member?(date_specific_route_ids, id) end) + + params + |> Map.put(:route_ids, filtered_route_ids) + |> Map.delete(:date_specific_route_ids) + end + + defp consolidate_route_ids(%{date_specific_route_ids: date_specific_route_ids} = params) do + params + |> Map.put(:route_ids, date_specific_route_ids) + |> Map.delete(:date_specific_route_ids) + end + + defp consolidate_route_ids(params), do: params + + defp process_date(date_str) do + case Date.from_iso8601(date_str) do + {:ok, date} -> + ids = + date + |> ServiceByDate.by_date() + |> RoutesByService.for_service_ids() + + %{date_specific_route_ids: ids} + + {:error, _} -> + [] + end end defp pagination_opts(params, conn) do diff --git a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs index 9d808a50..e8d3888f 100644 --- a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs @@ -479,6 +479,135 @@ defmodule ApiWeb.RoutePatternControllerTest do assert [%{"sort_order" => 1}, %{"sort_order" => 2}, %{"sort_order" => 3}] = attrs end + + test "can filter by date", %{conn: base_conn} do + today = Parse.Time.service_date() + bad_date = ~D[2017-01-01] + + service = %Model.Service{ + id: "service", + start_date: today, + end_date: today, + added_dates: [today] + } + + route = %Model.Route{id: "route"} + route_pattern = %Model.RoutePattern{id: "rp1", route_id: "route"} + + trip = %Model.Trip{ + id: "trip", + route_id: route.id, + service_id: service.id, + route_pattern_id: route_pattern.id + } + + State.Service.new_state([service]) + State.Route.new_state([route]) + State.RoutePattern.new_state([route_pattern]) + State.Trip.new_state([trip]) + State.Trip.reset_gather() + State.RoutesByService.update!() + + today_iso = Date.to_iso8601(today) + bad_date_iso = Date.to_iso8601(bad_date) + + params = %{"filter" => %{"date" => today_iso}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern] == data + + params = put_in(params["filter"]["date"], bad_date_iso) + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [] == data + + params = %{"filter" => %{"date" => ""}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern] == data + + params = %{"filter" => %{"date" => "invalid"}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern] == data + end + + test "can filter by date and route(s)", %{conn: base_conn} do + today = Parse.Time.service_date() + bad_date = ~D[2027-01-01] + + service = %Model.Service{ + id: "service", + start_date: today, + end_date: today, + added_dates: [today] + } + + future_service = %Model.Service{ + id: "future_service", + start_date: bad_date, + end_date: bad_date, + added_dates: [bad_date] + } + + route1 = %Model.Route{id: "route1"} + route2 = %Model.Route{id: "route2"} + route3 = %Model.Route{id: "route3"} + route_pattern1 = %Model.RoutePattern{id: "rp1", route_id: route1.id} + route_pattern2 = %Model.RoutePattern{id: "rp2", route_id: route2.id} + route_pattern3 = %Model.RoutePattern{id: "rp3", route_id: route3.id} + + trip1 = %Model.Trip{ + id: "trip1", + route_id: route1.id, + service_id: service.id, + route_pattern_id: route_pattern1.id + } + + trip2 = %Model.Trip{ + id: "trip2", + route_id: route2.id, + service_id: future_service.id, + route_pattern_id: route_pattern2.id + } + + trip3 = %Model.Trip{ + id: "trip3", + route_id: route3.id, + service_id: service.id, + route_pattern_id: route_pattern3.id + } + + State.Service.new_state([service, future_service]) + State.Route.new_state([route1, route2, route3]) + State.RoutePattern.new_state([route_pattern1, route_pattern2, route_pattern3]) + State.Trip.new_state([trip1, trip2, trip3]) + State.Trip.reset_gather() + State.RoutesByService.update!() + + today_iso = Date.to_iso8601(today) + bad_date_iso = Date.to_iso8601(bad_date) + + params = %{"filter" => %{"date" => today_iso}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern1, route_pattern3] == data + + params = put_in(params["filter"]["route"], "route1,route2") + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern1] == data + + params = put_in(params["filter"]["route"], "route3,route4,Red,Blue") + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern3] == data + + params = %{"filter" => %{"date" => bad_date_iso, "route" => "route1,route2,route3"}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern2] == data + + params = %{"filter" => %{"date" => today_iso, "route" => ""}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern1, route_pattern3] == data + + params = %{"filter" => %{"date" => today_iso, "route" => nil}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern1, route_pattern3] == data + end end describe "show" do From 2c8dbd5efd1603098fb08e38304eaa1542c8ad4d Mon Sep 17 00:00:00 2001 From: Brian Fauble Date: Tue, 17 Oct 2023 12:01:28 -0600 Subject: [PATCH 2/5] update logic to account for direction_id and canonical --- .../controllers/route_pattern_controller.ex | 27 +++++- .../route_pattern_controller_test.exs | 86 +++++++++++++++++-- apps/state/lib/state/route_pattern.ex | 10 +-- 3 files changed, 107 insertions(+), 16 deletions(-) diff --git a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex index d78e6531..9f2ed559 100644 --- a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex +++ b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex @@ -10,7 +10,7 @@ defmodule ApiWeb.RoutePatternController do * canonical """ use ApiWeb.Web, :api_controller - alias State.{RoutePattern, RoutesByService, ServiceByDate} + alias State.{RoutePattern, RoutesByService, ServiceByDate, Trip} plug(:ensure_path_matches_version) @@ -122,6 +122,7 @@ defmodule ApiWeb.RoutePatternController do end) |> Enum.into(%{}) |> consolidate_route_ids() + |> gather_trip_based_route_pattern_ids() end defp consolidate_route_ids( @@ -146,6 +147,28 @@ defmodule ApiWeb.RoutePatternController do defp consolidate_route_ids(params), do: params + defp gather_trip_based_route_pattern_ids( + %{date: _date, route_ids: [_ | _] = route_ids} = params + ) do + route_pattern_ids = + params + |> Map.take([:date, :direction_id]) + |> Map.put(:routes, route_ids) + |> Trip.filter_by() + |> Enum.map(& &1.route_pattern_id) + |> consolidate_route_pattern_ids(params) + + Map.put(params, :ids, route_pattern_ids) + end + + defp gather_trip_based_route_pattern_ids(params), do: params + + defp consolidate_route_pattern_ids(route_pattern_ids, %{ids: [_ | _] = ids}) do + Enum.filter(route_pattern_ids, fn rid -> Enum.member?(ids, rid) end) + end + + defp consolidate_route_pattern_ids(route_pattern_ids, _), do: route_pattern_ids + defp process_date(date_str) do case Date.from_iso8601(date_str) do {:ok, date} -> @@ -154,7 +177,7 @@ defmodule ApiWeb.RoutePatternController do |> ServiceByDate.by_date() |> RoutesByService.for_service_ids() - %{date_specific_route_ids: ids} + %{date: date, date_specific_route_ids: ids} {:error, _} -> [] diff --git a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs index e8d3888f..6f9843e0 100644 --- a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs @@ -549,9 +549,23 @@ defmodule ApiWeb.RoutePatternControllerTest do route1 = %Model.Route{id: "route1"} route2 = %Model.Route{id: "route2"} route3 = %Model.Route{id: "route3"} - route_pattern1 = %Model.RoutePattern{id: "rp1", route_id: route1.id} - route_pattern2 = %Model.RoutePattern{id: "rp2", route_id: route2.id} - route_pattern3 = %Model.RoutePattern{id: "rp3", route_id: route3.id} + route_pattern1 = %Model.RoutePattern{id: "rp1", route_id: route1.id, canonical: false} + route_pattern2 = %Model.RoutePattern{id: "rp2", route_id: route2.id, canonical: false} + route_pattern3 = %Model.RoutePattern{id: "rp3", route_id: route3.id, canonical: true} + + route_pattern4 = %Model.RoutePattern{ + id: "rp4", + route_id: route3.id, + canonical: false, + direction_id: 0 + } + + route_pattern5 = %Model.RoutePattern{ + id: "rp5", + route_id: route3.id, + canonical: false, + direction_id: 1 + } trip1 = %Model.Trip{ id: "trip1", @@ -574,21 +588,53 @@ defmodule ApiWeb.RoutePatternControllerTest do route_pattern_id: route_pattern3.id } + trip4 = %Model.Trip{ + id: "trip4", + route_id: route3.id, + direction_id: 0, + service_id: future_service.id, + route_pattern_id: route_pattern4.id + } + + trip5 = %Model.Trip{ + id: "trip5", + route_id: route3.id, + direction_id: 1, + service_id: future_service.id, + route_pattern_id: route_pattern5.id + } + State.Service.new_state([service, future_service]) State.Route.new_state([route1, route2, route3]) - State.RoutePattern.new_state([route_pattern1, route_pattern2, route_pattern3]) - State.Trip.new_state([trip1, trip2, trip3]) + + State.RoutePattern.new_state([ + route_pattern1, + route_pattern2, + route_pattern3, + route_pattern4, + route_pattern5 + ]) + + State.Trip.new_state([trip1, trip2, trip3, trip4, trip5]) State.Trip.reset_gather() State.RoutesByService.update!() today_iso = Date.to_iso8601(today) - bad_date_iso = Date.to_iso8601(bad_date) + future_date_iso = Date.to_iso8601(bad_date) params = %{"filter" => %{"date" => today_iso}} data = ApiWeb.RoutePatternController.index_data(base_conn, params) assert [route_pattern1, route_pattern3] == data - params = put_in(params["filter"]["route"], "route1,route2") + params = %{"filter" => %{"date" => today_iso, "canonical" => "false"}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern1] == data + + params = %{"filter" => %{"date" => today_iso, "canonical" => "true"}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern3] == data + + params = %{"filter" => %{"date" => today_iso, "route" => "route1,route2"}} data = ApiWeb.RoutePatternController.index_data(base_conn, params) assert [route_pattern1] == data @@ -596,9 +642,31 @@ defmodule ApiWeb.RoutePatternControllerTest do data = ApiWeb.RoutePatternController.index_data(base_conn, params) assert [route_pattern3] == data - params = %{"filter" => %{"date" => bad_date_iso, "route" => "route1,route2,route3"}} + params = %{"filter" => %{"date" => future_date_iso, "route" => "route1,route2,route3"}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern2, route_pattern4, route_pattern5] == data + + params = %{ + "filter" => %{ + "date" => future_date_iso, + "route" => "route1,route2,route3", + "direction_id" => "0" + } + } + + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern4] == data + + params = %{ + "filter" => %{ + "date" => future_date_iso, + "route" => "route1,route2,route3", + "direction_id" => "1" + } + } + data = ApiWeb.RoutePatternController.index_data(base_conn, params) - assert [route_pattern2] == data + assert [route_pattern5] == data params = %{"filter" => %{"date" => today_iso, "route" => ""}} data = ApiWeb.RoutePatternController.index_data(base_conn, params) diff --git a/apps/state/lib/state/route_pattern.ex b/apps/state/lib/state/route_pattern.ex index 089b227c..eb7de6cb 100644 --- a/apps/state/lib/state/route_pattern.ex +++ b/apps/state/lib/state/route_pattern.ex @@ -29,11 +29,6 @@ defmodule State.RoutePattern do end end - @spec filter_by(filters()) :: [RoutePattern.t()] - def filter_by(%{ids: ids}) do - by_ids(ids) - end - def filter_by(%{canonical: canonical} = filters) do filters |> Map.delete(:canonical) @@ -41,6 +36,11 @@ defmodule State.RoutePattern do |> Enum.filter(fn %RoutePattern{canonical: is_c} -> canonical == is_c end) end + @spec filter_by(filters()) :: [RoutePattern.t()] + def filter_by(%{ids: ids}) do + by_ids(ids) + end + def filter_by(%{route_ids: _route_ids, stop_ids: _stop_ids} = filters) do ids_from_stops = ids_from_stops(filters) ids_from_routes = ids_from_routes(filters) From 931cbdaf04f5e9779631f4f353ad3615a39ed81a Mon Sep 17 00:00:00 2001 From: Brian Fauble Date: Wed, 18 Oct 2023 12:45:05 -0600 Subject: [PATCH 3/5] update format_filters pattern to process dependent filters for following filters rather than after all have been partially processed --- .../controllers/route_pattern_controller.ex | 138 +++++++++++------- .../route_pattern_controller_test.exs | 4 +- 2 files changed, 88 insertions(+), 54 deletions(-) diff --git a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex index 9f2ed559..e45396ce 100644 --- a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex +++ b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex @@ -97,78 +97,112 @@ defmodule ApiWeb.RoutePatternController do defp reject_invalid_canonical_filter(filters), do: filters @spec format_filters(%{optional(String.t()) => String.t()}) :: RoutePattern.filters() - defp format_filters(filters) do + defp format_filters(filters, acc \\ %{}) + + defp format_filters(%{"id" => ids} = filters, acc) do + new_acc = Map.put(acc, :ids, Params.split_on_comma(ids)) + + filters + |> Map.delete("id") + |> format_filters(new_acc) + end + + defp format_filters(%{"direction_id" => direction_id} = filters, acc) do + new_acc = Map.put(acc, :direction_id, Params.direction_id(%{"direction_id" => direction_id})) + + filters + |> Map.delete("direction_id") + |> format_filters(new_acc) + end + + defp format_filters(%{"route" => route_ids} = filters, acc) do + new_acc = Map.put(acc, :route_ids, Params.split_on_comma(route_ids)) + + filters + |> Map.delete("route") + |> format_filters(new_acc) + end + + defp format_filters(%{"date" => date} = filters, acc) do + case process_date(date, acc) do + :error -> + filters + |> Map.delete("date") + |> format_filters(acc) + + new_acc -> + filters + |> Map.delete("date") + |> format_filters(new_acc) + end + end + + defp format_filters(%{"stop" => stop_ids} = filters, acc) do + new_acc = Map.put(acc, :stop_ids, Params.split_on_comma(stop_ids)) + filters - |> Enum.flat_map(fn {key, value} -> - case {key, value} do - {"id", ids} -> - %{ids: Params.split_on_comma(ids)} - - {"route", route_ids} -> - %{route_ids: Params.split_on_comma(route_ids)} - - {"direction_id", direction_id} -> - %{direction_id: Params.direction_id(%{"direction_id" => direction_id})} - - {"stop", stop_ids} -> - %{stop_ids: Params.split_on_comma(stop_ids)} - - {"canonical", canonical} -> - %{canonical: Params.canonical(canonical)} - - {"date", date} -> - process_date(date) - end - end) - |> Enum.into(%{}) - |> consolidate_route_ids() - |> gather_trip_based_route_pattern_ids() + |> Map.delete("stop") + |> format_filters(new_acc) end - defp consolidate_route_ids( - %{ - route_ids: [_ | _] = route_ids, - date_specific_route_ids: [_ | _] = date_specific_route_ids - } = params - ) do - filtered_route_ids = - Enum.filter(route_ids, fn id -> Enum.member?(date_specific_route_ids, id) end) - - params - |> Map.put(:route_ids, filtered_route_ids) - |> Map.delete(:date_specific_route_ids) + defp format_filters(%{"canonical" => canonical} = filters, acc) do + new_acc = Map.put(acc, :canonical, Params.canonical(canonical)) + + filters + |> Map.delete("canonical") + |> format_filters(new_acc) end - defp consolidate_route_ids(%{date_specific_route_ids: date_specific_route_ids} = params) do - params - |> Map.put(:route_ids, date_specific_route_ids) - |> Map.delete(:date_specific_route_ids) + defp format_filters(_, acc) do + acc end - defp consolidate_route_ids(params), do: params + defp consolidate_route_ids(%{route_ids: [_ | _] = route_ids}, [_ | _] = date_specific_route_ids) do + route_ids + |> MapSet.new() + |> MapSet.intersection(MapSet.new(date_specific_route_ids)) + |> MapSet.to_list() + end - defp gather_trip_based_route_pattern_ids( - %{date: _date, route_ids: [_ | _] = route_ids} = params - ) do + defp consolidate_route_ids(_, date_specific_route_ids), do: date_specific_route_ids + + defp gather_trip_based_route_pattern_ids(%{date: _date, route_ids: [_ | _] = route_ids} = acc) do route_pattern_ids = - params + acc |> Map.take([:date, :direction_id]) |> Map.put(:routes, route_ids) |> Trip.filter_by() |> Enum.map(& &1.route_pattern_id) - |> consolidate_route_pattern_ids(params) + |> consolidate_route_pattern_ids(acc) - Map.put(params, :ids, route_pattern_ids) + Map.put(acc, :ids, route_pattern_ids) end - defp gather_trip_based_route_pattern_ids(params), do: params + defp gather_trip_based_route_pattern_ids(acc), do: acc defp consolidate_route_pattern_ids(route_pattern_ids, %{ids: [_ | _] = ids}) do - Enum.filter(route_pattern_ids, fn rid -> Enum.member?(ids, rid) end) + route_pattern_ids + |> MapSet.new() + |> MapSet.intersection(MapSet.new(ids)) + |> MapSet.to_list() end defp consolidate_route_pattern_ids(route_pattern_ids, _), do: route_pattern_ids + defp process_date(date_str, acc) do + case process_date(date_str) do + :error -> + :error + + %{date: date, route_ids: date_specific_route_ids} -> + filtered_route_ids = consolidate_route_ids(acc, date_specific_route_ids) + + acc + |> Map.merge(%{route_ids: filtered_route_ids, date: date}) + |> gather_trip_based_route_pattern_ids() + end + end + defp process_date(date_str) do case Date.from_iso8601(date_str) do {:ok, date} -> @@ -177,10 +211,10 @@ defmodule ApiWeb.RoutePatternController do |> ServiceByDate.by_date() |> RoutesByService.for_service_ids() - %{date: date, date_specific_route_ids: ids} + %{date: date, route_ids: ids} {:error, _} -> - [] + :error end end diff --git a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs index 6f9843e0..7ba1b56c 100644 --- a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs @@ -482,7 +482,7 @@ defmodule ApiWeb.RoutePatternControllerTest do test "can filter by date", %{conn: base_conn} do today = Parse.Time.service_date() - bad_date = ~D[2017-01-01] + bad_date = Date.add(today, 2) service = %Model.Service{ id: "service", @@ -530,7 +530,7 @@ defmodule ApiWeb.RoutePatternControllerTest do test "can filter by date and route(s)", %{conn: base_conn} do today = Parse.Time.service_date() - bad_date = ~D[2027-01-01] + bad_date = Date.add(today, 2) service = %Model.Service{ id: "service", From 8dca8c656ef87dd98cce57a66e3d426cb78d1e9e Mon Sep 17 00:00:00 2001 From: Brian Fauble Date: Fri, 20 Oct 2023 11:02:29 -0600 Subject: [PATCH 4/5] pass route_patterns/_ids to trip query instead of filtering afterward. Add test for date+ids --- .../controllers/route_pattern_controller.ex | 35 +++++++++++-------- .../route_pattern_controller_test.exs | 4 +++ 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex index e45396ce..4097847f 100644 --- a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex +++ b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex @@ -166,29 +166,34 @@ defmodule ApiWeb.RoutePatternController do defp consolidate_route_ids(_, date_specific_route_ids), do: date_specific_route_ids + defp gather_trip_based_route_pattern_ids( + %{ids: ids, date: _date, route_ids: [_ | _] = route_ids} = acc + ) do + acc + |> Map.take([:date, :direction_id, :route_patterns]) + |> Map.put(:route_patterns, ids) + |> Map.put(:routes, route_ids) + |> gather_trip_based_route_pattern_ids(acc) + end + defp gather_trip_based_route_pattern_ids(%{date: _date, route_ids: [_ | _] = route_ids} = acc) do + acc + |> Map.take([:date, :direction_id, :route_patterns]) + |> Map.put(:routes, route_ids) + |> gather_trip_based_route_pattern_ids(acc) + end + + defp gather_trip_based_route_pattern_ids(acc), do: acc + + defp gather_trip_based_route_pattern_ids(filters, acc) do route_pattern_ids = - acc - |> Map.take([:date, :direction_id]) - |> Map.put(:routes, route_ids) + filters |> Trip.filter_by() |> Enum.map(& &1.route_pattern_id) - |> consolidate_route_pattern_ids(acc) Map.put(acc, :ids, route_pattern_ids) end - defp gather_trip_based_route_pattern_ids(acc), do: acc - - defp consolidate_route_pattern_ids(route_pattern_ids, %{ids: [_ | _] = ids}) do - route_pattern_ids - |> MapSet.new() - |> MapSet.intersection(MapSet.new(ids)) - |> MapSet.to_list() - end - - defp consolidate_route_pattern_ids(route_pattern_ids, _), do: route_pattern_ids - defp process_date(date_str, acc) do case process_date(date_str) do :error -> diff --git a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs index 7ba1b56c..5aacd351 100644 --- a/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs +++ b/apps/api_web/test/api_web/controllers/route_pattern_controller_test.exs @@ -675,6 +675,10 @@ defmodule ApiWeb.RoutePatternControllerTest do params = %{"filter" => %{"date" => today_iso, "route" => nil}} data = ApiWeb.RoutePatternController.index_data(base_conn, params) assert [route_pattern1, route_pattern3] == data + + params = %{"filter" => %{"date" => today_iso, "id" => "rp1,rp2"}} + data = ApiWeb.RoutePatternController.index_data(base_conn, params) + assert [route_pattern1] == data end end From 27978cbfeb5b6e46245a128770e9a695cd33eb2e Mon Sep 17 00:00:00 2001 From: Brian Fauble Date: Fri, 20 Oct 2023 11:17:46 -0600 Subject: [PATCH 5/5] remove unused take key --- .../api_web/lib/api_web/controllers/route_pattern_controller.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex index 4097847f..e57dbb0b 100644 --- a/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex +++ b/apps/api_web/lib/api_web/controllers/route_pattern_controller.ex @@ -170,7 +170,7 @@ defmodule ApiWeb.RoutePatternController do %{ids: ids, date: _date, route_ids: [_ | _] = route_ids} = acc ) do acc - |> Map.take([:date, :direction_id, :route_patterns]) + |> Map.take([:date, :direction_id]) |> Map.put(:route_patterns, ids) |> Map.put(:routes, route_ids) |> gather_trip_based_route_pattern_ids(acc)