Skip to content

Commit

Permalink
Fix comment endpoints to match ember
Browse files Browse the repository at this point in the history
  • Loading branch information
begedin committed Oct 18, 2016
1 parent 593f659 commit e255ab3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/code_corps/helpers/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,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
12 changes: 10 additions & 2 deletions test/controllers/comment_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@ defmodule CodeCorps.CommentControllerTest do
describe "index" do
test "lists all entries for specified task on index", %{conn: conn} do
task = insert(:task)
comments = insert_list(2, :comment, task: task)
insert_list(3, :comment)

path = conn |> task_comment_path(:index, task)
conn = conn |> get(path)
assert json_response(conn, 200)["data"] == []
json = conn |> get(path) |> json_response(200)

assert json["data"] |> Enum.count == 2
end

test "lists all entries for specific task on index", %{conn: conn} do

end
end

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: [task_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)
end

def handle_create(conn, attributes) do
%Comment{}
|> Comment.create_changeset(attributes)
Expand Down
2 changes: 1 addition & 1 deletion web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ defmodule CodeCorps.Router do
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]
resources "/comments", CommentController, only: [:index]
end
get "/users/email_available", UserController, :email_available
get "/users/username_available", UserController, :username_available
Expand Down

0 comments on commit e255ab3

Please sign in to comment.