2019-03-14 18:31:14 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.Address do
|
|
|
|
@moduledoc """
|
|
|
|
Handles the comment-related GraphQL calls
|
|
|
|
"""
|
|
|
|
require Logger
|
2019-03-19 11:16:03 +01:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-03-14 18:31:14 +01:00
|
|
|
alias Mobilizon.Service.Geospatial
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Search an address
|
|
|
|
"""
|
|
|
|
@spec search(map(), map(), map()) :: {:ok, list(Address.t())}
|
2019-11-08 19:37:14 +01:00
|
|
|
def search(_parent, %{query: query, locale: locale, page: _page, limit: _limit}, %{
|
|
|
|
context: %{ip: ip}
|
|
|
|
}) do
|
|
|
|
geolix = Geolix.lookup(ip)
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
country_code =
|
|
|
|
case geolix do
|
|
|
|
%{country: %{iso_code: country_code}} -> String.downcase(country_code)
|
|
|
|
_ -> nil
|
|
|
|
end
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
addresses = Geospatial.service().search(query, lang: locale, country_code: country_code)
|
2019-08-22 15:57:44 +02:00
|
|
|
|
2019-03-14 18:31:14 +01:00
|
|
|
{:ok, addresses}
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Reverse geocode some coordinates
|
|
|
|
"""
|
|
|
|
@spec reverse_geocode(map(), map(), map()) :: {:ok, list(Address.t())}
|
2019-11-08 19:37:14 +01:00
|
|
|
def reverse_geocode(
|
|
|
|
_parent,
|
|
|
|
%{longitude: longitude, latitude: latitude, zoom: zoom, locale: locale},
|
|
|
|
_context
|
|
|
|
) do
|
|
|
|
addresses = Geospatial.service().geocode(longitude, latitude, lang: locale, zoom: zoom)
|
2019-03-14 18:31:14 +01:00
|
|
|
|
|
|
|
{:ok, addresses}
|
|
|
|
end
|
|
|
|
end
|