Allow to add metadata to an event

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-08-09 14:26:11 +02:00
parent 33bf8334fe
commit 5f3d1f89df
24 changed files with 1512 additions and 339 deletions

View File

@@ -16,6 +16,7 @@ defmodule Mobilizon.Events.Event do
alias Mobilizon.Discussions.Comment
alias Mobilizon.Events.{
EventMetadata,
EventOptions,
EventParticipantStats,
EventStatus,
@@ -108,6 +109,7 @@ defmodule Mobilizon.Events.Event do
embeds_one(:options, EventOptions, on_replace: :delete)
embeds_one(:participant_stats, EventParticipantStats, on_replace: :update)
embeds_many(:metadata, EventMetadata, on_replace: :delete)
belongs_to(:organizer_actor, Actor, foreign_key: :organizer_actor_id)
belongs_to(:attributed_to, Actor, foreign_key: :attributed_to_id)
belongs_to(:physical_address, Address, on_replace: :nilify)
@@ -151,6 +153,7 @@ defmodule Mobilizon.Events.Event do
defp common_changeset(%Changeset{} = changeset, attrs) do
changeset
|> cast_embed(:options)
|> cast_embed(:metadata)
|> put_assoc(:contacts, Map.get(attrs, :contacts, []))
|> put_assoc(:media, Map.get(attrs, :media, []))
|> put_tags(attrs)

View File

@@ -0,0 +1,45 @@
defmodule Mobilizon.Events.EventMetadata do
@moduledoc """
Participation stats on event
"""
use Ecto.Schema
import Ecto.Changeset
import EctoEnum
defenum(EventMetadataTypeEnum, string: 0, integer: 1, boolean: 2)
@type t :: %__MODULE__{
key: String.t(),
value: String.t()
}
@required_attrs [
:key,
:value
]
@optional_attrs [
:title,
:type
]
@attrs @required_attrs ++ @optional_attrs
@primary_key false
@derive Jason.Encoder
embedded_schema do
field(:key, :string)
field(:title, :string)
field(:value, :string)
field(:type, EventMetadataTypeEnum, default: :string)
end
@doc false
@spec changeset(t, map) :: Ecto.Changeset.t()
def changeset(%__MODULE__{} = event_metadata, attrs) do
event_metadata
|> cast(attrs, @attrs)
|> validate_required(@required_attrs)
end
end