-
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.
Merge pull request #886 from vishaldeepak/organization_controller_switch
Remove ja resource from organization controller
- Loading branch information
Showing
4 changed files
with
41 additions
and
16 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
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
41 changes: 30 additions & 11 deletions
41
lib/code_corps_web/controllers/organization_controller.ex
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,22 +1,41 @@ | ||
defmodule CodeCorpsWeb.OrganizationController do | ||
use CodeCorpsWeb, :controller | ||
use JaResource | ||
|
||
import CodeCorps.Helpers.Query, only: [id_filter: 2] | ||
alias CodeCorps.{Helpers.Query, Organization, User} | ||
|
||
alias CodeCorps.Organization | ||
action_fallback CodeCorpsWeb.FallbackController | ||
plug CodeCorpsWeb.Plug.DataToAttributes | ||
|
||
plug :load_and_authorize_resource, model: Organization, only: [:create, :update] | ||
plug JaResource | ||
@spec index(Conn.t, map) :: Conn.t | ||
def index(%Conn{} = conn, %{} = params) do | ||
with organizations <- Organization |> Query.id_filter(params) |> Repo.all do | ||
conn |> render("index.json-api", data: organizations) | ||
end | ||
end | ||
|
||
@spec model :: module | ||
def model, do: CodeCorps.Organization | ||
@spec show(Conn.t, map) :: Conn.t | ||
def show(%Conn{} = conn, %{"id" => id}) do | ||
with %Organization{} = organization <- Organization |> Repo.get(id) do | ||
conn |> render("show.json-api", data: organization) | ||
end | ||
end | ||
|
||
def filter(_conn, query, "id", id_list) do | ||
query |> id_filter(id_list) | ||
@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, %Organization{}, params), | ||
{:ok, %Organization{} = organization} <- %Organization{} |> Organization.create_changeset(params) |> Repo.insert do | ||
conn |> put_status(:created) |> render("show.json-api", data: organization) | ||
end | ||
end | ||
|
||
def handle_create(_conn, attributes) do | ||
Organization.create_changeset(%Organization{}, attributes) | ||
@spec update(Conn.t, map) :: Conn.t | ||
def update(%Conn{} = conn, %{"id" => id} = params) do | ||
with %Organization{} = organization <- Organization |> Repo.get(id), | ||
%User{} = current_user <- conn |> Guardian.Plug.current_resource, | ||
{:ok, :authorized} <- current_user |> Policy.authorize(:update, organization), | ||
{:ok, %Organization{} = organization} <- organization |> Organization.changeset(params) |> Repo.update do | ||
conn |> render("show.json-api", data: organization) | ||
end | ||
end | ||
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