1
0
mirror of https://github.com/danbee/chess synced 2025-03-04 08:39:06 +00:00
chess/lib/chess/store/move.ex
2023-02-04 21:35:56 -06:00

40 lines
748 B
Elixir

defmodule Chess.Store.Move do
@moduledoc false
use Ecto.Schema
import Ecto.Changeset
alias Chess.Store.Game
schema "moves" do
field(:from, :map)
field(:to, :map)
field(:piece, :map)
field(:piece_captured, :map)
belongs_to(:game, Game)
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, required_attrs())
|> validate_required(required_attrs())
end
def transform(move) do
%{
id: move.id,
piece: move.piece,
piece_captured: move.piece_captured,
from: <<97 + move.from["file"], 49 + move.from["rank"]>>,
to: <<97 + move.to["file"], 49 + move.to["rank"]>>
}
end
defp required_attrs, do: ~w[game_id from to piece]a
end