diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb index f0809fe..56c4688 100644 --- a/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin.rb @@ -55,6 +55,10 @@ class CommentParagraphPlugin < Noosfero::Plugin end + def self.api_mount_points + [CommentParagraphPlugin::API] + end + end require_dependency 'comment_paragraph_plugin/macros/allow_comment' diff --git a/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb new file mode 100644 index 0000000..a269d1b --- /dev/null +++ b/plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb @@ -0,0 +1,14 @@ +class CommentParagraphPlugin::API < Grape::API + MAX_PER_PAGE = 20 + + resource :articles do + paginate max_per_page: MAX_PER_PAGE + get ':id/comment_paragraph_plugin/comments' do + article = find_article(environment.articles, params[:id]) + comments = select_filtered_collection_of(article, :comments, params) + comments = comments.in_paragraph(params[:paragraph_uuid]) + comments = comments.without_reply if(params[:without_reply].present?) + present paginate(comments), :with => Noosfero::API::Entities::Comment, :current_person => current_person + end + end +end diff --git a/plugins/comment_paragraph/lib/ext/entities.rb b/plugins/comment_paragraph/lib/ext/entities.rb new file mode 100644 index 0000000..15a2cfc --- /dev/null +++ b/plugins/comment_paragraph/lib/ext/entities.rb @@ -0,0 +1,12 @@ +require File.join(Rails.root,'lib','noosfero','api','entities') +module Noosfero + module API + module Entities + class Comment < CommentBase + expose :paragraph_uuid + expose :comment_paragraph_selected_area + expose :comment_paragraph_selected_content + end + end + end +end diff --git a/plugins/comment_paragraph/test/unit/api_test.rb b/plugins/comment_paragraph/test/unit/api_test.rb new file mode 100644 index 0000000..f68f6b8 --- /dev/null +++ b/plugins/comment_paragraph/test/unit/api_test.rb @@ -0,0 +1,39 @@ +require_relative '../test_helper' +require_relative '../../../../test/api/test_helper' + +class APITest < ActiveSupport::TestCase + + def setup + login_api + environment = Environment.default + environment.enable_plugin(CommentParagraphPlugin) + end + + should 'return custom parameters for each comment' do + article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) + comment = fast_create(Comment, paragraph_uuid: '1', source_id: article.id, author_id: fast_create(Person).id) + comment.comment_paragraph_selected_area = 'area' + comment.comment_paragraph_selected_content = 'content' + comment.save! + params[:paragraph_uuid] = '1' + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" + + json = JSON.parse(last_response.body) + assert_equivalent ['1'], json['comments'].map {|c| c['paragraph_uuid']} + assert_equivalent ['area'], json['comments'].map {|c| c['comment_paragraph_selected_area']} + assert_equivalent ['content'], json['comments'].map {|c| c['comment_paragraph_selected_content']} + end + + should 'return comments that belongs to a paragraph' do + article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) + comment1 = fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id) + comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id) + comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) + params[:paragraph_uuid] = '1' + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" + + json = JSON.parse(last_response.body) + assert_equivalent [comment1.id], json['comments'].map {|c| c['id']} + end + +end -- libgit2 0.21.2