Skip to content
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

Fix comment endpoints to match ember #372

Merged
merged 1 commit into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions blueprint/api.apib
Original file line number Diff line number Diff line change
Expand Up @@ -150,49 +150,6 @@ The Category resource allows us to categorize projects (Project Category), as we

This endpoint is for creating and returning comments.

## Task Comments [/tasks/{task_id}/comments]

You can list all comments or retrieve individual comments for a single task.

### List all of a task's comments [GET]

+ Parameters

+ task_id (number) - Id of a task.

+ Request

+ Headers

Accept: application/vnd.api+json

+ Response 200 (application/vnd.api+json; charset=utf-8)

+ Attributes (Comments Response)

## Task Comment [/tasks/{task_id}/comments/{id}]

### Retrieve a task's comment [GET]

+ Parameters

+ task_id (number) - Id of a task.
+ id (number) - The `id` of a comment.

+ Request

+ Headers

Accept: application/vnd.api+json

+ Response 200 (application/vnd.api+json; charset=utf-8)

+ Attributes (Comment Response)

+ Response 404 (application/vnd.api+json; charset=utf-8)

+ Attributes (Record Not Found Response)

## Comments [/comments]

You can list all comments or retrieve individual comments, as well as create and update comments.
Expand All @@ -216,22 +173,6 @@ You can list all comments or retrieve individual comments, as well as create and

+ Attributes (Unprocessable Entity Response)

## List comments for a specified task [GET /tasks/{task_id}/comments]

+ Parameters

+ task_id: 1 (number, required) - Task id, uniquely identifying a task

+ Request

+ Headers

Accept: application/vnd.api+json

+ Response 200 (application/vnd.api+json; charset=utf-8)

+ Attributes (Comments Response)

### Filter comments by list of ids [GET /comments{?filter[id]}]

+ Parameters
Expand Down
8 changes: 8 additions & 0 deletions lib/code_corps/helpers/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ defmodule CodeCorps.Helpers.Query do

# end task queries

# def comment queries

def task_filter(query, task_id) do
query |> where([object], object.task_id == ^task_id)
end

# end comment queries

# sorting

def sort_by_newest_first(query), do: query |> order_by([desc: :inserted_at])
Expand Down
19 changes: 16 additions & 3 deletions test/controllers/comment_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ 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)
path = conn |> task_comment_path(:index, task)
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 = "comments/?filter[id]=#{first_comment.id},#{second_comment.id}"

conn
|> get(path)
|> json_response(200)
|> assert_ids_from_response([first_comment.id, second_comment.id])
end
end

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

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 filter(_conn, query, "id", id_list) do
query |> id_filter(id_list)
end

def handle_create(conn, attributes) do
%Comment{}
|> Comment.create_changeset(attributes)
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, :show]
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