mirror of
https://github.com/danbee/chess
synced 2025-03-04 08:39:06 +00:00
Game controller accepts opponent id
This commit is contained in:
parent
fffff9624e
commit
8f19a086df
@ -21,10 +21,13 @@ defmodule ChessWeb.GameController do
|
||||
render(conn, "new.html", changeset: changeset, opponents: opponents)
|
||||
end
|
||||
|
||||
def create(conn, _params) do
|
||||
def create(conn, %{"game" => %{"opponent_id" => opponent_id}}) do
|
||||
changeset = Game.create_changeset(
|
||||
%Game{},
|
||||
%{user_id: current_user(conn).id}
|
||||
%{
|
||||
user_id: current_user(conn).id,
|
||||
opponent_id: opponent_id
|
||||
}
|
||||
)
|
||||
|
||||
case Repo.insert(changeset) do
|
||||
|
||||
@ -2,9 +2,9 @@ defmodule Chess.GameControllerTest do
|
||||
use ChessWeb.ConnCase
|
||||
|
||||
alias Chess.Store.Game
|
||||
alias Chess.Auth.User
|
||||
alias Chess.Auth.Guardian
|
||||
@valid_attrs %{}
|
||||
|
||||
import Chess.Factory, only: [create_user: 0, create_user: 2]
|
||||
|
||||
test "lists all entries on index", %{conn: conn} do
|
||||
conn = login(conn)
|
||||
@ -13,8 +13,11 @@ defmodule Chess.GameControllerTest do
|
||||
end
|
||||
|
||||
test "creates resource and redirects when data is valid", %{conn: conn} do
|
||||
opponent = create_user("daruk", "deathmountain")
|
||||
attrs = %{"opponent_id" => opponent.id}
|
||||
|
||||
conn = login(conn)
|
||||
conn = post conn, game_path(conn, :create), game: @valid_attrs
|
||||
conn = post conn, game_path(conn, :create), game: attrs
|
||||
game = Repo.one(Game)
|
||||
assert redirected_to(conn) == game_path(conn, :show, game)
|
||||
end
|
||||
@ -45,12 +48,4 @@ defmodule Chess.GameControllerTest do
|
||||
user = create_user()
|
||||
conn |> Guardian.Plug.sign_in(user)
|
||||
end
|
||||
|
||||
defp create_user() do
|
||||
changeset = User.changeset(
|
||||
%User{},
|
||||
%{username: "link@hyrule.kingdom", password: "ilovezelda"}
|
||||
)
|
||||
Repo.insert!(changeset)
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
Reference in New Issue
Block a user