diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index 19deaa5..0838a9a 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -103,15 +103,21 @@ module Noosfero params[:order] || "created_at DESC" end + def by_reference(scope, params) + if params[:reference_id] + created_at = scope.find(params[:reference_id]).created_at + scope.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) + else + scope + end + end + def select_filtered_collection_of(object, method, params) conditions = make_conditions_with_parameter(params) order = make_order_with_parameters(params) objects = object.send(method) - if params[:reference_id] - created_at = objects.find(params[:reference_id]).created_at - objects = objects.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) - end + objects = by_reference(objects, params) objects = objects.where(conditions).limit(limit).order(order) objects diff --git a/lib/noosfero/api/v1/comments.rb b/lib/noosfero/api/v1/comments.rb index 3b88259..980d67b 100644 --- a/lib/noosfero/api/v1/comments.rb +++ b/lib/noosfero/api/v1/comments.rb @@ -15,18 +15,10 @@ module Noosfero # Example Request: # GET /articles/12/comments?oldest&limit=10&reference_id=23 get ":id/comments" do - - conditions = make_conditions_with_parameter(params) article = find_article(environment.articles, params[:id]) + comments = select_filtered_collection_of(article, :comments, params) - if params[:reference_id] - created_at = article.comments.find(params[:reference_id]).created_at - comments = article.comments.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) - else - comments = article.comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) - end present comments, :with => Entities::Comment - end get ":id/comments/:comment_id" do diff --git a/test/unit/api/comments_test.rb b/test/unit/api/comments_test.rb index 59c3fb4..0a718ea 100644 --- a/test/unit/api/comments_test.rb +++ b/test/unit/api/comments_test.rb @@ -25,7 +25,7 @@ class CommentsTest < ActiveSupport::TestCase assert_equal 403, last_response.status end - should 'not comment a article if user has no permission to view it' do + should 'not comment an article if user has no permission to view it' do person = fast_create(Person) article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) assert !article.published? @@ -41,7 +41,28 @@ class CommentsTest < ActiveSupport::TestCase get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" json = JSON.parse(last_response.body) + assert_equal 200, last_response.status assert_equal 2, json["comments"].length end + should 'return comment of an article' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + comment = article.comments.create!(:body => "another comment", :author => user.person) + + get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 200, last_response.status + assert_equal comment.id, json['comment']['id'] + end + + should 'comment an article' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + body = 'My comment' + params.merge!({:body => body}) + + post "/api/v1/articles/#{article.id}/comments?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 201, last_response.status + assert_equal body, json['comment']['body'] + end end -- libgit2 0.21.2