Commit bac4c76146f743783c60c7ca00fdb8a279dcc4ef
1 parent
055920fc
Exists in
web_steps_improvements
and in
6 other branches
api: paginate comments
Showing
2 changed files
with
13 additions
and
0 deletions
Show diff stats
lib/noosfero/api/v1/comments.rb
@@ -2,9 +2,12 @@ module Noosfero | @@ -2,9 +2,12 @@ module Noosfero | ||
2 | module API | 2 | module API |
3 | module V1 | 3 | module V1 |
4 | class Comments < Grape::API | 4 | class Comments < Grape::API |
5 | + MAX_PER_PAGE = 20 | ||
6 | + | ||
5 | before { authenticate! } | 7 | before { authenticate! } |
6 | 8 | ||
7 | resource :articles do | 9 | resource :articles do |
10 | + paginate max_per_page: MAX_PER_PAGE | ||
8 | # Collect comments from articles | 11 | # Collect comments from articles |
9 | # | 12 | # |
10 | # Parameters: | 13 | # Parameters: |
test/api/comments_test.rb
@@ -78,4 +78,14 @@ class CommentsTest < ActiveSupport::TestCase | @@ -78,4 +78,14 @@ class CommentsTest < ActiveSupport::TestCase | ||
78 | assert_not_nil comment.source | 78 | assert_not_nil comment.source |
79 | end | 79 | end |
80 | 80 | ||
81 | + should 'paginate comments' do | ||
82 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
83 | + 5.times { article.comments.create!(:body => "some comment", :author => user.person) } | ||
84 | + params[:per_page] = 3 | ||
85 | + | ||
86 | + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" | ||
87 | + json = JSON.parse(last_response.body) | ||
88 | + assert_equal 200, last_response.status | ||
89 | + assert_equal 3, json["comments"].length | ||
90 | + end | ||
81 | end | 91 | end |