Skip to content

Commit

Permalink
Merge bca95a5 into 2c4af3d
Browse files Browse the repository at this point in the history
  • Loading branch information
aonomike committed Jan 8, 2020
2 parents 2c4af3d + bca95a5 commit cc83bc3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
15 changes: 7 additions & 8 deletions lib/sing_for_needs/performances/performance.ex
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
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

0 comments on commit cc83bc3

Please sign in to comment.