Make Categories a predefined list
Signed-off-by: Thomas Citharel <tcit@tcit.fr> Allow null values for categories for now Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -1,26 +0,0 @@
|
||||
defmodule Mobilizon.Events.Category do
|
||||
@moduledoc """
|
||||
Represents a category for events
|
||||
"""
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Mobilizon.Events.Category
|
||||
use Arc.Ecto.Schema
|
||||
|
||||
schema "categories" do
|
||||
field(:description, :string)
|
||||
field(:picture, MobilizonWeb.Uploaders.Category.Type)
|
||||
field(:title, :string, null: false)
|
||||
|
||||
timestamps()
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(%Category{} = category, attrs) do
|
||||
category
|
||||
|> cast(attrs, [:title, :description])
|
||||
|> cast_attachments(attrs, [:picture])
|
||||
|> validate_required([:title])
|
||||
|> unique_constraint(:title)
|
||||
end
|
||||
end
|
||||
@@ -19,13 +19,21 @@ defenum(Mobilizon.Events.EventStatusEnum, :event_status_type, [
|
||||
:cancelled
|
||||
])
|
||||
|
||||
defenum(Mobilizon.Event.EventCategoryEnum, :event_category_type, [
|
||||
:business,
|
||||
:conference,
|
||||
:birthday,
|
||||
:demonstration,
|
||||
:meeting
|
||||
])
|
||||
|
||||
defmodule Mobilizon.Events.Event do
|
||||
@moduledoc """
|
||||
Represents an event
|
||||
"""
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Mobilizon.Events.{Event, Participant, Tag, Category, Session, Track}
|
||||
alias Mobilizon.Events.{Event, Participant, Tag, Session, Track}
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Addresses.Address
|
||||
|
||||
@@ -45,10 +53,10 @@ defmodule Mobilizon.Events.Event do
|
||||
field(:uuid, Ecto.UUID, default: Ecto.UUID.generate())
|
||||
field(:online_address, :string)
|
||||
field(:phone_address, :string)
|
||||
field(:category, :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")
|
||||
belongs_to(:category, Category)
|
||||
many_to_many(:participants, Actor, join_through: Participant)
|
||||
has_many(:tracks, Track)
|
||||
has_many(:sessions, Session)
|
||||
@@ -67,7 +75,7 @@ defmodule Mobilizon.Events.Event do
|
||||
:begins_on,
|
||||
:ends_on,
|
||||
:organizer_actor_id,
|
||||
:category_id,
|
||||
:category,
|
||||
:status,
|
||||
:visibility,
|
||||
:thumbnail,
|
||||
@@ -83,7 +91,7 @@ defmodule Mobilizon.Events.Event do
|
||||
:title,
|
||||
:begins_on,
|
||||
:organizer_actor_id,
|
||||
:category_id,
|
||||
:category,
|
||||
:url,
|
||||
:uuid
|
||||
])
|
||||
|
||||
@@ -27,7 +27,6 @@ defmodule Mobilizon.Events do
|
||||
order_by: [desc: :id],
|
||||
preload: [
|
||||
:organizer_actor,
|
||||
:category,
|
||||
:sessions,
|
||||
:tracks,
|
||||
:tags,
|
||||
@@ -145,7 +144,6 @@ defmodule Mobilizon.Events do
|
||||
where: e.uuid == ^uuid and e.visibility in [^:public, ^:unlisted],
|
||||
preload: [
|
||||
:organizer_actor,
|
||||
:category,
|
||||
:sessions,
|
||||
:tracks,
|
||||
:tags,
|
||||
@@ -164,7 +162,6 @@ defmodule Mobilizon.Events do
|
||||
|
||||
Repo.preload(event, [
|
||||
:organizer_actor,
|
||||
:category,
|
||||
:sessions,
|
||||
:tracks,
|
||||
:tags,
|
||||
@@ -182,7 +179,6 @@ defmodule Mobilizon.Events do
|
||||
where: e.url == ^url and e.visibility in [^:public, ^:unlisted],
|
||||
preload: [
|
||||
:organizer_actor,
|
||||
:category,
|
||||
:sessions,
|
||||
:tracks,
|
||||
:tags,
|
||||
@@ -205,7 +201,6 @@ defmodule Mobilizon.Events do
|
||||
where: e.url == ^url and e.visibility in [^:public, ^:unlisted],
|
||||
preload: [
|
||||
:organizer_actor,
|
||||
:category,
|
||||
:sessions,
|
||||
:tracks,
|
||||
:tags,
|
||||
@@ -350,110 +345,6 @@ defmodule Mobilizon.Events do
|
||||
Event.changeset(event, %{})
|
||||
end
|
||||
|
||||
alias Mobilizon.Events.Category
|
||||
|
||||
@doc """
|
||||
Returns the list of categories.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_categories()
|
||||
[%Category{}, ...]
|
||||
|
||||
"""
|
||||
def list_categories(page \\ nil, limit \\ nil) do
|
||||
Repo.all(
|
||||
Category
|
||||
|> paginate(page, limit)
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single category.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Category does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_category!(123)
|
||||
%Category{}
|
||||
|
||||
iex> get_category!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_category!(id), do: Repo.get!(Category, id)
|
||||
|
||||
@spec get_category_by_title(String.t()) :: Category.t() | nil
|
||||
def get_category_by_title(title) when is_binary(title) do
|
||||
Repo.get_by(Category, title: title)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a category.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_category(%{field: value})
|
||||
{:ok, %Category{}}
|
||||
|
||||
iex> create_category(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_category(attrs \\ %{}) do
|
||||
%Category{}
|
||||
|> Category.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a category.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_category(category, %{field: new_value})
|
||||
{:ok, %Category{}}
|
||||
|
||||
iex> update_category(category, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_category(%Category{} = category, attrs) do
|
||||
category
|
||||
|> Category.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a Category.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_category(category)
|
||||
{:ok, %Category{}}
|
||||
|
||||
iex> delete_category(category)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_category(%Category{} = category) do
|
||||
Repo.delete(category)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking category changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_category(category)
|
||||
%Ecto.Changeset{source: %Category{}}
|
||||
|
||||
"""
|
||||
def change_category(%Category{} = category) do
|
||||
Category.changeset(category, %{})
|
||||
end
|
||||
|
||||
alias Mobilizon.Events.Tag
|
||||
|
||||
@doc """
|
||||
|
||||
Reference in New Issue
Block a user