2020-05-08 03:43:02 +00:00
|
|
|
defmodule Planningpoker.Db do
|
|
|
|
require Logger
|
|
|
|
use GenServer
|
|
|
|
|
|
|
|
def start_link(_) do
|
|
|
|
GenServer.start_link(__MODULE__, [], name: __MODULE__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def init(_) do
|
|
|
|
:ets.new(:users, [:named_table, :public])
|
2020-05-08 04:34:42 +00:00
|
|
|
:ets.new(:votes, [:named_table, :public])
|
2020-05-08 03:43:02 +00:00
|
|
|
{:ok, %{}}
|
|
|
|
end
|
|
|
|
|
|
|
|
def save_name(user, name) do
|
|
|
|
Logger.debug("Storing user name (#{user} -> #{name})")
|
|
|
|
:ets.insert(:users, {user, name})
|
|
|
|
end
|
|
|
|
|
2020-05-08 04:34:42 +00:00
|
|
|
def save_vote(user, room, value) do
|
|
|
|
Logger.debug("Storing vote of #{value} for player #{user} in room #{room}")
|
|
|
|
:ets.insert(:votes, {{user, room}, value})
|
|
|
|
end
|
|
|
|
|
2020-05-08 03:43:02 +00:00
|
|
|
def get_users(keys) do
|
2020-05-08 04:34:42 +00:00
|
|
|
match = for key <- keys do
|
|
|
|
{{key, :_}, [], [:"$_"]}
|
|
|
|
end
|
|
|
|
Logger.debug("Getting users: #{inspect match}")
|
|
|
|
:ets.select(:users, match)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_votes(users, room) do
|
|
|
|
match = for user <- users do
|
|
|
|
{{{user, room}, :_}, [], [:"$_"]}
|
|
|
|
end
|
|
|
|
Logger.debug("Getting votes: #{inspect match}")
|
|
|
|
:ets.select(:votes, match)
|
2020-05-08 03:43:02 +00:00
|
|
|
end
|
|
|
|
end
|