diff --git a/test/controllers/organization_controller_test.exs b/test/controllers/organization_controller_test.exs index 49db2ddeb..766ed557e 100644 --- a/test/controllers/organization_controller_test.exs +++ b/test/controllers/organization_controller_test.exs @@ -53,11 +53,9 @@ defmodule CodeCorps.OrganizationControllerTest do assert data["attributes"]["slug"] == organization.slug end - test "does not show resource and instead throw error when id is nonexistent", %{conn: conn} do - assert_error_sent 404, fn -> - path = conn |> organization_path(:show, -1) - conn |> get(path) - end + test "renders 404 when id is nonexistent", %{conn: conn} do + path = conn |> organization_path(:show, -1) + assert conn |> get(path) |> json_response(404) end end diff --git a/web/controllers/organization_controller.ex b/web/controllers/organization_controller.ex index d481964e6..0ac65d6b3 100644 --- a/web/controllers/organization_controller.ex +++ b/web/controllers/organization_controller.ex @@ -1,60 +1,20 @@ defmodule CodeCorps.OrganizationController do use CodeCorps.Web, :controller + use JaResource alias CodeCorps.Organization - alias JaSerializer.Params - import Organization, only: [changeset: 2] + import CodeCorps.ControllerHelpers, only: [coalesce_id_string: 1] plug :load_and_authorize_resource, model: Organization, only: [:create, :update] - plug :scrub_params, "data" when action in [:create, :update] + plug JaResource - def index(conn, params) do - organizations = - Organization - |> Organization.index_filters(params) - |> Repo.all - - render(conn, "index.json-api", data: organizations) - end - - def create(conn, %{"data" => data = %{"type" => "organization", "attributes" => _organization_params}}) do - changeset = Organization.create_changeset(%Organization{}, Params.to_attributes(data)) - - case Repo.insert(changeset) do - {:ok, organization} -> - conn - |> put_status(:created) - |> put_resp_header("location", organization_path(conn, :show, organization)) - |> render("show.json-api", data: organization) - {:error, changeset} -> - conn - |> put_status(:unprocessable_entity) - |> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset) - end + def filter(_conn, query, "id", id_list) do + ids = id_list |> coalesce_id_string + query |> where([object], object.id in ^ids) end - def show(conn, %{"id" => id}) do - organization = - Organization - |> Repo.get!(id) - render(conn, "show.json-api", data: organization) + def handle_create(_conn, attributes) do + Organization.create_changeset(%Organization{}, attributes) end - - def update(conn, %{"id" => id, "data" => data = %{"type" => "organization", "attributes" => _organization_params}}) do - changeset = - Organization - |> Repo.get!(id) - |> changeset(Params.to_attributes(data)) - - case Repo.update(changeset) do - {:ok, organization} -> - render(conn, "show.json-api", data: organization) - {:error, changeset} -> - conn - |> put_status(:unprocessable_entity) - |> render(CodeCorps.ChangesetView, "error.json-api", changeset: changeset) - end - end - end diff --git a/web/models/organization.ex b/web/models/organization.ex index 56403db1e..afd2defe6 100644 --- a/web/models/organization.ex +++ b/web/models/organization.ex @@ -51,10 +51,6 @@ defmodule CodeCorps.Organization do |> put_slugged_route() end - def index_filters(query, params) do - query |> id_filter(params) - end - defp put_slugged_route(changeset) do case changeset do %Ecto.Changeset{valid?: true, changes: %{slug: slug}} ->