~ihabunek/triglav

1709b4463509423d8b84eaebffea35e29fd6b541 — Ivan Habunek 3 months ago 86bd16c
Filter PT relations by type, add docs
1 files changed, 17 insertions(+), 5 deletions(-)

M lib/triglav/zet/osmosis.ex
M lib/triglav/zet/osmosis.ex => lib/triglav/zet/osmosis.ex +17 -5
@@ 11,9 11,20 @@ defmodule Triglav.Zet.Osmosis do

  import Ecto.Query

  @doc """
  Returns a list of relations which are tagged with `type=route` or
  `type=route_master` and `network=ZET`.

  Options:

  * ref - filter by `ref` tag
  * type - filter by `type` tag
  * members - if set to true, will preload related RelationMember records
  """
  def list_public_transport_relations(opts \\ []) do
    pt_relations()
    |> maybe_filter_by_ref(opts)
    |> maybe_filter_by_tag(:ref, opts)
    |> maybe_filter_by_tag(:type, opts)
    |> maybe_preload_members(opts)
    |> Repo.all()
  end


@@ 154,11 165,12 @@ defmodule Triglav.Zet.Osmosis do
    )
  end

  defp maybe_filter_by_ref(query, opts) do
    ref = Keyword.get(opts, :ref)
  defp maybe_filter_by_tag(query, tag, opts) do
    value = Keyword.get(opts, tag)
    tag = to_string(tag)

    if ref do
      where(query, [r], fragment("tags->?", "ref") == ^ref)
    if value do
      where(query, [r], fragment("tags->?", ^tag) == ^value)
    else
      query
    end