mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Make changeset function responsible for the move
This commit is contained in:
parent
e831297fb8
commit
caa68d41f8
@ -35,7 +35,11 @@ defmodule Chess.Store.Game do
|
||||
|> foreign_key_constraint(:opponent_id)
|
||||
end
|
||||
|
||||
def move_changeset(struct, params \\ %{}) do
|
||||
def move_changeset(struct, move_params) do
|
||||
params = %{
|
||||
board: Board.move_piece(struct.board, move_params),
|
||||
}
|
||||
|
||||
struct
|
||||
|> cast(params, required_attrs())
|
||||
|> validate_king_in_check(struct, params)
|
||||
|
||||
@ -38,11 +38,7 @@ defmodule ChessWeb.GameChannel do
|
||||
|> Game.for_user_id()
|
||||
|> Repo.get!(socket.assigns.game_id)
|
||||
|
||||
changeset = Game.move_changeset(
|
||||
game, %{
|
||||
board: Board.move_piece(game.board, move_params),
|
||||
}
|
||||
)
|
||||
changeset = Game.move_changeset(game, move_params)
|
||||
|
||||
case Repo.update(changeset) do
|
||||
{:ok, game} ->
|
||||
|
||||
@ -89,9 +89,7 @@ defmodule Chess.GameTest do
|
||||
|
||||
move_params = %{"from" => [4, 1], "to" => [4, 3]}
|
||||
|
||||
changeset = Game.move_changeset(game, %{
|
||||
board: Board.move_piece(game.board, move_params),
|
||||
})
|
||||
changeset = Game.move_changeset(game, move_params)
|
||||
|
||||
assert {:ok, new_game} = Repo.update(changeset)
|
||||
assert new_game.turn == "black"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user