1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00
chess/lib/chess_web/models/game.ex
Dan Barber 43a4299478
Upgrade to Phoenix 1.3
I've also moved everything around to match the new Phoenix 1.3 directory
structure.
2018-01-12 16:47:26 -05:00

36 lines
646 B
Elixir

defmodule Chess.Game do
use Chess.Web, :model
schema "games" do
field :board, :map
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct) do
struct
|> cast(%{}, [:board])
|> set_default_board
|> validate_required([:board])
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:board])
|> validate_required([:board])
end
def ordered(query) do
query
|> order_by([game], desc: game.inserted_at)
end
def set_default_board(changeset) do
changeset
|> put_change(:board, Chess.Board.default)
end
end