From 9df9a795c740f8b7cdac4786ffd8f5aee4415a1a Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 15 Jan 2018 11:50:26 +0100 Subject: [PATCH 1/3] Add state and fix casting Signed-off-by: Thomas Citharel --- lib/eventos/events/event.ex | 3 ++- priv/repo/migrations/20180110092600_create_events.exs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/eventos/events/event.ex b/lib/eventos/events/event.ex index dea3a0d64..b517f2f65 100644 --- a/lib/eventos/events/event.ex +++ b/lib/eventos/events/event.ex @@ -44,6 +44,7 @@ defmodule Eventos.Events.Event do field :geom, Geo.Geometry field :slug, TitleSlug.Type field :state, :integer, default: 0 + field :status, :integer, default: 0 field :public, :boolean, default: true field :thumbnail, :string field :large_image, :string @@ -61,7 +62,7 @@ defmodule Eventos.Events.Event do @doc false def changeset(%Event{} = event, attrs) do event - |> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_id]) + |> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_id, :state, :geom, :status, :public, :thumbnail, :large_image, :publish_at]) |> validate_required([:title, :description, :begins_on, :ends_on, :organizer_id]) |> TitleSlug.maybe_generate_slug() |> TitleSlug.unique_constraint() diff --git a/priv/repo/migrations/20180110092600_create_events.exs b/priv/repo/migrations/20180110092600_create_events.exs index cb5ae738a..f5f3fcd48 100644 --- a/priv/repo/migrations/20180110092600_create_events.exs +++ b/priv/repo/migrations/20180110092600_create_events.exs @@ -11,6 +11,7 @@ defmodule Eventos.Repo.Migrations.CreateEvents do add :geom, :geometry add :state, :integer, null: false add :public, :boolean, null: false + add :status, :integer, null: false add :large_image, :string add :thumbnail, :string add :publish_at, :datetimetz From 8b4d1ab4e49064936d2c8c4839b24436044ccfbb Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 15 Jan 2018 12:04:09 +0100 Subject: [PATCH 2/3] Add categories properly Signed-off-by: Thomas Citharel --- lib/eventos/events/event.ex | 7 ++++--- ...s.exs => 20180110091747_create_categories.exs} | 0 .../migrations/20180110092600_create_events.exs | 1 + test/eventos/events/events_test.exs | 15 ++++++--------- .../controllers/event_controller_test.exs | 3 +++ test/support/factory.ex | 10 +++++++++- 6 files changed, 23 insertions(+), 13 deletions(-) rename priv/repo/migrations/{20180110093747_create_categories.exs => 20180110091747_create_categories.exs} (100%) diff --git a/lib/eventos/events/event.ex b/lib/eventos/events/event.ex index b517f2f65..ca10113ec 100644 --- a/lib/eventos/events/event.ex +++ b/lib/eventos/events/event.ex @@ -32,7 +32,7 @@ defmodule Eventos.Events.Event do """ use Ecto.Schema import Ecto.Changeset - alias Eventos.Events.{Event, Participant, Request, Tag, Session, Track} + alias Eventos.Events.{Event, Participant, Request, Tag, Category, Session, Track} alias Eventos.Events.Event.TitleSlug alias Eventos.Accounts.Account @@ -51,6 +51,7 @@ defmodule Eventos.Events.Event do field :publish_at, Timex.Ecto.DateTimeWithTimezone belongs_to :organizer, Account, [foreign_key: :organizer_id] has_many :tags, Tag + belongs_to :category, Category many_to_many :participants, Account, join_through: Participant has_many :event_request, Request has_many :tracks, Track @@ -62,8 +63,8 @@ defmodule Eventos.Events.Event do @doc false def changeset(%Event{} = event, attrs) do event - |> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_id, :state, :geom, :status, :public, :thumbnail, :large_image, :publish_at]) - |> validate_required([:title, :description, :begins_on, :ends_on, :organizer_id]) + |> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_id, :category_id, :state, :geom, :status, :public, :thumbnail, :large_image, :publish_at]) + |> validate_required([:title, :description, :begins_on, :ends_on, :organizer_id, :category_id]) |> TitleSlug.maybe_generate_slug() |> TitleSlug.unique_constraint() end diff --git a/priv/repo/migrations/20180110093747_create_categories.exs b/priv/repo/migrations/20180110091747_create_categories.exs similarity index 100% rename from priv/repo/migrations/20180110093747_create_categories.exs rename to priv/repo/migrations/20180110091747_create_categories.exs diff --git a/priv/repo/migrations/20180110092600_create_events.exs b/priv/repo/migrations/20180110092600_create_events.exs index f5f3fcd48..4b5dd2946 100644 --- a/priv/repo/migrations/20180110092600_create_events.exs +++ b/priv/repo/migrations/20180110092600_create_events.exs @@ -16,6 +16,7 @@ defmodule Eventos.Repo.Migrations.CreateEvents do add :thumbnail, :string add :publish_at, :datetimetz add :organizer_id, references(:accounts, on_delete: :nothing), null: false + add :category_id, references(:categories, on_delete: :nothing), null: false timestamps() end diff --git a/test/eventos/events/events_test.exs b/test/eventos/events/events_test.exs index 6ca644303..894bc4d5a 100644 --- a/test/eventos/events/events_test.exs +++ b/test/eventos/events/events_test.exs @@ -17,6 +17,10 @@ defmodule Eventos.EventsTest do insert(:event, organizer: account_fixture()) end + def category_fixture do + insert(:category) + end + describe "events" do alias Eventos.Events.Event @@ -37,7 +41,9 @@ defmodule Eventos.EventsTest do test "create_event/1 with valid data creates a event" do {:ok, account} = Accounts.create_account(@account_valid_attrs) + category = category_fixture() valid_attrs_with_account_id = Map.put(@event_valid_attrs, :organizer_id, account.id) + valid_attrs_with_account_id = Map.put(valid_attrs_with_account_id, :category_id, category.id) assert {:ok, %Event{} = event} = Events.create_event(valid_attrs_with_account_id) assert event.begins_on == DateTime.from_naive!(~N[2010-04-17 14:00:00.000000Z], "Etc/UTC") assert event.description == "some description" @@ -146,15 +152,6 @@ defmodule Eventos.EventsTest do @update_attrs %{description: "some updated description", picture: "some updated picture", title: "some updated title"} @invalid_attrs %{description: nil, picture: nil, title: nil} - def category_fixture(attrs \\ %{}) do - {:ok, category} = - attrs - |> Enum.into(@valid_attrs) - |> Events.create_category() - - category - end - test "list_categories/0 returns all categories" do category = category_fixture() assert Events.list_categories() == [category] diff --git a/test/eventos_web/controllers/event_controller_test.exs b/test/eventos_web/controllers/event_controller_test.exs index 1a7250dda..370469d54 100644 --- a/test/eventos_web/controllers/event_controller_test.exs +++ b/test/eventos_web/controllers/event_controller_test.exs @@ -30,6 +30,9 @@ defmodule EventosWeb.EventControllerTest do describe "create event" do test "renders event when data is valid", %{conn: conn, user: user} do attrs = Map.put(@create_attrs, :organizer_id, user.account.id) + + category = insert(:category) + attrs = Map.put(attrs, :category_id, category.id) conn = auth_conn(conn, user) conn = post conn, event_path(conn, :create), event: attrs assert %{"id" => id} = json_response(conn, 201)["data"] diff --git a/test/support/factory.ex b/test/support/factory.ex index 33f963349..21e0927c7 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -25,6 +25,13 @@ defmodule Eventos.Factory do } end + def category_factory do + %Eventos.Events.Category{ + title: sequence("MyCategory"), + description: "My category desc" + } + end + def event_factory do %Eventos.Events.Event{ title: sequence("MyEvent"), @@ -32,7 +39,8 @@ defmodule Eventos.Factory do description: "My desc", begins_on: nil, ends_on: nil, - organizer: build(:account) + organizer: build(:account), + category: build(:category) } end From 32bd50d161a45893c4296022d7e2150a5b5a297d Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 15 Jan 2018 12:17:34 +0100 Subject: [PATCH 3/3] fix tags Signed-off-by: Thomas Citharel --- lib/eventos/events/event.ex | 3 ++- .../migrations/20180110093906_create_events_tags.exs | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 priv/repo/migrations/20180110093906_create_events_tags.exs diff --git a/lib/eventos/events/event.ex b/lib/eventos/events/event.ex index ca10113ec..c1249ddbd 100644 --- a/lib/eventos/events/event.ex +++ b/lib/eventos/events/event.ex @@ -50,7 +50,7 @@ defmodule Eventos.Events.Event do field :large_image, :string field :publish_at, Timex.Ecto.DateTimeWithTimezone belongs_to :organizer, Account, [foreign_key: :organizer_id] - has_many :tags, Tag + many_to_many :tags, Tag, join_through: "events_tags" belongs_to :category, Category many_to_many :participants, Account, join_through: Participant has_many :event_request, Request @@ -64,6 +64,7 @@ defmodule Eventos.Events.Event do def changeset(%Event{} = event, attrs) do event |> cast(attrs, [:title, :description, :begins_on, :ends_on, :organizer_id, :category_id, :state, :geom, :status, :public, :thumbnail, :large_image, :publish_at]) + |> cast_assoc(:tags) |> validate_required([:title, :description, :begins_on, :ends_on, :organizer_id, :category_id]) |> TitleSlug.maybe_generate_slug() |> TitleSlug.unique_constraint() diff --git a/priv/repo/migrations/20180110093906_create_events_tags.exs b/priv/repo/migrations/20180110093906_create_events_tags.exs new file mode 100644 index 000000000..ee49c21a2 --- /dev/null +++ b/priv/repo/migrations/20180110093906_create_events_tags.exs @@ -0,0 +1,10 @@ +defmodule Eventos.Repo.Migrations.CreateEventsTags do + use Ecto.Migration + + def change do + create table(:events_tags, primary_key: false) do + add :event_id, references(:events) + add :tag_id, references(:tags) + end + end +end