Skip to content

Commit

Permalink
only update idea card when like is added instead if broadcasting lane…
Browse files Browse the repository at this point in the history
… updates
  • Loading branch information
JannikStreek committed Nov 12, 2024
1 parent cfc481d commit 293a6c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
26 changes: 6 additions & 20 deletions lib/mindwendel/likes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 """
Expand All @@ -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}
Expand All @@ -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 """
Expand Down
9 changes: 6 additions & 3 deletions test/mindwendel/likes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 293a6c1

Please sign in to comment.