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