diff --git a/lib/sing_for_needs/performances/performance.ex b/lib/sing_for_needs/performances/performance.ex index 90045f8..d521390 100644 --- a/lib/sing_for_needs/performances/performance.ex +++ b/lib/sing_for_needs/performances/performance.ex @@ -32,7 +32,11 @@ defmodule SingForNeeds.Performances.Performance do if attrs[:artists] do performance = put_assoc(performance, :artists, attrs[:artists]) else - performance + if attrs[:cause_id] do + performance = build_cause_assoc(performance, attrs[:cause_id]) + else + performance + end end # cond attrs do @@ -52,15 +56,10 @@ defmodule SingForNeeds.Performances.Performance do @doc """ change set insert performance with artist (while creating the relationship) """ - def changeset_update_artists(performance, attrs) do - performance - |> cast(attrs, [:title, :description]) - |> put_assoc(:artists, attrs.artists) - end defp build_cause_assoc(performance, cause_id) do - cause = Causes.get_cause(cause_id) - Ecto.build_assoc(performance, cause, :performances) + cause = Causes.get_cause!(cause_id) + Ecto.build_assoc(cause, :performances, performance) end defp put_artists_assoc(performance, artist_ids) do diff --git a/test/sing_for_needs/performances/performances_test.exs b/test/sing_for_needs/performances/performances_test.exs index 6dd78b6..5e6707b 100644 --- a/test/sing_for_needs/performances/performances_test.exs +++ b/test/sing_for_needs/performances/performances_test.exs @@ -34,6 +34,27 @@ defmodule SingForNeeds.PerformancesTest do assert performance.performance_date == ~D[2020-01-01] end + test "create_performance/1 creates a performance with artists" do + artists = insert_list(2, :artist) + valid_attrs_with_artists = Map.put(@valid_attrs, :artists, artists) + + assert {:ok, %Performance{} = performance} = + Performances.create_performance(valid_attrs_with_artists) + + assert performance.artists == artists + end + + test "create_performance/1 creates a performance with a related cause" do + cause = insert(:cause) + valid_attrs_with_cause_id = Map.put(@valid_attrs, :cause_id, cause.id) + + assert {:ok, %Performance{} = performance} = + Performances.create_performance(valid_attrs_with_cause_id) + + assert performance.title == valid_attrs_with_cause_id.title + assert performance.cause_id == cause.id + end + @doc """ list_performence/0 gets list of all the performences in the database """