Fix upsert on discord id conflict
This commit is contained in:
parent
4edaae9343
commit
324d041d5c
@ -35,11 +35,19 @@ defmodule Chessh.Player do
|
|||||||
|> cast(attrs, [:authentications])
|
|> cast(attrs, [:authentications])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def discord_changeset(player, attrs) do
|
||||||
|
player
|
||||||
|
|> cast(attrs, [:username, :discord_id])
|
||||||
|
|> validate_username()
|
||||||
|
|> validate_discord_id()
|
||||||
|
end
|
||||||
|
|
||||||
def registration_changeset(player, attrs, opts \\ []) do
|
def registration_changeset(player, attrs, opts \\ []) do
|
||||||
player
|
player
|
||||||
|> cast(attrs, [:username, :password, :discord_id])
|
|> cast(attrs, [:username, :password, :discord_id])
|
||||||
|> validate_username()
|
|> validate_username()
|
||||||
|> validate_password(opts)
|
|> validate_password(opts)
|
||||||
|
|> validate_discord_id()
|
||||||
end
|
end
|
||||||
|
|
||||||
def password_changeset(player, attrs, opts \\ []) do
|
def password_changeset(player, attrs, opts \\ []) do
|
||||||
@ -67,13 +75,16 @@ defmodule Chessh.Player do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp validate_discord_id(changeset) do
|
||||||
|
changeset
|
||||||
|
|> unique_constraint(:discord_id)
|
||||||
|
end
|
||||||
|
|
||||||
defp validate_username(changeset) do
|
defp validate_username(changeset) do
|
||||||
changeset
|
changeset
|
||||||
|> validate_required([:username])
|
|> validate_required([:username])
|
||||||
|> validate_length(:username, min: 2, max: 16)
|
|> validate_length(:username, min: 2, max: 40)
|
||||||
|> validate_format(:username, ~r/^[a-zA-Z0-9_\-]*$/,
|
|> validate_format(:username, ~r/^.{3,32}#[0-9]{4}$/, message: "must match discord tag format")
|
||||||
message: "only letters, numbers, underscores, and hyphens allowed"
|
|
||||||
)
|
|
||||||
|> unique_constraint(:username)
|
|> unique_constraint(:username)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -270,19 +270,22 @@ defmodule Chessh.Web.Endpoint do
|
|||||||
%{"username" => username, "discriminator" => discriminator, "id" => discord_id} =
|
%{"username" => username, "discriminator" => discriminator, "id" => discord_id} =
|
||||||
Jason.decode!(String.Chars.to_string(user_details))
|
Jason.decode!(String.Chars.to_string(user_details))
|
||||||
|
|
||||||
%Player{id: id} =
|
case Repo.get_by(Player, discord_id: discord_id) do
|
||||||
Repo.insert!(
|
nil -> %Player{discord_id: discord_id}
|
||||||
%Player{discord_id: discord_id, username: username <> "#" <> discriminator},
|
player -> player
|
||||||
on_conflict: [set: [discord_id: discord_id]],
|
end
|
||||||
conflict_target: :discord_id
|
|> Player.discord_changeset(%{
|
||||||
)
|
username: username <> "#" <> discriminator,
|
||||||
|
discord_id: discord_id
|
||||||
|
})
|
||||||
|
|> Repo.insert_or_update()
|
||||||
|
|
||||||
{200,
|
{200,
|
||||||
%{
|
%{
|
||||||
success: true,
|
success: true,
|
||||||
jwt:
|
jwt:
|
||||||
Token.generate_and_sign!(%{
|
Token.generate_and_sign!(%{
|
||||||
"uid" => id
|
"uid" => Repo.get_by(Player, discord_id: discord_id).id
|
||||||
})
|
})
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user