diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb index c0b69ee..547125b 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb @@ -21,5 +21,11 @@ class CommentParagraphPlugin::API < Grape::API present_partial article, :with => Noosfero::API::Entities::Article end end + + get ':id/comment_paragraph_plugin/comments/count' do + article = find_article(environment.articles, params[:id]) + comments = select_filtered_collection_of(article, :comments, params) + comments.group(:paragraph_uuid).count + end end end diff --git a/plugins/comment_paragraph/test/unit/api_test.rb b/plugins/comment_paragraph/test/unit/api_test.rb index 363b499..12a29a9 100644 --- a/plugins/comment_paragraph/test/unit/api_test.rb +++ b/plugins/comment_paragraph/test/unit/api_test.rb @@ -56,4 +56,16 @@ class APITest < ActiveSupport::TestCase assert_equal 403, last_response.status end end + + should 'return comment counts grouped by paragraph' do + article = fast_create(TextArticle, :profile_id => person.id, :name => "Some thing", :published => false) + fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id) + fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id) + fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) + fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments/count?#{params.to_query}" + + json = JSON.parse(last_response.body) + assert_equal({"1"=>1, ""=>1, "2"=>2}, json) + end end -- libgit2 0.21.2