1
0
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:
Daniel Barber 2018-02-23 11:42:46 -05:00
parent 08786460d0
commit 34b985fa77
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
4 changed files with 15 additions and 13 deletions

View File

@ -1,7 +1,7 @@
defmodule Chess.Board do
@moduledoc false
def output(board) do
def transform(board) do
Enum.map(0..7, fn (rank) ->
Enum.map(0..7, fn (file) ->
board["#{file},#{rank}"]

View File

@ -2,6 +2,7 @@ defmodule ChessWeb.Api.GameController do
use ChessWeb, :controller
alias Chess.Store.Game
alias Chess.Board
import Chess.Auth, only: [current_user: 1]
@ -12,7 +13,8 @@ defmodule ChessWeb.Api.GameController do
|> Game.for_user()
|> Repo.get!(id)
render(conn, "show.json", game: game)
conn
|> json(Board.transform(game.board))
end
def update(conn, %{"id" => id, "move" => move_params}) do
@ -28,7 +30,8 @@ defmodule ChessWeb.Api.GameController do
case Repo.update(changeset) do
{:ok, game} ->
render(conn, "show.json", game: game)
conn
|> json(Board.transform(game.board))
end
end

View File

@ -15,7 +15,8 @@ defmodule ChessWeb.GameController do
|> preload([:user, :opponent])
|> Repo.all
render(conn, "index.html", games: games, changeset: changeset)
conn
|> render("index.html", games: games, changeset: changeset)
end
def new(conn, _params) do
@ -25,7 +26,8 @@ defmodule ChessWeb.GameController do
|> current_user()
|> get_opponents()
render(conn, "new.html", changeset: changeset, opponents: opponents)
conn
|> render("new.html", changeset: changeset, opponents: opponents)
end
def create(conn, %{"game" => %{"opponent_id" => opponent_id}}) do
@ -42,9 +44,11 @@ defmodule ChessWeb.GameController do
conn
|> put_flash(:info, "Game created successfully.")
|> redirect(to: game_path(conn, :show, game))
{:error, changeset} ->
opponents = get_opponents(current_user(conn))
render(conn, "new.html", changeset: changeset, opponents: opponents)
conn
|> render("new.html", changeset: changeset, opponents: opponents)
end
end
@ -58,7 +62,8 @@ defmodule ChessWeb.GameController do
query
|> Repo.get!(id)
render(conn, "show.html", game: game)
conn
|> render("show.html", game: game)
end
def delete(conn, %{"id" => id}) do

View File

@ -1,9 +1,3 @@
defmodule ChessWeb.Api.GameView do
use ChessWeb, :view
alias Chess.Board
def render("show.json", %{game: game}) do
Board.output(game.board)
end
end