Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions lib/sing_for_needs/performances/performance.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions test/sing_for_needs/performances/performances_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down