From 293a6c1cdca402335fc286fbe6809eaf0d41717c Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Tue, 12 Nov 2024 22:28:02 +0100 Subject: [PATCH] only update idea card when like is added instead if broadcasting lane updates --- lib/mindwendel/likes.ex | 26 ++++++-------------------- test/mindwendel/likes_test.exs | 9 ++++++--- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/lib/mindwendel/likes.ex b/lib/mindwendel/likes.ex index 2e2ce45c..f1f8a515 100644 --- a/lib/mindwendel/likes.ex +++ b/lib/mindwendel/likes.ex @@ -6,28 +6,14 @@ defmodule Mindwendel.Likes do import Ecto.Query, warn: false alias Mindwendel.Repo alias Mindwendel.Ideas - alias Mindwendel.Lanes + alias Mindwendel.Brainstormings alias Mindwendel.Brainstormings.Like require Logger @doc """ - Returns a Boolean if a like for the given idea and user exists. - - TODO something is strange here as this was not working for the card component?? - - ## Examples - - iex> exists_like_for_idea?(1, 2) - true - - """ - def exists_like_for_idea?(idea_id, user_id) do - Repo.exists?(from like in Like, where: like.user_id == ^user_id and like.idea_id == ^idea_id) - end - - @doc """ - Returns a Boolean if a user id like exists in the given likes + Returns a boolean if like with a given user id exists in the given likes. + This method is primarily used with preloaded data from an idea, therefore it is not needed to reload data from the repo. ## Examples @@ -36,7 +22,7 @@ defmodule Mindwendel.Likes do """ def exists_user_in_likes?(likes, user_id) do - Enum.map(likes, fn like -> like.user_id end) |> Enum.member?(user_id) + likes |> Enum.map(fn like -> like.user_id end) |> Enum.member?(user_id) end @doc """ @@ -56,7 +42,7 @@ defmodule Mindwendel.Likes do case status do :ok -> - {:ok, Lanes.broadcast_lanes_update(Ideas.get_idea!(idea_id).brainstorming_id)} + {:ok, Brainstormings.broadcast({:ok, Ideas.get_idea!(idea_id)}, :idea_updated)} :error -> {:error, result} @@ -78,7 +64,7 @@ defmodule Mindwendel.Likes do from like in Like, where: like.user_id == ^user_id and like.idea_id == ^idea_id ) - Lanes.broadcast_lanes_update(Ideas.get_idea!(idea_id).brainstorming_id) + Brainstormings.broadcast({:ok, Ideas.get_idea!(idea_id)}, :idea_updated) end @doc """ diff --git a/test/mindwendel/likes_test.exs b/test/mindwendel/likes_test.exs index 883ececb..be39a907 100644 --- a/test/mindwendel/likes_test.exs +++ b/test/mindwendel/likes_test.exs @@ -2,6 +2,7 @@ defmodule Mindwendel.LikesTest do use Mindwendel.DataCase, async: true alias Mindwendel.Factory alias Mindwendel.Likes + alias Mindwendel.Brainstormings.Like setup do user = Factory.insert!(:user) @@ -16,14 +17,16 @@ defmodule Mindwendel.LikesTest do } end - describe "exists_like_for_idea?" do + describe "exists_user_in_likes?" do test "returns true if like is given", %{idea: idea, user: user} do Factory.insert!(:like, %{idea_id: idea.id, user_id: user.id}) - assert Likes.exists_like_for_idea?(idea.id, user.id) == true + likes = Repo.all(from like in Like, where: like.idea_id == ^idea.id) + assert Likes.exists_user_in_likes?(likes, user.id) == true end test "returns false if like is not given", %{idea: idea, user: user} do - assert Likes.exists_like_for_idea?(idea.id, user.id) == false + likes = Repo.all(from like in Like, where: like.idea_id == ^idea.id) + assert Likes.exists_user_in_likes?(likes, user.id) == false end end