mobilizon.chapril.org-mobil.../lib/mobilizon/addresses/addresses.ex

152 lines
4.0 KiB
Elixir
Raw Normal View History

defmodule Mobilizon.Addresses do
@moduledoc """
The Addresses context.
"""
2019-09-08 03:05:30 +02:00
import Ecto.Query
alias Mobilizon.Addresses.Address
2019-09-08 01:49:56 +02:00
alias Mobilizon.Storage.Repo
require Logger
@doc """
Gets a single address.
"""
2019-09-08 03:05:30 +02:00
@spec get_address(integer | String.t()) :: Address.t() | nil
def get_address(id), do: Repo.get(Address, id)
@doc """
2019-09-08 03:05:30 +02:00
Gets a single address.
Raises `Ecto.NoResultsError` if the address does not exist.
"""
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)
@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)
@spec get_address_by_origin_id(String.t()) :: Address.t() | nil
def get_address_by_origin_id(origin_id), do: Repo.get_by(Address, origin_id: origin_id)
2019-09-08 03:05:30 +02:00
@doc """
Creates an address.
"""
2019-09-08 03:05:30 +02:00
@spec create_address(map) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()}
def create_address(attrs \\ %{}) do
%Address{}
|> Address.changeset(attrs)
|> Repo.insert(
on_conflict: {:replace_all_except, [:id]},
conflict_target: [:origin_id]
)
end
@doc """
2019-09-08 03:05:30 +02:00
Updates an address.
"""
2019-09-08 03:05:30 +02:00
@spec update_address(Address.t(), map) :: {:ok, Address.t()} | {:error, Ecto.Changeset.t()}
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.
"""
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)
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)
@doc """
2019-09-08 03:05:30 +02:00
Searches addresses.
2019-09-08 03:05:30 +02:00
We only look at the description for now, and eventually order by object distance.
"""
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)
end
end
@doc """
2019-09-08 03:05:30 +02:00
Reverse geocode from coordinates.
2019-09-08 03:05:30 +02:00
We only take addresses 50km around and sort them by distance.
"""
2019-09-08 03:05:30 +02:00
@spec reverse_geocode(number, number, keyword) :: [Address.t()]
def reverse_geocode(lon, lat, options) do
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-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-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-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-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})'")]
)
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-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-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
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
)
end
end