Some work

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2018-07-04 14:29:17 +02:00
parent 394057d45e
commit 93a97b0865
56 changed files with 5577 additions and 4327 deletions

View File

@@ -1,30 +1,5 @@
defmodule Eventos.Events.Event.TitleSlug do
@moduledoc """
Generates a slug for an event title
"""
alias Eventos.Events.Event
import Ecto.Query
alias Eventos.Repo
use EctoAutoslugField.Slug, from: :title, to: :slug
def build_slug(sources, changeset) do
slug = super(sources, changeset)
build_unique_slug(slug, changeset)
end
defp build_unique_slug(slug, changeset) do
query = from e in Event,
where: e.slug == ^slug
case Repo.one(query) do
nil -> slug
_event ->
slug
|> Eventos.Slug.increment_slug()
|> build_unique_slug(changeset)
end
end
end
import EctoEnum
defenum AddressTypeEnum, :address_type, [:physical, :url, :phone, :other]
defmodule Eventos.Events.Event do
@moduledoc """
@@ -33,7 +8,6 @@ defmodule Eventos.Events.Event do
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Events.{Event, Participant, Tag, Category, Session, Track}
alias Eventos.Events.Event.TitleSlug
alias Eventos.Actors.Actor
alias Eventos.Addresses.Address
@@ -44,7 +18,6 @@ defmodule Eventos.Events.Event do
field :description, :string
field :ends_on, Timex.Ecto.DateTimeWithTimezone
field :title, :string
field :slug, TitleSlug.Type
field :state, :integer, default: 0
field :status, :integer, default: 0
field :public, :boolean, default: true
@@ -52,6 +25,9 @@ defmodule Eventos.Events.Event do
field :large_image, :string
field :publish_at, Timex.Ecto.DateTimeWithTimezone
field :uuid, Ecto.UUID, default: Ecto.UUID.generate()
field :address_type, AddressTypeEnum, default: :physical
field :online_address, :string
field :phone, :string
belongs_to :organizer_actor, Actor, [foreign_key: :organizer_actor_id]
belongs_to :attributed_to, Actor, [foreign_key: :attributed_to_id]
many_to_many :tags, Tag, join_through: "events_tags"
@@ -59,7 +35,7 @@ defmodule Eventos.Events.Event do
many_to_many :participants, Actor, join_through: Participant
has_many :tracks, Track
has_many :sessions, Session
belongs_to :address, Address
belongs_to :physical_address, Address
timestamps(type: :utc_datetime)
end
@@ -75,13 +51,28 @@ defmodule Eventos.Events.Event do
""
end
event
|> cast(attrs, [:title, :description, :url, :begins_on, :ends_on, :organizer_actor_id, :category_id, :state, :status, :public, :thumbnail, :large_image, :publish_at])
|> 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,
])
|> cast_assoc(:tags)
|> cast_assoc(:address)
|> TitleSlug.maybe_generate_slug()
|> TitleSlug.unique_constraint()
|> cast_assoc(:physical_address)
|> put_change(:uuid, uuid)
|> put_change(:url, "#{EventosWeb.Endpoint.url()}/@#{actor_url}/#{uuid}")
|> validate_required([:title, :description, :begins_on, :ends_on, :organizer_actor_id, :category_id, :url, :uuid])
|> validate_required([:title, :begins_on, :ends_on, :organizer_actor_id, :category_id, :url, :uuid, :address_type])
end
end

View File

@@ -32,7 +32,7 @@ defmodule Eventos.Events do
limit: ^limit,
order_by: [desc: :id],
offset: ^start,
preload: [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :address]
preload: [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :physical_address]
events = Repo.all(query)
count_events = Repo.one(from e in Event, select: count(e.id), where: e.organizer_actor_id == ^actor_id)
{:ok, events, count_events}
@@ -89,7 +89,7 @@ defmodule Eventos.Events do
"""
def get_event_full!(id) do
event = Repo.get!(Event, id)
Repo.preload(event, [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :address])
Repo.preload(event, [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :physical_address])
end
@doc """
@@ -97,7 +97,7 @@ defmodule Eventos.Events do
"""
def get_event_full_by_url!(url) do
event = Repo.get_by(Event, url: url)
Repo.preload(event, [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :address])
Repo.preload(event, [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :physical_address])
end
@doc """
@@ -105,23 +105,7 @@ defmodule Eventos.Events do
"""
def get_event_full_by_uuid(uuid) do
event = Repo.get_by(Event, uuid: uuid)
Repo.preload(event, [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :address])
end
@spec get_event_full_by_name_and_slug!(String.t, String.t) :: Event.t
def get_event_full_by_name_and_slug!(name, slug) do
query = case String.split(name, "@") do
[name, domain] -> from e in Event,
join: a in Actor,
on: a.id == e.organizer_actor_id and a.preferred_username == ^name and a.domain == ^domain,
where: e.slug == ^slug
[name] -> from e in Event,
join: a in Actor,
on: a.id == e.organizer_actor_id and a.preferred_username == ^name and is_nil(a.domain),
where: e.slug == ^slug
end
event = Repo.one(query)
Repo.preload(event, [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :address])
Repo.preload(event, [:organizer_actor, :category, :sessions, :tracks, :tags, :participants, :physical_address])
end
@doc """