Correctly handle event update
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -110,6 +110,24 @@ defmodule Mobilizon.Actors.Actor do
|
||||
|> unique_constraint(:url, name: :actors_url_index)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def update_changeset(%Actor{} = actor, attrs) do
|
||||
actor
|
||||
|> Ecto.Changeset.cast(attrs, [
|
||||
:name,
|
||||
:summary,
|
||||
:keys,
|
||||
:manually_approves_followers,
|
||||
:suspended,
|
||||
:user_id
|
||||
])
|
||||
|> cast_embed(:avatar)
|
||||
|> cast_embed(:banner)
|
||||
|> validate_required([:preferred_username, :keys, :suspended, :url])
|
||||
|> unique_constraint(:preferred_username, name: :actors_preferred_username_domain_type_index)
|
||||
|> unique_constraint(:url, name: :actors_url_index)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changeset for person registration
|
||||
"""
|
||||
|
||||
@@ -124,7 +124,7 @@ defmodule Mobilizon.Actors do
|
||||
"""
|
||||
def update_actor(%Actor{} = actor, attrs) do
|
||||
actor
|
||||
|> Actor.changeset(attrs)
|
||||
|> Actor.update_changeset(attrs)
|
||||
|> delete_files_if_media_changed()
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@@ -54,10 +54,10 @@ defmodule Mobilizon.Events.Event do
|
||||
field(:online_address, :string)
|
||||
field(:phone_address, :string)
|
||||
field(:category, :string)
|
||||
embeds_one(:options, Mobilizon.Events.EventOptions)
|
||||
embeds_one(:options, Mobilizon.Events.EventOptions, on_replace: :update)
|
||||
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")
|
||||
many_to_many(:tags, Tag, join_through: "events_tags", on_replace: :delete)
|
||||
many_to_many(:participants, Actor, join_through: Participant)
|
||||
has_many(:tracks, Track)
|
||||
has_many(:sessions, Session)
|
||||
@@ -98,6 +98,38 @@ defmodule Mobilizon.Events.Event do
|
||||
])
|
||||
end
|
||||
|
||||
@doc false
|
||||
def update_changeset(%Event{} = event, attrs) do
|
||||
event
|
||||
|> Ecto.Changeset.cast(attrs, [
|
||||
:title,
|
||||
:slug,
|
||||
:description,
|
||||
:begins_on,
|
||||
:ends_on,
|
||||
:category,
|
||||
:status,
|
||||
:visibility,
|
||||
:publish_at,
|
||||
:online_address,
|
||||
:phone_address,
|
||||
:picture_id,
|
||||
:physical_address_id
|
||||
])
|
||||
|> cast_embed(:options)
|
||||
|> put_tags(attrs)
|
||||
|> validate_required([
|
||||
:title,
|
||||
:begins_on,
|
||||
:organizer_actor_id,
|
||||
:url,
|
||||
:uuid
|
||||
])
|
||||
end
|
||||
|
||||
defp put_tags(changeset, %{"tags" => tags}), do: put_assoc(changeset, :tags, tags)
|
||||
defp put_tags(changeset, _), do: changeset
|
||||
|
||||
def can_event_be_managed_by(%Event{organizer_actor_id: organizer_actor_id}, actor_id)
|
||||
when organizer_actor_id == actor_id do
|
||||
{:event_can_be_managed, true}
|
||||
|
||||
@@ -227,11 +227,12 @@ defmodule Mobilizon.Events do
|
||||
:tracks,
|
||||
:tags,
|
||||
:participants,
|
||||
:physical_address
|
||||
:physical_address,
|
||||
:picture
|
||||
])}
|
||||
|
||||
err ->
|
||||
{:error, err}
|
||||
_err ->
|
||||
{:error, :event_not_found}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -435,7 +436,8 @@ defmodule Mobilizon.Events do
|
||||
"""
|
||||
def update_event(%Event{} = event, attrs) do
|
||||
event
|
||||
|> Event.changeset(attrs)
|
||||
|> Repo.preload(:tags)
|
||||
|> Event.update_changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user