Skip to content

Commit

Permalink
Take out a plug conn for generating URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
vdegove committed Jan 27, 2025
1 parent f7e359a commit 1be161f
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ defmodule TransportWeb.API.DatasetController do

@spec datasets(Plug.Conn.t(), map()) :: Plug.Conn.t()
def datasets(%Plug.Conn{} = conn, _params) do
comp_fn = fn -> prepare_datasets_index_data(conn) end
comp_fn = fn -> prepare_datasets_index_data() end
data = Transport.Cache.fetch("api-datasets-index", comp_fn, @cache_ttl)

render(conn, %{data: data})
Expand Down Expand Up @@ -86,7 +86,7 @@ defmodule TransportWeb.API.DatasetController do
if is_nil(dataset) do
conn |> put_status(404) |> render(%{errors: "dataset not found"})
else
comp_fn = fn -> prepare_dataset_detail_data(conn, dataset) end
comp_fn = fn -> prepare_dataset_detail_data(dataset) end
data = Transport.Cache.fetch("api-datasets-#{datagouv_id}", comp_fn, @cache_ttl)

conn |> assign(:data, data) |> render()
Expand Down Expand Up @@ -155,16 +155,16 @@ defmodule TransportWeb.API.DatasetController do
"features" => features
}

@spec transform_dataset(Plug.Conn.t(), Dataset.t() | map()) :: map()
defp transform_dataset(%Plug.Conn{} = conn, %Dataset{} = dataset),
@spec transform_dataset(Dataset.t() | map()) :: map()
defp transform_dataset(%Dataset{} = dataset),
do: %{
"datagouv_id" => dataset.datagouv_id,
# to help discoverability, we explicitly add the datagouv_id as the id
# (since it's used in /dataset/:id)
"id" => dataset.datagouv_id,
"title" => dataset.custom_title,
"created_at" => dataset.created_at |> DateTime.to_date() |> Date.to_string(),
"page_url" => TransportWeb.Router.Helpers.dataset_url(conn, :details, dataset.slug),
"page_url" => TransportWeb.Router.Helpers.dataset_url(TransportWeb.Endpoint, :details, dataset.slug),
"slug" => dataset.slug,
"updated" => Helpers.last_updated(Dataset.official_resources(dataset)),
"resources" => Enum.map(dataset.resources, &transform_resource/1),
Expand All @@ -185,10 +185,10 @@ defmodule TransportWeb.API.DatasetController do
"type" => "organization"
}

@spec transform_dataset_with_detail(Plug.Conn.t(), Dataset.t() | map()) :: map()
defp transform_dataset_with_detail(%Plug.Conn{} = conn, %Dataset{} = dataset) do
conn
|> transform_dataset(dataset)
@spec transform_dataset_with_detail(Dataset.t() | map()) :: map()
defp transform_dataset_with_detail(%Dataset{} = dataset) do
dataset
|> transform_dataset()
|> add_conversions(dataset)
|> Map.put(
"history",
Expand Down Expand Up @@ -334,7 +334,7 @@ defmodule TransportWeb.API.DatasetController do
|> Enum.map(fn region -> %{"name" => region.nom, "insee" => region.insee} end)
end

defp prepare_datasets_index_data(%Plug.Conn{} = conn) do
def prepare_datasets_index_data do
datasets_with_gtfs_metadata =
DB.Dataset.base_query()
|> DB.Dataset.join_from_dataset_to_metadata(Transport.Validators.GTFSTransport.validator_name())
Expand Down Expand Up @@ -383,10 +383,10 @@ defmodule TransportWeb.API.DatasetController do
enriched_dataset = Map.get(existing_ids, dataset.id)
add_enriched_resources_to_dataset(dataset, enriched_dataset)
end)
|> Enum.map(&transform_dataset(conn, &1))
|> Enum.map(&transform_dataset(&1))
end

defp prepare_dataset_detail_data(%Plug.Conn{} = conn, %DB.Dataset{} = dataset) do
defp prepare_dataset_detail_data(%DB.Dataset{} = dataset) do
gtfs_resources_with_metadata =
DB.Resource.base_query()
|> DB.ResourceHistory.join_resource_with_latest_resource_history()
Expand Down Expand Up @@ -421,6 +421,6 @@ defmodule TransportWeb.API.DatasetController do

dataset = dataset |> Map.put(:resources, enriched_resources)

transform_dataset_with_detail(conn, dataset)
transform_dataset_with_detail(dataset)
end
end

0 comments on commit 1be161f

Please sign in to comment.