diff --git a/lib/chess_web/controllers/game_controller.ex b/lib/chess_web/controllers/game_controller.ex
index b12519f..f27640b 100644
--- a/lib/chess_web/controllers/game_controller.ex
+++ b/lib/chess_web/controllers/game_controller.ex
@@ -2,6 +2,7 @@ defmodule ChessWeb.GameController do
use ChessWeb, :controller
alias Chess.Store.Game
+ alias Chess.Auth.User
def index(conn, _params) do
changeset = Game.changeset(%Game{})
@@ -14,6 +15,18 @@ defmodule ChessWeb.GameController do
render(conn, "index.html", games: games, changeset: changeset)
end
+ def new(conn, _params) do
+ changeset = Game.changeset(%Game{})
+
+ query = from user in "users",
+ where: user.id != ^current_user(conn).id,
+ select: {user.username, user.id}
+
+ opponents = query |> Repo.all
+
+ render(conn, "new.html", changeset: changeset, opponents: opponents)
+ end
+
def create(conn, _params) do
changeset = Game.create_changeset(
%Game{},
diff --git a/lib/chess_web/router.ex b/lib/chess_web/router.ex
index 1bb7ca4..5366679 100644
--- a/lib/chess_web/router.ex
+++ b/lib/chess_web/router.ex
@@ -35,7 +35,7 @@ defmodule ChessWeb.Router do
pipe_through [:browser, :auth, :ensure_auth]
resources "/games", GameController,
- only: [:index, :create, :show, :delete]
+ only: [:index, :new, :create, :show, :delete]
end
# Other scopes may use custom stacks.
diff --git a/lib/chess_web/templates/game/index.html.eex b/lib/chess_web/templates/game/index.html.eex
index b6b01d0..c9f4c7c 100644
--- a/lib/chess_web/templates/game/index.html.eex
+++ b/lib/chess_web/templates/game/index.html.eex
@@ -15,8 +15,6 @@
-<%= form_for @changeset, game_path(@conn, :create), [class: "create-game"], fn _f -> %>
-
- <%= submit "Create game", class: "btn btn-primary" %>
-
-<% end %>
+
+ <%= link "New game", to: game_path(@conn, :new), class: "button" %>
+
diff --git a/lib/chess_web/templates/game/new.html.eex b/lib/chess_web/templates/game/new.html.eex
new file mode 100644
index 0000000..41a5049
--- /dev/null
+++ b/lib/chess_web/templates/game/new.html.eex
@@ -0,0 +1,14 @@
+New game
+
+<%= form_for @changeset, game_path(@conn, :create), [class: "create-game"],
+ fn form -> %>
+
+ <%= label form, :opponent_id %>
+ <%= select form, :opponent_id, @opponents %>
+ <%= error_tag form, :opponent_id %>
+
+
+
+ <%= submit "Create game", class: "button" %>
+
+<% end %>
diff --git a/test/features/games_test.exs b/test/features/games_test.exs
index 20d1cf2..bf2c594 100644
--- a/test/features/games_test.exs
+++ b/test/features/games_test.exs
@@ -1,13 +1,8 @@
defmodule Chess.GamesTest do
use ChessWeb.FeatureCase
- import Wallaby.Query, only: [
- css: 1,
- css: 2,
- button: 1,
- text_field: 1,
- link: 1,
- ]
+ import Wallaby.Query
+
import Chess.Factory, only: [create_user: 2, create_game_for: 1]
test "visit homepage", %{session: session} do
@@ -17,27 +12,42 @@ defmodule Chess.GamesTest do
end
test "can create a new game", %{session: session} do
+ create_user("zelda", "ganonsucks")
+
session
|> login()
- |> create_game()
+ |> visit("/games")
+ |> click(link("New game"))
+ |> select("game[opponent_id]", option: "zelda")
+ |> click(button("Create game"))
|> assert_has(css(".board"))
end
test "can only see own games", %{session: session} do
- user = create_user("zelda@hyrule.kingdom", "ganonsucks")
+ create_user("urbosa", "gerudoqueen")
+
+ user = create_user("zelda", "ganonsucks")
create_game_for(user)
session
|> login()
- |> create_game()
+ |> visit("/games")
+ |> click(link("New game"))
+ |> select("game[opponent_id]", option: "urbosa")
+ |> click(button("Create game"))
|> click(link("Back to games"))
|> assert_has(css(".table tr", count: 1))
end
test "can move a piece", %{session: session} do
+ create_user("zelda", "ganonsucks")
+
session
|> login()
- |> create_game()
+ |> visit("/games")
+ |> click(link("New game"))
+ |> select("game[opponent_id]", option: "zelda")
+ |> click(button("Create game"))
session
|> click(css("#f4-r1"))
@@ -60,10 +70,12 @@ defmodule Chess.GamesTest do
|> click(button("Log in"))
end
- defp create_game(session) do
+ def select(session, name, [option: option]) do
+ session
+ |> find(css("[name='#{name}']"))
+ |> click(option(option))
+
session
- |> visit("/games")
- |> click(button("Create game"))
end
defp square_selected(square) do
diff --git a/test/support/factory.ex b/test/support/factory.ex
index 7393ad5..3ecb923 100644
--- a/test/support/factory.ex
+++ b/test/support/factory.ex
@@ -3,7 +3,7 @@ defmodule Chess.Factory do
alias Chess.Store.Game
alias Chess.Repo
- def create_user(username \\ "zelda", password \\ "password") do
+ def create_user(username \\ "link", password \\ "ilovezelda") do
User.changeset(
%User{},
%{username: username, password: password}
diff --git a/test/support/feature_case.ex b/test/support/feature_case.ex
index 6e10016..6063df5 100644
--- a/test/support/feature_case.ex
+++ b/test/support/feature_case.ex
@@ -8,7 +8,6 @@ defmodule ChessWeb.FeatureCase do
alias Chess.Repo
import Ecto
import Ecto.Changeset
- import Ecto.Query
import ChessWeb.Router.Helpers
end