mirror of
https://github.com/mgerb/classic-wow-forums
synced 2026-01-11 09:32:51 +00:00
server - validate character exists on user update
This commit is contained in:
@@ -37,16 +37,26 @@ defmodule MyAppWeb.UserController do
|
|||||||
end
|
end
|
||||||
|
|
||||||
def update_selected_character(conn, params) do
|
def update_selected_character(conn, params) do
|
||||||
id = conn
|
%{"id" => user_id, "access_token" => access_token} = conn
|
||||||
|> MyApp.Guardian.Plug.current_claims
|
|> MyApp.Guardian.Plug.current_claims
|
||||||
|> Map.get("id")
|
|> Map.take(["id", "access_token"])
|
||||||
|
|
||||||
params = params
|
# validate the character exists in users WoW profile
|
||||||
|> Map.put("id", id)
|
{:ok, characterList} = BattleNet.User.get_user_characters(user_id, access_token)
|
||||||
|> Map.put_new("character_guild", nil) # set guild to nil if it doesn't exist
|
exists = Enum.find(characterList["characters"], fn(char) ->
|
||||||
|
char["name"] == params["character_name"] && char["realm"] == params["character_realm"]
|
||||||
|
end)
|
||||||
|
|
||||||
{output, status} = params
|
{output, status} = case exists do
|
||||||
|> Data.User.update_character
|
nil -> {:error, "character doesn't exist"}
|
||||||
|
_ ->
|
||||||
|
params = params
|
||||||
|
|> Map.put("id", user_id)
|
||||||
|
|> Map.put_new("character_guild", nil) # set guild to nil if it doesn't exist
|
||||||
|
|
||||||
|
params
|
||||||
|
|> Data.User.update_character
|
||||||
|
end
|
||||||
|> Response.put_resp
|
|> Response.put_resp
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|
|||||||
Reference in New Issue
Block a user