-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switches category controller away from ja_resource (#881)
* Swithed category controller away from ja_resource and canary * Address comments
- Loading branch information
1 parent
aca20b6
commit 798690b
Showing
3 changed files
with
41 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
defmodule CodeCorps.Policy.Category do | ||
alias CodeCorps.User | ||
|
||
@spec create?(User.t) :: boolean | ||
def create?(%User{admin: true}), do: true | ||
def create?(%User{admin: false}), do: false | ||
def create?(%User{}), do: false | ||
|
||
@spec update?(User.t) :: boolean | ||
def update?(%User{admin: true}), do: true | ||
def update?(%User{admin: false}), do: false | ||
def update?(%User{}), do: false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,43 @@ | ||
defmodule CodeCorpsWeb.CategoryController do | ||
use CodeCorpsWeb, :controller | ||
use JaResource | ||
|
||
alias CodeCorps.Category | ||
alias CodeCorps.{Category, User, Helpers.Query} | ||
|
||
plug :load_resource, model: Category, only: [:show] | ||
plug :load_and_authorize_resource, model: Category, only: [:create, :update] | ||
plug JaResource | ||
action_fallback CodeCorpsWeb.FallbackController | ||
plug CodeCorpsWeb.Plug.DataToAttributes | ||
|
||
@spec model :: module | ||
def model, do: CodeCorps.Category | ||
@spec index(Conn.t, map) :: Conn.t | ||
def index(%Conn{} = conn, %{} = params) do | ||
with categories <- Category |> Query.id_filter(params) |> Repo.all do | ||
conn |> render("index.json-api", data: categories) | ||
end | ||
end | ||
|
||
@spec show(Conn.t, map) :: Conn.t | ||
def show(%Conn{} = conn, %{"id" => id}) do | ||
with %Category{} = category <- Category |> Repo.get(id) do | ||
conn |> render("show.json-api", data: category) | ||
end | ||
end | ||
|
||
@spec create(Conn.t, map) :: Conn.t | ||
def create(%Conn{} = conn, %{} = params) do | ||
with %User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:create, %Category{}, params), | ||
{:ok, %Category{} = category} <- %Category{} |> Category.create_changeset(params) |> Repo.insert | ||
do | ||
conn |> put_status(:created) |> render("show.json-api", data: category) | ||
end | ||
end | ||
|
||
def handle_create(_conn, attributes) do | ||
Category.create_changeset(%Category{}, attributes) | ||
@spec update(Conn.t, map) :: Conn.t | ||
def update(%Conn{} = conn, %{"id" => id} = params) do | ||
with %Category{} = category <- Category |> Repo.get(id), | ||
%User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:update, category), | ||
{:ok, %Category{} = category} <- category |> Category.changeset(params) |> Repo.update | ||
do | ||
conn |> render("show.json-api", data: category) | ||
end | ||
end | ||
end |