mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Refactor controllers a bit
This commit is contained in:
parent
08786460d0
commit
34b985fa77
@ -1,7 +1,7 @@
|
|||||||
defmodule Chess.Board do
|
defmodule Chess.Board do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
def output(board) do
|
def transform(board) do
|
||||||
Enum.map(0..7, fn (rank) ->
|
Enum.map(0..7, fn (rank) ->
|
||||||
Enum.map(0..7, fn (file) ->
|
Enum.map(0..7, fn (file) ->
|
||||||
board["#{file},#{rank}"]
|
board["#{file},#{rank}"]
|
||||||
|
|||||||
@ -2,6 +2,7 @@ defmodule ChessWeb.Api.GameController do
|
|||||||
use ChessWeb, :controller
|
use ChessWeb, :controller
|
||||||
|
|
||||||
alias Chess.Store.Game
|
alias Chess.Store.Game
|
||||||
|
alias Chess.Board
|
||||||
|
|
||||||
import Chess.Auth, only: [current_user: 1]
|
import Chess.Auth, only: [current_user: 1]
|
||||||
|
|
||||||
@ -12,7 +13,8 @@ defmodule ChessWeb.Api.GameController do
|
|||||||
|> Game.for_user()
|
|> Game.for_user()
|
||||||
|> Repo.get!(id)
|
|> Repo.get!(id)
|
||||||
|
|
||||||
render(conn, "show.json", game: game)
|
conn
|
||||||
|
|> json(Board.transform(game.board))
|
||||||
end
|
end
|
||||||
|
|
||||||
def update(conn, %{"id" => id, "move" => move_params}) do
|
def update(conn, %{"id" => id, "move" => move_params}) do
|
||||||
@ -28,7 +30,8 @@ defmodule ChessWeb.Api.GameController do
|
|||||||
|
|
||||||
case Repo.update(changeset) do
|
case Repo.update(changeset) do
|
||||||
{:ok, game} ->
|
{:ok, game} ->
|
||||||
render(conn, "show.json", game: game)
|
conn
|
||||||
|
|> json(Board.transform(game.board))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,8 @@ defmodule ChessWeb.GameController do
|
|||||||
|> preload([:user, :opponent])
|
|> preload([:user, :opponent])
|
||||||
|> Repo.all
|
|> Repo.all
|
||||||
|
|
||||||
render(conn, "index.html", games: games, changeset: changeset)
|
conn
|
||||||
|
|> render("index.html", games: games, changeset: changeset)
|
||||||
end
|
end
|
||||||
|
|
||||||
def new(conn, _params) do
|
def new(conn, _params) do
|
||||||
@ -25,7 +26,8 @@ defmodule ChessWeb.GameController do
|
|||||||
|> current_user()
|
|> current_user()
|
||||||
|> get_opponents()
|
|> get_opponents()
|
||||||
|
|
||||||
render(conn, "new.html", changeset: changeset, opponents: opponents)
|
conn
|
||||||
|
|> render("new.html", changeset: changeset, opponents: opponents)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(conn, %{"game" => %{"opponent_id" => opponent_id}}) do
|
def create(conn, %{"game" => %{"opponent_id" => opponent_id}}) do
|
||||||
@ -42,9 +44,11 @@ defmodule ChessWeb.GameController do
|
|||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Game created successfully.")
|
|> put_flash(:info, "Game created successfully.")
|
||||||
|> redirect(to: game_path(conn, :show, game))
|
|> redirect(to: game_path(conn, :show, game))
|
||||||
|
|
||||||
{:error, changeset} ->
|
{:error, changeset} ->
|
||||||
opponents = get_opponents(current_user(conn))
|
opponents = get_opponents(current_user(conn))
|
||||||
render(conn, "new.html", changeset: changeset, opponents: opponents)
|
conn
|
||||||
|
|> render("new.html", changeset: changeset, opponents: opponents)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -58,7 +62,8 @@ defmodule ChessWeb.GameController do
|
|||||||
query
|
query
|
||||||
|> Repo.get!(id)
|
|> Repo.get!(id)
|
||||||
|
|
||||||
render(conn, "show.html", game: game)
|
conn
|
||||||
|
|> render("show.html", game: game)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(conn, %{"id" => id}) do
|
def delete(conn, %{"id" => id}) do
|
||||||
|
|||||||
@ -1,9 +1,3 @@
|
|||||||
defmodule ChessWeb.Api.GameView do
|
defmodule ChessWeb.Api.GameView do
|
||||||
use ChessWeb, :view
|
use ChessWeb, :view
|
||||||
|
|
||||||
alias Chess.Board
|
|
||||||
|
|
||||||
def render("show.json", %{game: game}) do
|
|
||||||
Board.output(game.board)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user