Skip to content

Commit

Permalink
Avoid needlessly loading idea & idea_label
Browse files Browse the repository at this point in the history
* idea is already part of the state of the component (and we only
need id and brainstorming_id)
* idea_label we only need the id anyhow
  • Loading branch information
PragTob committed Dec 23, 2024
1 parent ce34983 commit 95a8781
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 28 deletions.
8 changes: 4 additions & 4 deletions lib/mindwendel/idea_labels.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,22 @@ 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()

Lanes.broadcast_lanes_update(idea.brainstorming_id)
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()

Expand Down
12 changes: 2 additions & 10 deletions lib/mindwendel_web/live/idea_live/card_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions lib/mindwendel_web/live/idea_live/card_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -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}
>
Expand All @@ -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}
>
Expand Down
2 changes: 1 addition & 1 deletion test/mindwendel/brainstormings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
22 changes: 11 additions & 11 deletions test/mindwendel/idea_labels_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 95a8781

Please sign in to comment.