Create players

This commit is contained in:
2020-12-28 22:33:14 -08:00
parent 6266b089a5
commit bddf7ff040
13 changed files with 114 additions and 7 deletions
+9 -3
View File
@@ -3,14 +3,20 @@ defmodule Bwc do
The core module for the Blank White Cards game.
"""
alias Bwc.{Repo, Card}
alias Bwc.{Repo, Card, Player}
@repo Repo
def get_title do
def new_player, do: Player.changeset(%Player{})
end
def create_player(attrs), do: Player.changeset(%Player{},attrs) |> @repo.insert()
def delete_player(id), do: get_player(id) |> @repo.delete
def list_players, do: @repo.all(Player)
def get_player(id), do: @repo.get!(Player, id)
def list_cards, do: @repo.all(Card)
+18
View File
@@ -0,0 +1,18 @@
defmodule Bwc.Player do
use Ecto.Schema
import Ecto.Changeset
schema "players" do
field :username, :string
field :picture, :string
end
def changeset(player, attrs \\ %{}) do
player
|> cast(attrs, [:username, :picture])
|> validate_required(:username)
|> unique_constraint(:username)
|> validate_length(:username, min: 3)
end
end
@@ -0,0 +1,10 @@
defmodule Bwc.Repo.Migrations.CreatePlayers do
use Ecto.Migration
def change do
create table "players" do
add :username, :string
add :picture, :string
end
end
end
@@ -0,0 +1,7 @@
defmodule Bwc.Repo.Migrations.AddUniqueUsernames do
use Ecto.Migration
def change do
create unique_index(:players, [:username])
end
end