<commit_before>defmodule RoomUserStatesApiRouter do
  use Dynamo.Router
  use Ecto.Query
  import Cheko.RouterUtils

  prepare do
    authenticate_user!(conn)
  end

  get "/" do
    user_id = get_session(conn, :user_id)
    rooms = Repo.all(Room)

    room_user_states_attributes = Enum.map rooms, fn(room)->
      query = from r in RoomUserState, where: r.user_id == ^user_id and r.room_id == ^room.id
      result = Repo.all query
      if length(result) == 0 do
        room_user_state = RoomUserState.new(user_id: user_id, room_id: room.id, joined: false)
        room_user_state = Repo.create(room_user_state)
        RoomUserState.public_attributes(room_user_state) ++ [room: Room.public_attributes(room)]
      else
        [room_user_state|_] = result
        RoomUserState.public_attributes(room_user_state) ++ [room: Room.public_attributes(room)]
      end
    end

    json_response([room_user_states: room_user_states_attributes], conn)
  end


  put "/:room_user_state_id" do
    room_user_state_id = conn.params[:room_user_state_id]
    user_id = get_session(conn, :user_id)

    params = json_decode conn.req_body

    room_user_state_params = whitelist_params(params["room_user_state"], ["joined"])
    query = from r in RoomUserState, where: r.id == ^room_user_state_id and r.user_id == ^user_id
    room_user_state = Repo.get(RoomUserState, room_user_state_id)
    new_room_user_state = room_user_state.update(room_user_state_params)

    case RoomUserState.validate(new_room_user_state) do
      [] ->
        :ok = Repo.update(new_room_user_state)
        json_response [user: RoomUserState.public_attributes(new_room_user_state)], conn
      errors ->
        json_response [errors: errors], conn, 422
    end
  end

end<commit_msg>Query room user state which matches the user<commit_after>defmodule RoomUserStatesApiRouter do
  use Dynamo.Router
  use Ecto.Query
  import Cheko.RouterUtils

  prepare do
    authenticate_user!(conn)
  end

  get "/" do
    user_id = get_session(conn, :user_id)
    rooms = Repo.all(Room)

    room_user_states_attributes = Enum.map rooms, fn(room)->
      query = from r in RoomUserState, where: r.user_id == ^user_id and r.room_id == ^room.id
      result = Repo.all query
      if length(result) == 0 do
        room_user_state = RoomUserState.new(user_id: user_id, room_id: room.id, joined: false)
        room_user_state = Repo.create(room_user_state)
        RoomUserState.public_attributes(room_user_state) ++ [room: Room.public_attributes(room)]
      else
        [room_user_state|_] = result
        RoomUserState.public_attributes(room_user_state) ++ [room: Room.public_attributes(room)]
      end
    end

    json_response([room_user_states: room_user_states_attributes], conn)
  end


  put "/:room_user_state_id" do
    room_user_state_id = binary_to_integer(conn.params[:room_user_state_id])
    user_id = get_session(conn, :user_id)
    params = json_decode conn.req_body

    room_user_state_params = whitelist_params(params["room_user_state"], ["joined"])
    query = from r in RoomUserState,
      where: r.id == ^room_user_state_id and r.user_id == ^user_id

    [room_user_state] = Repo.all query
    new_room_user_state = room_user_state.update(room_user_state_params)

    case RoomUserState.validate(new_room_user_state) do
      [] ->
        :ok = Repo.update(new_room_user_state)
        json_response [user: RoomUserState.public_attributes(new_room_user_state)], conn
      errors ->
        json_response [errors: errors], conn, 422
    end
  end

end
{'fraction_non_alphanumeric': 0.09414758269720101, 'fraction_numerical': 0.0022618037885213456, 'mean_word_length': 3.7681940700808627, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5439944', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1177, 'n_words': 234}