2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Addresses do
|
2018-01-17 11:39:01 +01:00
|
|
|
@moduledoc """
|
|
|
|
The Addresses context.
|
|
|
|
"""
|
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
import Ecto.Query
|
2018-01-17 11:39:01 +01:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Addresses.Address
|
2019-09-08 01:49:56 +02:00
|
|
|
alias Mobilizon.Storage.Repo
|
|
|
|
|
|
|
|
require Logger
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single address.
|
|
|
|
"""
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec get_address(integer | String.t()) :: Address.t() | nil
|
2019-08-22 15:57:44 +02:00
|
|
|
def get_address(id), do: Repo.get(Address, id)
|
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
@doc """
|
2019-09-08 03:05:30 +02:00
|
|
|
Gets a single address.
|
|
|
|
Raises `Ecto.NoResultsError` if the address does not exist.
|
2019-07-30 10:35:29 +02:00
|
|
|
"""
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec get_address!(integer | String.t()) :: Address.t()
|
|
|
|
def get_address!(id), do: Repo.get!(Address, id)
|
2019-07-30 10:35:29 +02:00
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
@doc """
|
2019-09-08 03:05:30 +02:00
|
|
|
Gets a single address by its url.
|
|
|
|
"""
|
|
|
|
@spec get_address_by_url(String.t()) :: Address.t() | nil
|
|
|
|
def get_address_by_url(url), do: Repo.get_by(Address, url: url)
|
2018-01-17 11:39:01 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@doc """
|
|
|
|
Creates an address.
|
2018-01-17 11:39:01 +01:00
|
|
|
"""
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec create_address(map) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()}
|
2018-01-17 11:39:01 +01:00
|
|
|
def create_address(attrs \\ %{}) do
|
|
|
|
%Address{}
|
|
|
|
|> Address.changeset(attrs)
|
2019-08-22 15:57:44 +02:00
|
|
|
|> Repo.insert(
|
2020-01-30 20:27:25 +01:00
|
|
|
on_conflict: {:replace_all_except, [:id]},
|
2019-08-22 15:57:44 +02:00
|
|
|
conflict_target: [:origin_id]
|
|
|
|
)
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-08 03:05:30 +02:00
|
|
|
Updates an address.
|
2018-01-17 11:39:01 +01:00
|
|
|
"""
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec update_address(Address.t(), map) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()}
|
2018-01-17 11:39:01 +01:00
|
|
|
def update_address(%Address{} = address, attrs) do
|
|
|
|
address
|
|
|
|
|> Address.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-08 03:05:30 +02:00
|
|
|
Deletes an address.
|
2018-01-17 11:39:01 +01:00
|
|
|
"""
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec delete_address(Address.t()) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
def delete_address(%Address{} = address), do: Repo.delete(address)
|
2018-01-17 11:39:01 +01:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of addresses.
|
|
|
|
"""
|
|
|
|
@spec list_addresses :: [Address.t()]
|
|
|
|
def list_addresses, do: Repo.all(Address)
|
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
@doc """
|
2019-09-08 03:05:30 +02:00
|
|
|
Searches addresses.
|
2018-01-17 11:39:01 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
We only look at the description for now, and eventually order by object distance.
|
2018-01-17 11:39:01 +01:00
|
|
|
"""
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec search_addresses(String.t(), keyword) :: [Address.t()]
|
|
|
|
def search_addresses(search, options \\ []) do
|
|
|
|
query =
|
|
|
|
search
|
|
|
|
|> search_addresses_query(Keyword.get(options, :limit, 5))
|
|
|
|
|> order_by_coords(Keyword.get(options, :coords))
|
|
|
|
|> filter_by_contry(Keyword.get(options, :country))
|
|
|
|
|
|
|
|
case Keyword.get(options, :single, false) do
|
2019-09-11 03:16:37 +02:00
|
|
|
true ->
|
|
|
|
Repo.one(query)
|
|
|
|
|
|
|
|
false ->
|
|
|
|
Repo.all(query)
|
2018-01-21 19:43:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-14 18:31:14 +01:00
|
|
|
@doc """
|
2019-09-08 03:05:30 +02:00
|
|
|
Reverse geocode from coordinates.
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
We only take addresses 50km around and sort them by distance.
|
2019-03-14 18:31:14 +01:00
|
|
|
"""
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec reverse_geocode(number, number, keyword) :: [Address.t()]
|
|
|
|
def reverse_geocode(lon, lat, options) do
|
2019-03-14 18:31:14 +01:00
|
|
|
limit = Keyword.get(options, :limit, 5)
|
2019-09-08 03:05:30 +02:00
|
|
|
radius = Keyword.get(options, :radius, 50_000)
|
|
|
|
country = Keyword.get(options, :country)
|
|
|
|
srid = Keyword.get(options, :srid, 4326)
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
with {:ok, point} <- Geo.WKT.decode("SRID=#{srid};POINT(#{lon} #{lat})") do
|
|
|
|
point
|
|
|
|
|> addresses_around_query(radius, limit)
|
|
|
|
|> filter_by_contry(country)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
end
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec search_addresses_query(String.t(), integer) :: Ecto.Query.t()
|
|
|
|
defp search_addresses_query(search, limit) do
|
|
|
|
from(
|
|
|
|
a in Address,
|
|
|
|
where: ilike(a.description, ^"%#{search}%"),
|
|
|
|
limit: ^limit
|
|
|
|
)
|
|
|
|
end
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec order_by_coords(Ecto.Query.t(), map | nil) :: Ecto.Query.t()
|
|
|
|
defp order_by_coords(query, nil), do: query
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
defp order_by_coords(query, coords) do
|
|
|
|
from(
|
|
|
|
a in query,
|
|
|
|
order_by: [fragment("? <-> ?", a.geom, ^"POINT(#{coords.lon} #{coords.lat})'")]
|
|
|
|
)
|
2019-03-14 18:31:14 +01:00
|
|
|
end
|
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec filter_by_contry(Ecto.Query.t(), String.t() | nil) :: Ecto.Query.t()
|
|
|
|
defp filter_by_contry(query, nil), do: query
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
defp filter_by_contry(query, country) do
|
|
|
|
from(
|
|
|
|
a in query,
|
|
|
|
where: ilike(a.country, ^"%#{country}%")
|
|
|
|
)
|
|
|
|
end
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec addresses_around_query(Geo.geometry(), integer, integer) :: Ecto.Query.t()
|
|
|
|
defp addresses_around_query(point, radius, limit) do
|
2019-03-14 18:31:14 +01:00
|
|
|
import Geo.PostGIS
|
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
from(a in Address,
|
|
|
|
where: st_dwithin_in_meters(^point, a.geom, ^radius),
|
|
|
|
order_by: [fragment("? <-> ?", a.geom, ^point)],
|
|
|
|
limit: ^limit
|
|
|
|
)
|
2019-03-14 18:31:14 +01:00
|
|
|
end
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|