Commit a339de44eaaf3d61a28e78f0a5816344c7abe5cb
1 parent
99422d53
Exists in
staging
and in
32 other branches
comment_paragraph: add endpoint to list comments by paragraph uuid
Showing
4 changed files
with
69 additions
and
0 deletions
Show diff stats
plugins/comment_paragraph/lib/comment_paragraph_plugin.rb
| @@ -55,6 +55,10 @@ class CommentParagraphPlugin < Noosfero::Plugin | @@ -55,6 +55,10 @@ class CommentParagraphPlugin < Noosfero::Plugin | ||
| 55 | 55 | ||
| 56 | end | 56 | end |
| 57 | 57 | ||
| 58 | + def self.api_mount_points | ||
| 59 | + [CommentParagraphPlugin::API] | ||
| 60 | + end | ||
| 61 | + | ||
| 58 | end | 62 | end |
| 59 | 63 | ||
| 60 | require_dependency 'comment_paragraph_plugin/macros/allow_comment' | 64 | require_dependency 'comment_paragraph_plugin/macros/allow_comment' |
plugins/comment_paragraph/lib/comment_paragraph_plugin/api.rb
0 → 100644
| @@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
| 1 | +class CommentParagraphPlugin::API < Grape::API | ||
| 2 | + MAX_PER_PAGE = 20 | ||
| 3 | + | ||
| 4 | + resource :articles do | ||
| 5 | + paginate max_per_page: MAX_PER_PAGE | ||
| 6 | + get ':id/comment_paragraph_plugin/comments' do | ||
| 7 | + article = find_article(environment.articles, params[:id]) | ||
| 8 | + comments = select_filtered_collection_of(article, :comments, params) | ||
| 9 | + comments = comments.in_paragraph(params[:paragraph_uuid]) | ||
| 10 | + comments = comments.without_reply if(params[:without_reply].present?) | ||
| 11 | + present paginate(comments), :with => Noosfero::API::Entities::Comment, :current_person => current_person | ||
| 12 | + end | ||
| 13 | + end | ||
| 14 | +end |
| @@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
| 1 | +require File.join(Rails.root,'lib','noosfero','api','entities') | ||
| 2 | +module Noosfero | ||
| 3 | + module API | ||
| 4 | + module Entities | ||
| 5 | + class Comment < CommentBase | ||
| 6 | + expose :paragraph_uuid | ||
| 7 | + expose :comment_paragraph_selected_area | ||
| 8 | + expose :comment_paragraph_selected_content | ||
| 9 | + end | ||
| 10 | + end | ||
| 11 | + end | ||
| 12 | +end |
| @@ -0,0 +1,39 @@ | @@ -0,0 +1,39 @@ | ||
| 1 | +require_relative '../test_helper' | ||
| 2 | +require_relative '../../../../test/api/test_helper' | ||
| 3 | + | ||
| 4 | +class APITest < ActiveSupport::TestCase | ||
| 5 | + | ||
| 6 | + def setup | ||
| 7 | + login_api | ||
| 8 | + environment = Environment.default | ||
| 9 | + environment.enable_plugin(CommentParagraphPlugin) | ||
| 10 | + end | ||
| 11 | + | ||
| 12 | + should 'return custom parameters for each comment' do | ||
| 13 | + article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) | ||
| 14 | + comment = fast_create(Comment, paragraph_uuid: '1', source_id: article.id, author_id: fast_create(Person).id) | ||
| 15 | + comment.comment_paragraph_selected_area = 'area' | ||
| 16 | + comment.comment_paragraph_selected_content = 'content' | ||
| 17 | + comment.save! | ||
| 18 | + params[:paragraph_uuid] = '1' | ||
| 19 | + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" | ||
| 20 | + | ||
| 21 | + json = JSON.parse(last_response.body) | ||
| 22 | + assert_equivalent ['1'], json['comments'].map {|c| c['paragraph_uuid']} | ||
| 23 | + assert_equivalent ['area'], json['comments'].map {|c| c['comment_paragraph_selected_area']} | ||
| 24 | + assert_equivalent ['content'], json['comments'].map {|c| c['comment_paragraph_selected_content']} | ||
| 25 | + end | ||
| 26 | + | ||
| 27 | + should 'return comments that belongs to a paragraph' do | ||
| 28 | + article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) | ||
| 29 | + comment1 = fast_create(Comment, :paragraph_uuid => '1', :source_id => article.id) | ||
| 30 | + comment2 = fast_create(Comment, :paragraph_uuid => nil, :source_id => article.id) | ||
| 31 | + comment3 = fast_create(Comment, :paragraph_uuid => '2', :source_id => article.id) | ||
| 32 | + params[:paragraph_uuid] = '1' | ||
| 33 | + get "/api/v1/articles/#{article.id}/comment_paragraph_plugin/comments?#{params.to_query}" | ||
| 34 | + | ||
| 35 | + json = JSON.parse(last_response.body) | ||
| 36 | + assert_equivalent [comment1.id], json['comments'].map {|c| c['id']} | ||
| 37 | + end | ||
| 38 | + | ||
| 39 | +end |