2018-10-11 17:37:39 +02:00
|
|
|
defmodule Mobilizon.Addresses.Address do
|
2019-09-08 03:05:30 +02:00
|
|
|
@moduledoc """
|
|
|
|
Represents an address for an event or a group.
|
|
|
|
"""
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
use Ecto.Schema
|
2019-09-08 03:05:30 +02:00
|
|
|
|
2018-01-17 11:39:01 +01:00
|
|
|
import Ecto.Changeset
|
2019-09-08 03:05:30 +02:00
|
|
|
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Events.Event
|
2019-09-08 03:05:30 +02:00
|
|
|
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.Endpoint
|
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
country: String.t(),
|
|
|
|
locality: String.t(),
|
|
|
|
region: String.t(),
|
|
|
|
description: String.t(),
|
|
|
|
geom: Geo.PostGIS.Geometry.t(),
|
|
|
|
postal_code: String.t(),
|
|
|
|
street: String.t(),
|
2019-11-08 19:37:14 +01:00
|
|
|
type: String.t(),
|
2019-09-08 03:05:30 +02:00
|
|
|
url: String.t(),
|
|
|
|
origin_id: String.t(),
|
|
|
|
events: [Event.t()]
|
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:url]
|
|
|
|
@optional_attrs [
|
2019-07-30 10:35:29 +02:00
|
|
|
:description,
|
|
|
|
:geom,
|
|
|
|
:country,
|
|
|
|
:locality,
|
|
|
|
:region,
|
|
|
|
:postal_code,
|
|
|
|
:street,
|
2019-11-08 19:37:14 +01:00
|
|
|
:origin_id,
|
|
|
|
:type
|
2019-07-30 10:35:29 +02:00
|
|
|
]
|
2019-09-08 03:05:30 +02:00
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
schema "addresses" do
|
2019-03-22 15:51:23 +01:00
|
|
|
field(:country, :string)
|
|
|
|
field(:locality, :string)
|
|
|
|
field(:region, :string)
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:description, :string)
|
2018-08-24 12:31:41 +02:00
|
|
|
field(:geom, Geo.PostGIS.Geometry)
|
2019-03-22 15:51:23 +01:00
|
|
|
field(:postal_code, :string)
|
|
|
|
field(:street, :string)
|
2019-11-08 19:37:14 +01:00
|
|
|
field(:type, :string)
|
2019-07-30 10:35:29 +02:00
|
|
|
field(:url, :string)
|
2019-08-22 15:57:44 +02:00
|
|
|
field(:origin_id, :string)
|
2019-09-08 03:05:30 +02:00
|
|
|
|
|
|
|
has_many(:events, Event, foreign_key: :physical_address_id)
|
2018-01-17 11:39:01 +01:00
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec changeset(t | Ecto.Schema.t(), map) :: Ecto.Changeset.t()
|
2019-09-13 01:55:45 +02:00
|
|
|
def changeset(%__MODULE__{} = address, attrs) do
|
2018-01-17 11:39:01 +01:00
|
|
|
address
|
2019-07-30 10:35:29 +02:00
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> set_url()
|
2019-09-08 03:05:30 +02:00
|
|
|
|> validate_required(@required_attrs)
|
2021-04-22 17:48:43 +02:00
|
|
|
|> unique_constraint(:url, name: :addresses_url_index)
|
2019-07-30 10:35:29 +02:00
|
|
|
end
|
|
|
|
|
2019-09-08 03:05:30 +02:00
|
|
|
@spec set_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
2019-07-30 10:35:29 +02:00
|
|
|
defp set_url(%Ecto.Changeset{changes: changes} = changeset) do
|
2020-01-28 19:18:33 +01:00
|
|
|
url = Map.get(changes, :url, "#{Endpoint.url()}/address/#{Ecto.UUID.generate()}")
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
|
|
put_change(changeset, :url, url)
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|
2019-11-05 17:49:40 +01:00
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec coords(nil | t) :: nil | {float, float}
|
2019-11-05 17:49:40 +01:00
|
|
|
def coords(nil), do: nil
|
|
|
|
|
|
|
|
def coords(%__MODULE__{} = address) do
|
2021-08-16 10:23:16 +02:00
|
|
|
with %Geo.Point{coordinates: {longitude, latitude}, srid: 4326} <- address.geom do
|
2019-11-05 17:49:40 +01:00
|
|
|
{latitude, longitude}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec representation(nil | t) :: nil | String.t()
|
2019-11-05 17:49:40 +01:00
|
|
|
def representation(nil), do: nil
|
|
|
|
|
|
|
|
def representation(%__MODULE__{} = address) do
|
2021-04-19 12:41:40 +02:00
|
|
|
String.trim(
|
2021-06-07 16:39:44 +02:00
|
|
|
"#{address.street} #{address.postal_code} #{address.locality} #{address.region} #{address.country}"
|
2021-04-19 12:41:40 +02:00
|
|
|
)
|
2019-11-05 17:49:40 +01:00
|
|
|
end
|
2018-01-17 11:39:01 +01:00
|
|
|
end
|