Skip to content

Commit

Permalink
Simplify comments endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Oct 26, 2016
1 parent 4986291 commit 4f2f997
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
23 changes: 16 additions & 7 deletions test/controllers/comment_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ defmodule CodeCorps.CommentControllerTest do
defp build_payload, do: %{ "data" => %{"type" => "comment"}}

describe "index" do
test "lists all entries for specified task on index", %{conn: conn} do
task = insert(:task)
insert_list(2, :comment, task: task)
insert_list(3, :comment)
test "lists all entries on index", %{conn: conn} do
path = conn |> comment_path(:index)
conn = conn |> get(path)

assert json_response(conn, 200)["data"] == []
end

test "filters resources on index", %{conn: conn} do
first_comment = insert(:comment)
second_comment = insert(:comment)
insert(:comment)

path = conn |> task_comment_path(:index, task)
json = conn |> get(path) |> json_response(200)
path = "comments/?filter[id]=#{first_comment.id},#{second_comment.id}"

assert json["data"] |> Enum.count == 2
conn
|> get(path)
|> json_response(200)
|> assert_ids_from_response([first_comment.id, second_comment.id])
end
end

Expand Down
6 changes: 3 additions & 3 deletions web/controllers/comment_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ defmodule CodeCorps.CommentController do
use CodeCorps.Web, :controller
use JaResource

import CodeCorps.Helpers.Query, only: [task_filter: 2]
import CodeCorps.Helpers.Query, only: [id_filter: 2]

alias CodeCorps.Comment

plug :load_and_authorize_changeset, model: Comment, only: [:create]
plug :load_and_authorize_resource, model: Comment, only: [:update]
plug JaResource

def handle_index(_conn, %{"task_id" => task_id}) do
Comment |> task_filter(task_id)
def filter(_conn, query, "id", id_list) do
query |> id_filter(id_list)
end

def handle_create(conn, attributes) do
Expand Down
6 changes: 2 additions & 4 deletions web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule CodeCorps.Router do
post "/token/refresh", TokenController, :refresh

resources "/categories", CategoryController, only: [:index, :show]
resources "/comments", CommentController, only: [:show]
resources "/comments", CommentController, only: [:index, :show]
resources "/donation-goals", DonationGoalController, only: [:index, :show]
resources "/organizations", OrganizationController, only: [:index, :show]
resources "/organization-memberships", OrganizationMembershipController, only: [:index, :show]
Expand All @@ -56,9 +56,7 @@ defmodule CodeCorps.Router do
resources "/roles", RoleController, only: [:index, :show]
resources "/role-skills", RoleSkillController, only: [:index, :show]
resources "/skills", SkillController, only: [:index, :show]
resources "/tasks", TaskController, only: [:index, :show] do
resources "/comments", CommentController, only: [:index]
end
resources "/tasks", TaskController, only: [:index, :show]
get "/users/email_available", UserController, :email_available
get "/users/username_available", UserController, :username_available
resources "/users", UserController, only: [:index, :show, :create]
Expand Down

0 comments on commit 4f2f997

Please sign in to comment.