1
0
mirror of https://github.com/mgerb/classic-wow-forums synced 2026-01-09 16:42:49 +00:00

server - update user with character info

This commit is contained in:
2018-01-11 20:24:55 -06:00
parent 991e1bfe5a
commit dc7149eb8b
7 changed files with 91 additions and 62 deletions

View File

@@ -45,7 +45,7 @@ defmodule MyApp.BattleNet.User do
defp parse_character_response({:ok, %HTTPoison.Response{body: body}}, user_id) do
case Poison.decode(body) do
{:ok, data} ->
Cachex.set(:myapp, "usr_char:#{user_id}", data, ttl: :timer.minutes(1)) # 1 minute
Cachex.set(:myapp, "usr_char:#{user_id}", data, ttl: :timer.minutes(10)) # 10 minutes
{:ok, data}
{:error, error} -> {:error, error}
end

View File

@@ -10,6 +10,11 @@ defmodule MyApp.Data.User do
field :battle_net_id, :integer
field :battletag, :string
field :permissions, :string, default: "user" # admin, mod, user
field :character_guild, :string
field :character_name, :string
field :character_class, :string
field :character_realm, :string
field :character_avatar, :string
timestamps()
end
@@ -20,6 +25,25 @@ defmodule MyApp.Data.User do
|> unique_constraint(:battle_net_id)
end
defp update_char_changeset(user, params \\ %{}) do
user
|> cast(params, [:character_guild, :character_name, :character_class, :character_realm, :character_avatar])
|> validate_required([:character_name, :character_class, :character_realm, :character_avatar])
end
def update_character(params) do
{:ok, data} = Repo.transaction(fn ->
user = Repo.get(__MODULE__, Map.get(params, "id"))
# remove columns from data because we need to update all of them
|> Map.drop([:character_realm, :character_name, :character_guild, :character_class, :character_avatar])
output = user
|> update_char_changeset(params)
|> Repo.update
|> Data.Util.process_insert_or_update
end)
data
end
@spec get_user(integer) :: nil | map
defp get_user(battle_net_id) do
query = from u in "user",

View File

@@ -1,40 +0,0 @@
defmodule MyAppWeb.BattleNetController do
use MyAppWeb, :controller
alias MyAppWeb.Response
alias MyApp.BattleNet
alias MyApp.Data
alias MyApp.Guardian.Auth
# https://us.battle.net/oauth/authorize?redirect_uri=https://localhost/api/battlenet/authorize&scope=wow.profile&client_id=vxqv32fddxsy6cmk6259amtymbuzmfrq&response_type=code
@spec authorize(map, map) :: any
def authorize(conn, %{"code" => code}) when not is_nil(code) do
{output, status} = code
|> BattleNet.Auth.get_access_token
|> BattleNet.User.get_user
|> Data.User.upsert_user
|> Auth.Token.add_token_and_map_claims
|> Response.put_resp
conn
|>put_status(status)
|>Response.json(output)
end
# TODO: cache this end point
def characters(conn, _params) do
%{"access_token" => token, "id" => user_id} = conn
|> MyApp.Guardian.Plug.current_claims
|> Map.take(["access_token", "id"])
{output, status} = user_id
|> BattleNet.User.get_user_characters(token)
|> Response.put_resp
conn
|>put_status(status)
|>Response.json(output)
end
end

View File

@@ -1,11 +1,57 @@
defmodule MyAppWeb.UserController do
use MyAppWeb, :controller
alias MyAppWeb.Response
alias MyApp.BattleNet
alias MyApp.Data
alias MyApp.Guardian.Auth
# https://us.battle.net/oauth/authorize?redirect_uri=https://localhost/api/battlenet/authorize&scope=wow.profile&client_id=vxqv32fddxsy6cmk6259amtymbuzmfrq&response_type=code
@spec authorize(map, map) :: any
def authorize(conn, %{"code" => code}) when not is_nil(code) do
{output, status} = code
|> BattleNet.Auth.get_access_token
|> BattleNet.User.get_user
|> Data.User.upsert_user
|> Auth.Token.add_token_and_map_claims
|> Response.put_resp
@spec index(map, map) :: any
def index(conn, _params) do
conn
|> Response.json("Auth works!")
|>put_status(status)
|>Response.json(output)
end
def characters(conn, _params) do
%{"access_token" => token, "id" => user_id} = conn
|> MyApp.Guardian.Plug.current_claims
|> Map.take(["access_token", "id"])
{output, status} = user_id
|> BattleNet.User.get_user_characters(token)
|> Response.put_resp
conn
|>put_status(status)
|>Response.json(output)
end
def update_selected_character(conn, params) do
id = conn
|> MyApp.Guardian.Plug.current_claims
|> Map.get("id")
params = params
|> Map.put("id", id)
|> Map.put_new("character_guild", nil) # set guild to nil if it doesn't exist
{output, status} = params
|> Data.User.update_character
|> Response.put_resp
conn
|>put_status(status)
|>Response.json(output)
end
end

View File

@@ -22,17 +22,12 @@ defmodule MyAppWeb.Router do
scope "/api", MyAppWeb do
pipe_through [:api]
scope "/battlenet" do
post "/authorize", BattleNetController, :authorize
pipe_through [:user_auth]
get "/characters", BattleNetController, :characters
end
scope "/user" do
# authenticated routes
post "/authorize", UserController, :authorize
pipe_through [:user_auth]
get "/", UserController, :index
get "/characters", UserController, :characters
put "/characters", UserController, :update_selected_character
end
scope "/thread" do

View File

@@ -6,7 +6,11 @@ defmodule MyApp.Repo.Migrations.CreateUser do
add :battle_net_id, :integer
add :battletag, :string
add :permissions, :string
add :character_guild, :string
add :character_name, :string
add :character_class, :string
add :character_realm, :string
add :character_avatar, :string
timestamps()
end

View File

@@ -1,14 +1,14 @@
defmodule MyAppWeb.UserControllerTest do
use MyAppWeb.ConnCase, async: true
test "get user index should return unauthorized" do
conn =
build_conn()
|> get("/api/user")
# test "get user index should return unauthorized" do
# conn =
# build_conn()
# |> get("/api/user")
body = conn |> response(401) |> Poison.decode!
# body = conn |> response(401) |> Poison.decode!
assert body["error"]["message"] == "unauthorized"
end
# assert body["error"]["message"] == "unauthorized"
# end
end