From 21d843ebc87fad0af02cc71ae3d59f79b71fc268 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Wed, 21 Feb 2018 14:24:06 -0500 Subject: [PATCH] Refactor current_user --- lib/chess/auth.ex | 70 +------------------ .../controllers/api/game_controller.ex | 6 +- lib/chess_web/controllers/game_controller.ex | 6 +- lib/chess_web/views/game_view.ex | 2 +- lib/chess_web/views/layout_view.ex | 4 +- 5 files changed, 9 insertions(+), 79 deletions(-) diff --git a/lib/chess/auth.ex b/lib/chess/auth.ex index 4b22712..9c55709 100644 --- a/lib/chess/auth.ex +++ b/lib/chess/auth.ex @@ -9,96 +9,32 @@ defmodule Chess.Auth do alias Chess.Auth.User - @doc """ - Returns the list of users. + def current_user(conn) do + Guardian.Plug.current_resource(conn) + end - ## Examples - - iex> list_users() - [%User{}, ...] - - """ def list_users do Repo.all(User) end - @doc """ - Gets a single user. - - Raises `Ecto.NoResultsError` if the User does not exist. - - ## Examples - - iex> get_user!(123) - %User{} - - iex> get_user!(456) - ** (Ecto.NoResultsError) - - """ def get_user!(id), do: Repo.get!(User, id) - @doc """ - Creates a user. - - ## Examples - - iex> create_user(%{field: value}) - {:ok, %User{}} - - iex> create_user(%{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ def create_user(attrs \\ %{}) do %User{} |> User.changeset(attrs) |> Repo.insert() end - @doc """ - Updates a user. - - ## Examples - - iex> update_user(user, %{field: new_value}) - {:ok, %User{}} - - iex> update_user(user, %{field: bad_value}) - {:error, %Ecto.Changeset{}} - - """ def update_user(%User{} = user, attrs) do user |> User.changeset(attrs) |> Repo.update() end - @doc """ - Deletes a User. - - ## Examples - - iex> delete_user(user) - {:ok, %User{}} - - iex> delete_user(user) - {:error, %Ecto.Changeset{}} - - """ def delete_user(%User{} = user) do Repo.delete(user) end - @doc """ - Returns an `%Ecto.Changeset{}` for tracking user changes. - - ## Examples - - iex> change_user(user) - %Ecto.Changeset{source: %User{}} - - """ def change_user(%User{} = user) do User.changeset(user, %{}) end diff --git a/lib/chess_web/controllers/api/game_controller.ex b/lib/chess_web/controllers/api/game_controller.ex index 36fccba..be57e03 100644 --- a/lib/chess_web/controllers/api/game_controller.ex +++ b/lib/chess_web/controllers/api/game_controller.ex @@ -3,6 +3,8 @@ defmodule ChessWeb.Api.GameController do alias Chess.Store.Game + import Chess.Auth, only: [current_user: 1] + def show(conn, %{"id" => id}) do query = from(game in Game, preload: [:user, :opponent]) @@ -32,8 +34,4 @@ defmodule ChessWeb.Api.GameController do Map.put(board, "#{to_file},#{to_rank}", piece) end - - defp current_user(conn) do - Guardian.Plug.current_resource(conn) - end end diff --git a/lib/chess_web/controllers/game_controller.ex b/lib/chess_web/controllers/game_controller.ex index 4c66c81..b7e74a4 100644 --- a/lib/chess_web/controllers/game_controller.ex +++ b/lib/chess_web/controllers/game_controller.ex @@ -3,6 +3,8 @@ defmodule ChessWeb.GameController do alias Chess.Store.Game + import Chess.Auth, only: [current_user: 1] + def index(conn, _params) do changeset = Game.changeset(%Game{}) games = @@ -72,8 +74,4 @@ defmodule ChessWeb.GameController do query |> Repo.all end - - defp current_user(conn) do - Guardian.Plug.current_resource(conn) - end end diff --git a/lib/chess_web/views/game_view.ex b/lib/chess_web/views/game_view.ex index 6a4ded6..75bf284 100644 --- a/lib/chess_web/views/game_view.ex +++ b/lib/chess_web/views/game_view.ex @@ -1,7 +1,7 @@ defmodule ChessWeb.GameView do use ChessWeb, :view - import ChessWeb.LayoutView, only: [current_user: 1] + import Chess.Auth, only: [current_user: 1] def opponent(conn, game) do if current_user(conn).id == game.user_id do diff --git a/lib/chess_web/views/layout_view.ex b/lib/chess_web/views/layout_view.ex index 8cfd7ab..7c02b6e 100644 --- a/lib/chess_web/views/layout_view.ex +++ b/lib/chess_web/views/layout_view.ex @@ -1,7 +1,5 @@ defmodule ChessWeb.LayoutView do use ChessWeb, :view - def current_user(conn) do - Guardian.Plug.current_resource(conn) - end + import Chess.Auth, only: [current_user: 1] end