Not able to retrive proper data

I have made a function that returns data for required users from the database, I have made an application that is similar to Tinder where the user comes, sees, and swipes their profile, now the problem I am facing is,
the one user-4714(Test) swipe right to user-4713(Test1) Nothing happens all work, now when the user-4713(Test1) swipe right to user-4714(Test) now the second user(test1) able to see the first user(test)
but first user(test1) not able to see the second user(Test),
I have a table in the database which is to store matches between users, in which data is stored as

ID            user_one_id      user_one_id       
6578	4713	           4714	

The below code is from my Matched_match.ex file,

def list_matches_without_conversations(user_id) do
        queryString = "SELECT * FROM (" <>
                        "SELECT m.*, count(cm.id) AS messages_count FROM matches AS m " <>
                        "LEFT JOIN chat_messages AS cm ON ((sender_id = user_one_id AND receiver_id = user_two_id) OR (sender_id = user_two_id AND receiver_id = user_one_id)) " <>
                        "GROUP BY m.id " <>
                        "ORDER BY m.inserted_at DESC" <>
                      ") AS matches " <>
                      "WHERE messages_count = 0 "<>
                      "AND (user_one_id = $1 OR user_two_id = $1) "<>
                      "AND NOT (user_one_id = ANY($2::integer[])) " <>
                      "AND NOT (user_two_id = ANY($2::integer[]))"


        blocked_ids = WS2.Accounts.get_blocked_user_ids(user_id)
        blocked_ids = blocked_ids ++ [-1]
        result = WS2.Repo.query!(queryString, [user_id, blocked_ids])


  Enum.map(result.rows, fn(e) -> Repo.load(WS2.Matches.Match, {result.columns, e}) end)
        |> preload_base_match()
        |> Enum.filter(fn (m) -> m.user_one_id != m.user_two_id end)

        |> Enum.map(fn (m) ->
          user = [m.user_one, m.user_two]
          |> Enum.find(&(&1.id != user_id))
            Map.put(m ,:user_one, user)

        end)

       end

My match_controller.ex file code is:

  def list(conn, _) do
    user = Guardian.Plug.current_resource(conn)
    matches = Matches.list_matches_without_conversations(user.id)
    render(conn, "list.json", matches: matches)
  end

now when I login from user-4713(Test1) then I am able to see user-4714(Test) but when I login from user-4714(Test) I am not able to see user-4713(Test1) .

as I need information of user-4713(Test1) when I login from user-4714(Test) account but I got the information about user-4714(Test) user.

@hubertlepicki @kokolegorille @BartOtten Could please Help me here? If you have any questions please feel free to ask.

DJ, please stop “sliding into my DMs” here, on Twitter, or anywhere else, I am afraid I don’t have time to assist you directly with your project so please stop asking.

Ref. the question above, you’re not getting responses because the way you formulated the question is not clear, but also probably you’re doing something very strange, with raw SQL queries and with then filtering stuff in Elixir and at least for me it looks like you should go back to learning the basics on how to use Ecto and Phoenix.

4 Likes

:joy: it is hard to say who should have questions.

It kind of sounds like “please provide me guidance and ask the right questions, I only came here to say I have a problem and will not post more details unless somebody asks”. :confused:

He has been warned by the mods. Let me know if you get any further DMs.

1 Like

General tip: you can write a “not exists” query without COUNT and GROUP BY -

SELECT m.* FROM matches AS m
LEFT JOIN chat_messages AS cm on ((sender_id = user_one_id AND receiver_id = user_two_id) OR (sender_id = user_two_id AND receiver_id = user_one_id))
WHERE cm.id IS NULL

This will only return rows from matches that have no corresponding rows in chat_messages.


Something to think about regarding architecture: reading a relation like matches happens much much more often than writing that relation. Consider storing that data in a shape that’s easier to query; for instance, building two matches rows (one for each direction) and querying via one (user_one_id or user_two_id) key consistently instead of having to use OR all over the place.


As to your specific question it’s really hard to say. Readers of your post can’t execute the code to see intermediate values, and those values will depend on database data they don’t have. YOU need to provide that context in your post: what does result look like for a “good” vs “bad” scenario? What do intermediate values in that Enum pipeline look like?

2 Likes