Repository navigation
Switches category controller away from ja_resource #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
crodriguez1a
wants to merge
34
commits into
code-corps:develop
from
crodriguez1a:864/category_controller
Closed
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
117d696
Merge pull request #225 from code-corps/develop
joshsmith a274816
Merge pull request #229 from code-corps/develop
joshsmith b29220f
Merge pull request #234 from code-corps/develop
joshsmith 20caea6
Merge pull request #239 from code-corps/develop
joshsmith 9ef4f60
Merge pull request #276 from code-corps/develop
joshsmith 65453c9
Merge pull request #278 from code-corps/develop
joshsmith f449564
Merge pull request #292 from code-corps/develop
joshsmith 1852e7d
Merge pull request #295 from code-corps/develop
joshsmith 5fc30f7
Merge pull request #296 from code-corps/develop
joshsmith c359738
Merge pull request #308 from code-corps/develop
joshsmith a0e63fb
Merge pull request #318 from code-corps/develop
joshsmith 82702ad
Initial commit
begedin 566bf78
Merge pull request #655 from code-corps/develop
joshsmith 1f035de
Merge pull request #657 from code-corps/develop
joshsmith 9f50fe9
Merge pull request #662 from code-corps/develop
joshsmith 3f159a9
Merge pull request #679 from code-corps/develop
joshsmith 34ee692
Merge pull request #681 from code-corps/develop
joshsmith bc92ca5
Merge pull request #684 from code-corps/develop
joshsmith fd074f6
Merge pull request #686 from code-corps/develop
joshsmith 7ee4bc3
Merge pull request #691 from code-corps/develop
joshsmith 56ffc7a
Merge pull request #705 from code-corps/develop
joshsmith f6d3e87
Merge pull request #727 from code-corps/develop
joshsmith 84bc5a2
Merge pull request #747 from code-corps/develop
joshsmith bd12d27
Merge pull request #760 from code-corps/develop
joshsmith d79696c
Merge pull request #763 from code-corps/develop
joshsmith 7f2e3e2
Merge pull request #765 from code-corps/develop
joshsmith 68fddd6
Merge pull request #767 from code-corps/develop
joshsmith f57833d
Merge pull request #771 from code-corps/develop
joshsmith cbb673f
Merge pull request #777 from code-corps/develop
joshsmith d54d3f8
Merge pull request #810 from code-corps/develop
joshsmith b5081c4
Merge pull request #812 from code-corps/develop
joshsmith 54fb711
Merge pull request #818 from code-corps/develop
joshsmith ed5beb8
Merge pull request #870 from code-corps/develop
joshsmith 89316ba
Attempt to fix commit history
crodriguez1a File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||
|
|
||
| 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 hidden or 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 hidden or 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(Plug.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(Plug.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 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Textbook quality controller conversion here 👍