2018-07-04 14:29:17 +02:00
|
|
|
import EctoEnum
|
|
|
|
defenum AddressTypeEnum, :address_type, [:physical, :url, :phone, :other]
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
defmodule Eventos.Events.Event do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Represents an event
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-05-19 10:19:21 +02:00
|
|
|
alias Eventos.Events.{Event, Participant, Tag, Category, Session, Track}
|
2018-05-18 09:56:21 +02:00
|
|
|
alias Eventos.Actors.Actor
|
2018-01-17 11:39:01 +01:00
|
|
|
alias Eventos.Addresses.Address
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
schema "events" do
|
2018-05-17 11:32:23 +02:00
|
|
|
field :url, :string
|
|
|
|
field :local, :boolean, default: true
|
2018-01-13 23:33:03 +01:00
|
|
|
field :begins_on, Timex.Ecto.DateTimeWithTimezone
|
2017-12-08 09:58:14 +01:00
|
|
|
field :description, :string
|
2018-01-13 23:33:03 +01:00
|
|
|
field :ends_on, Timex.Ecto.DateTimeWithTimezone
|
2017-12-08 09:58:14 +01:00
|
|
|
field :title, :string
|
2018-01-13 23:33:03 +01:00
|
|
|
field :state, :integer, default: 0
|
2018-01-15 11:50:26 +01:00
|
|
|
field :status, :integer, default: 0
|
2018-01-13 23:33:03 +01:00
|
|
|
field :public, :boolean, default: true
|
|
|
|
field :thumbnail, :string
|
|
|
|
field :large_image, :string
|
|
|
|
field :publish_at, Timex.Ecto.DateTimeWithTimezone
|
2018-06-06 17:42:53 +02:00
|
|
|
field :uuid, Ecto.UUID, default: Ecto.UUID.generate()
|
2018-07-04 14:29:17 +02:00
|
|
|
field :address_type, AddressTypeEnum, default: :physical
|
|
|
|
field :online_address, :string
|
|
|
|
field :phone, :string
|
2018-05-18 09:56:21 +02:00
|
|
|
belongs_to :organizer_actor, Actor, [foreign_key: :organizer_actor_id]
|
2018-06-14 17:25:55 +02:00
|
|
|
belongs_to :attributed_to, Actor, [foreign_key: :attributed_to_id]
|
2018-01-15 12:17:34 +01:00
|
|
|
many_to_many :tags, Tag, join_through: "events_tags"
|
2018-01-15 12:04:09 +01:00
|
|
|
belongs_to :category, Category
|
2018-05-18 09:56:21 +02:00
|
|
|
many_to_many :participants, Actor, join_through: Participant
|
2018-01-13 23:33:03 +01:00
|
|
|
has_many :tracks, Track
|
|
|
|
has_many :sessions, Session
|
2018-07-04 14:29:17 +02:00
|
|
|
belongs_to :physical_address, Address
|
2017-12-08 09:58:14 +01:00
|
|
|
|
2018-05-17 11:32:23 +02:00
|
|
|
timestamps(type: :utc_datetime)
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%Event{} = event, attrs) do
|
2018-06-14 17:25:55 +02:00
|
|
|
uuid = Ecto.UUID.generate()
|
|
|
|
|
|
|
|
# TODO : check what's the use here. Tests ?
|
|
|
|
actor_url = if Map.has_key?(attrs, :organizer_actor) do
|
|
|
|
attrs.organizer_actor.preferred_username
|
|
|
|
else
|
|
|
|
""
|
|
|
|
end
|
|
|
|
event
|
2018-07-04 14:29:17 +02:00
|
|
|
|> Ecto.Changeset.cast(attrs, [
|
|
|
|
:title,
|
|
|
|
:description,
|
|
|
|
:url,
|
|
|
|
:begins_on,
|
|
|
|
:ends_on,
|
|
|
|
:organizer_actor_id,
|
|
|
|
:category_id,
|
|
|
|
:state,
|
|
|
|
:status,
|
|
|
|
:public,
|
|
|
|
:thumbnail,
|
|
|
|
:large_image,
|
|
|
|
:publish_at,
|
|
|
|
:address_type,
|
|
|
|
:online_address,
|
|
|
|
:phone,
|
|
|
|
])
|
2018-01-15 12:17:34 +01:00
|
|
|
|> cast_assoc(:tags)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> cast_assoc(:physical_address)
|
2018-06-14 17:25:55 +02:00
|
|
|
|> put_change(:uuid, uuid)
|
|
|
|
|> put_change(:url, "#{EventosWeb.Endpoint.url()}/@#{actor_url}/#{uuid}")
|
2018-07-04 14:29:17 +02:00
|
|
|
|> validate_required([:title, :begins_on, :ends_on, :organizer_actor_id, :category_id, :url, :uuid, :address_type])
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-01-14 17:56:50 +01:00
|
|
|
end
|