mobilizon.chapril.org-mobil.../lib/graphql/resolvers/address.ex

59 lines
1.4 KiB
Elixir
Raw Normal View History

2020-01-26 20:34:25 +01:00
defmodule Mobilizon.GraphQL.Resolvers.Address do
@moduledoc """
Handles the comment-related GraphQL calls
"""
2020-01-22 02:14:42 +01:00
alias Mobilizon.Addresses.Address
alias Mobilizon.Service.{Geospatial, Pictures}
2020-01-22 02:14:42 +01:00
require Logger
@doc """
Search an address
"""
@spec search(map, map, map) :: {:ok, [map()]}
2020-01-26 20:34:25 +01:00
def search(
_parent,
%{query: query, locale: locale, page: _page, limit: _limit} = args,
2020-01-26 20:34:25 +01:00
%{context: %{ip: ip}}
) do
geolix = Geolix.lookup(ip)
country_code =
case geolix do
%{country: %{iso_code: country_code}} -> String.downcase(country_code)
_ -> nil
end
addresses =
Geospatial.service().search(query,
lang: locale,
country_code: country_code,
type: Map.get(args, :type)
)
{:ok, addresses}
end
@doc """
Reverse geocode some coordinates
"""
2020-01-26 20:34:25 +01:00
@spec reverse_geocode(map, map, map) :: {:ok, [Address.t()]}
def reverse_geocode(
_parent,
%{longitude: longitude, latitude: latitude, zoom: zoom, locale: locale},
_context
) do
addresses =
Geospatial.service().geocode(longitude, latitude, lang: locale, zoom: zoom)
|> Enum.map(fn address ->
picture_info =
Pictures.service().search(address.locality || address.region || address.country)
Map.put(address, :picture_info, picture_info)
end)
{:ok, addresses}
end
end