diff --git a/lib/mindwendel/idea_labels.ex b/lib/mindwendel/idea_labels.ex index d31fbdec..be4f479d 100644 --- a/lib/mindwendel/idea_labels.ex +++ b/lib/mindwendel/idea_labels.ex @@ -30,9 +30,9 @@ defmodule Mindwendel.IdeaLabels do # As the broadcast results in a full reload of the ideas, we don't need to actually update # the idea struct, a new association is enough - def add_idea_label_to_idea(%Idea{} = idea, %IdeaLabel{} = idea_label) do + def add_idea_label_to_idea(idea, idea_label_id) do result = - %{idea_id: idea.id, idea_label_id: idea_label.id} + %{idea_id: idea.id, idea_label_id: idea_label_id} |> IdeaIdeaLabel.bare_creation_changeset() |> Repo.insert() @@ -40,12 +40,12 @@ defmodule Mindwendel.IdeaLabels do result end - def remove_idea_label_from_idea(%Idea{} = idea, %IdeaLabel{} = idea_label) do + def remove_idea_label_from_idea(%Idea{} = idea, idea_label_id) do result = from(idea_idea_label in IdeaIdeaLabel, where: idea_idea_label.idea_id == ^idea.id and - idea_idea_label.idea_label_id == ^idea_label.id + idea_idea_label.idea_label_id == ^idea_label_id ) |> Repo.delete_all() diff --git a/lib/mindwendel_web/live/idea_live/card_component.ex b/lib/mindwendel_web/live/idea_live/card_component.ex index 1620f5b7..141a080b 100644 --- a/lib/mindwendel_web/live/idea_live/card_component.ex +++ b/lib/mindwendel_web/live/idea_live/card_component.ex @@ -34,30 +34,22 @@ defmodule MindwendelWeb.IdeaLive.CardComponent do def handle_event( "add_idea_label_to_idea", %{ - "idea-id" => idea_id, "idea-label-id" => idea_label_id }, socket ) do - idea = Ideas.get_idea!(idea_id) - idea_label = IdeaLabels.get_idea_label(idea_label_id) - - IdeaLabels.add_idea_label_to_idea(idea, idea_label) + IdeaLabels.add_idea_label_to_idea(socket.assigns.idea, idea_label_id) {:noreply, socket} end def handle_event( "remove_idea_label_from_idea", %{ - "idea-id" => idea_id, "idea-label-id" => idea_label_id }, socket ) do - idea = Ideas.get_idea!(idea_id) - idea_label = IdeaLabels.get_idea_label(idea_label_id) - - IdeaLabels.remove_idea_label_from_idea(idea, idea_label) + IdeaLabels.remove_idea_label_from_idea(socket.assigns.idea, idea_label_id) {:noreply, socket} end end diff --git a/lib/mindwendel_web/live/idea_live/card_component.html.heex b/lib/mindwendel_web/live/idea_live/card_component.html.heex index 07a84bee..813ad04d 100644 --- a/lib/mindwendel_web/live/idea_live/card_component.html.heex +++ b/lib/mindwendel_web/live/idea_live/card_component.html.heex @@ -97,7 +97,6 @@ data-testid={brainstorming_idea_label.id} phx-click="add_idea_label_to_idea" phx-target={@myself} - phx-value-idea-id={@idea.id} title={"Label #{brainstorming_idea_label.name}"} phx-value-idea-label-id={brainstorming_idea_label.id} > @@ -116,7 +115,6 @@ data-testid={brainstorming_idea_label.id} phx-click="remove_idea_label_from_idea" phx-target={@myself} - phx-value-idea-id={@idea.id} title={"Label #{brainstorming_idea_label.name}"} phx-value-idea-label-id={brainstorming_idea_label.id} > diff --git a/test/mindwendel/brainstormings_test.exs b/test/mindwendel/brainstormings_test.exs index 6d1f38f6..174eacef 100644 --- a/test/mindwendel/brainstormings_test.exs +++ b/test/mindwendel/brainstormings_test.exs @@ -323,7 +323,7 @@ defmodule Mindwendel.BrainstormingsTest do like = Factory.insert!(:like, idea: idea) {:ok, _idea_idea_label} = - IdeaLabels.add_idea_label_to_idea(idea, Enum.at(brainstorming.labels, 0)) + IdeaLabels.add_idea_label_to_idea(idea, Enum.at(brainstorming.labels, 0).id) idea = idea |> Repo.preload([:idea_labels]) diff --git a/test/mindwendel/idea_labels_test.exs b/test/mindwendel/idea_labels_test.exs index ed9c3b33..075cefe5 100644 --- a/test/mindwendel/idea_labels_test.exs +++ b/test/mindwendel/idea_labels_test.exs @@ -18,14 +18,14 @@ defmodule Mindwendel.IdeaLabelsTest do describe "#add_idea_label_to_idea" do test "adds IdeaLabel to Idea", %{idea_label: idea_label, idea: idea} do - {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label) + {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label.id) assert [idea_label] == labels_of(idea) assert Repo.count(IdeaIdeaLabel) == 1 end test "creates one IdeaIdeaLabel", %{idea_label: idea_label, idea: idea} do - {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label) + {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label.id) assert Repo.count(IdeaIdeaLabel) == 1 @@ -37,7 +37,7 @@ defmodule Mindwendel.IdeaLabelsTest do test "does not create additional IdeaLabel", %{idea_label: idea_label, idea: idea} do assert Repo.count(IdeaLabel) == 1 - {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label) + {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label.id) assert Repo.count(IdeaLabel) == 1 assert Repo.one(IdeaLabel) == idea_label @@ -47,10 +47,10 @@ defmodule Mindwendel.IdeaLabelsTest do test "does not add the same IdeaLabel twice to Idea", %{idea_label: idea_label, idea: idea} do # Calling this method twice does not fail and does not create duplicates {:ok, idea_idea_label_after_method_call_1} = - IdeaLabels.add_idea_label_to_idea(idea, idea_label) + IdeaLabels.add_idea_label_to_idea(idea, idea_label.id) {:ok, idea_idea_label_after_method_call_2} = - IdeaLabels.add_idea_label_to_idea(idea, idea_label) + IdeaLabels.add_idea_label_to_idea(idea, idea_label.id) # There should still be only one IdeaIdeaLabel assert Repo.count(IdeaIdeaLabel) == 1 @@ -71,7 +71,7 @@ defmodule Mindwendel.IdeaLabelsTest do }) {:error, _changeset} = - IdeaLabels.add_idea_label_to_idea(idea, idea_label_from_another_brainstorming) + IdeaLabels.add_idea_label_to_idea(idea, idea_label_from_another_brainstorming.id) end @tag :skip @@ -95,7 +95,7 @@ defmodule Mindwendel.IdeaLabelsTest do idea = Repo.preload(idea, [:idea_labels, :idea_idea_labels]) idea_label = idea.brainstorming.labels |> Enum.at(0) - IdeaLabels.add_idea_label_to_idea(idea, idea_label) + IdeaLabels.add_idea_label_to_idea(idea, idea_label.id) assert Repo.count(IdeaIdeaLabel) == 1 assert Repo.one(IdeaIdeaLabel).idea_id == idea.id @@ -113,14 +113,14 @@ defmodule Mindwendel.IdeaLabelsTest do describe "#delete_idea_label_from_idea" do setup %{idea_label: idea_label, idea: idea} do - {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label) + {:ok, _idea_idea_label} = IdeaLabels.add_idea_label_to_idea(idea, idea_label.id) idea = idea |> Repo.reload!() |> Repo.preload(:idea_labels) assert Repo.count(IdeaIdeaLabel) == 1 %{idea: idea} end test "removes successfully IdeaLabel from Idea", %{idea_label: idea_label, idea: idea} do - IdeaLabels.remove_idea_label_from_idea(idea, idea_label) + IdeaLabels.remove_idea_label_from_idea(idea, idea_label.id) assert Enum.empty?(Repo.all(IdeaIdeaLabel)) end @@ -129,8 +129,8 @@ defmodule Mindwendel.IdeaLabelsTest do idea: idea } do # Calling this method twice does not fail - IdeaLabels.remove_idea_label_from_idea(idea, idea_label) - IdeaLabels.remove_idea_label_from_idea(idea, idea_label) + IdeaLabels.remove_idea_label_from_idea(idea, idea_label.id) + IdeaLabels.remove_idea_label_from_idea(idea, idea_label.id) assert Enum.empty?(Repo.all(IdeaIdeaLabel)) end