Skip to content

Commit

Permalink
feat: refactor matches
Browse files Browse the repository at this point in the history
  • Loading branch information
zvonimirr committed Jun 16, 2024
1 parent ce6d045 commit 549aae7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 40 deletions.
23 changes: 23 additions & 0 deletions lib/swipex/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@ defmodule Swipex.User do
end
end

def get_matches(id) do
conn = Bolt.Sips.conn()

with {:ok, %Bolt.Sips.Response{results: results}} <-
Bolt.Sips.query(
conn,
"""
MATCH (u2:User {id: $id})
MATCH (u:User)
WHERE (u2)-[:LIKES]->(u) AND (u)-[:LIKES]->(u2)
AND u <> u2
RETURN u
""",
%{id: id}
),
results <- Enum.map(results, &Map.get(&1, "u")),
matches <- Enum.map(results, &Map.get(&1, :properties)) do
matches
else
_ -> []
end
end

def has_matched(id, match_id) do
conn = Bolt.Sips.conn()

Expand Down
41 changes: 1 addition & 40 deletions lib/swipex_web/live/profile_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule SwipexWeb.ProfileLive do
assigns =
assign(assigns, :potential_match, Swipex.User.get_potential_match(assigns.user["id"]))

assigns = assign(assigns, :matches, get_matches(assigns.user["id"]))
assigns = assign(assigns, :matches, Swipex.User.get_matches(assigns.user["id"]))

~H"""
<div class="flex gap-4 justify-between w-full mb-3">
Expand Down Expand Up @@ -111,43 +111,4 @@ defmodule SwipexWeb.ProfileLive do
</div>
"""
end

defp get_matches(id) do
conn = Bolt.Sips.conn()

# Get all users that like the current user
Bolt.Sips.query!(
conn,
"""
MATCH (u2:User {id: $id})
MATCH (u:User)
WHERE (u2)-[:LIKES]->(u) AND (u)-[:LIKES]->(u2)
AND u <> u2
RETURN u
""",
%{id: id}
)
|> Map.get(:results)
|> Enum.map(&Map.get(&1, "u"))
|> Enum.map(&get_user_from_response/1)
end

defp get_user_from_response(%Bolt.Sips.Types.Node{} = node) do
node
|> Map.get(:properties)
|> Map.new(fn {k, v} -> {String.to_atom(k), v} end)
end

defp get_user_from_response(response) do
case response
|> Bolt.Sips.Response.first() do
nil ->
%{}

user ->
user
|> Map.get("u")
|> get_user_from_response()
end
end
end

0 comments on commit 549aae7

Please sign in to comment.