mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Remove unnecessary API controller
This commit is contained in:
parent
0d68eeabfb
commit
fe0c58917f
@ -1,60 +0,0 @@
|
|||||||
defmodule ChessWeb.Api.GameController do
|
|
||||||
use ChessWeb, :controller
|
|
||||||
|
|
||||||
alias Chess.Store.Game
|
|
||||||
alias Chess.Board
|
|
||||||
|
|
||||||
alias ChessWeb.GameChannel
|
|
||||||
|
|
||||||
import Chess.Auth, only: [current_user: 1]
|
|
||||||
|
|
||||||
def show(conn, %{"id" => id}) do
|
|
||||||
game =
|
|
||||||
conn
|
|
||||||
|> current_user()
|
|
||||||
|> Game.for_user()
|
|
||||||
|> Repo.get!(id)
|
|
||||||
|
|
||||||
conn
|
|
||||||
|> json(game_attrs(conn, game))
|
|
||||||
end
|
|
||||||
|
|
||||||
def update(conn, %{"id" => id, "move" => move_params}) do
|
|
||||||
game =
|
|
||||||
conn
|
|
||||||
|> current_user()
|
|
||||||
|> Game.for_user()
|
|
||||||
|> Repo.get!(id)
|
|
||||||
|
|
||||||
changeset = Game.changeset(
|
|
||||||
game, %{
|
|
||||||
board: Board.move_piece(game.board, move_params),
|
|
||||||
turn: Game.change_turn(game.turn)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
case Repo.update(changeset) do
|
|
||||||
{:ok, game} ->
|
|
||||||
GameChannel.update_game(game)
|
|
||||||
|
|
||||||
conn
|
|
||||||
|> json(game_attrs(conn, game))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp game_attrs(conn, game) do
|
|
||||||
%{
|
|
||||||
board: Board.transform(game.board),
|
|
||||||
player: player(conn, game),
|
|
||||||
turn: game.turn
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
defp player(conn, game) do
|
|
||||||
if game.user_id == current_user(conn).id do
|
|
||||||
"white"
|
|
||||||
else
|
|
||||||
"black"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,83 +0,0 @@
|
|||||||
defmodule Chess.ApiGameControllerTest do
|
|
||||||
use ChessWeb.ConnCase
|
|
||||||
|
|
||||||
alias Chess.Auth.Guardian
|
|
||||||
|
|
||||||
import Chess.Factory
|
|
||||||
|
|
||||||
test "shows chosen game", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
opponent = insert(:user, %{email: "revali@rito.village"})
|
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> login(user)
|
|
||||||
|> get(api_game_path(conn, :show, game))
|
|
||||||
|
|
||||||
assert json_response(conn, 200)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "does not show a game if the user is not a player", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
opponent = insert(:user, %{email: "revali@rito.village"})
|
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
|
||||||
|
|
||||||
other_user = insert(:user, %{email: "mipha@zora.domain"})
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> login(other_user)
|
|
||||||
|
|
||||||
assert_error_sent 404, fn ->
|
|
||||||
get(conn, api_game_path(conn, :show, game.id))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "responds with 403 if user is not logged in", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
opponent = insert(:user, %{email: "revali@rito.village"})
|
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> get(api_game_path(conn, :show, game.id))
|
|
||||||
|
|
||||||
assert json_response(conn, 403)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "does not update a game if the user is not a player", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
opponent = insert(:user, %{email: "revali@rito.village"})
|
|
||||||
game = insert(:game, %{user_id: user.id, opponent_id: opponent.id})
|
|
||||||
|
|
||||||
other_user = insert(:user, %{email: "mipha@zora.domain"})
|
|
||||||
|
|
||||||
conn =
|
|
||||||
conn
|
|
||||||
|> login(other_user)
|
|
||||||
|
|
||||||
assert_error_sent 404, fn ->
|
|
||||||
patch(
|
|
||||||
conn,
|
|
||||||
api_game_path(conn, :update, game.id),
|
|
||||||
%{move: %{from: [1, 1], to: [2, 1]}}
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
test "renders page not found when id is nonexistent", %{conn: conn} do
|
|
||||||
user = insert(:user)
|
|
||||||
conn = login(conn, user)
|
|
||||||
|
|
||||||
assert_error_sent 404, fn ->
|
|
||||||
get(conn, api_game_path(conn, :show, -1))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
defp login(conn, user) do
|
|
||||||
conn
|
|
||||||
|> Guardian.Plug.sign_in(user)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user