Commit 7e575db7f5b6f4a4571af133f881b6bea48d4746

Authored by Victor Costa
2 parents 8ebfe266 4cf430e9

Merge branch 'api_articles_grandchildren' into 'staging'

API - New endpoint: Get grandchildren of a article

Added new endpoint to get all **grandchildren** of a specific article. The URI of this endpoint is: `:id/children/:child_id/children`

See merge request !6
lib/noosfero/api/v1/articles.rb
@@ -203,7 +203,7 @@ module Noosfero @@ -203,7 +203,7 @@ module Noosfero
203 child.hit 203 child.hit
204 present_partial child, :with => Entities::Article 204 present_partial child, :with => Entities::Article
205 end 205 end
206 - 206 +
207 desc 'Suggest a article to another profile' do 207 desc 'Suggest a article to another profile' do
208 detail 'Suggest a article to another profile (person, community...)' 208 detail 'Suggest a article to another profile (person, community...)'
209 params Noosfero::API::Entities::Article.documentation 209 params Noosfero::API::Entities::Article.documentation
lib/noosfero/api/v1/search.rb
@@ -18,6 +18,7 @@ module Noosfero @@ -18,6 +18,7 @@ module Noosfero
18 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article') 18 scope = scope.where(:type => params[:type]) if params[:type] && !(params[:type] == 'Article')
19 scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present? 19 scope = scope.where(:parent_id => params[:parent_id]) if params[:parent_id].present?
20 scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present? 20 scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present?
  21 + scope = scope.where('children_count > 0') if params[:has_children].present?
21 query = params[:query] || "" 22 query = params[:query] || ""
22 order = "more_recent" 23 order = "more_recent"
23 24
test/unit/api/articles_test.rb
@@ -40,11 +40,12 @@ class ArticlesTest < ActiveSupport::TestCase @@ -40,11 +40,12 @@ class ArticlesTest < ActiveSupport::TestCase
40 end 40 end
41 41
42 should 'list article children' do 42 should 'list article children' do
43 - article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")  
44 - child1 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing")  
45 - child2 = fast_create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some thing") 43 + article = create(Article, :profile_id => user.person.id, :name => "Parent")
  44 + child1 = create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some Child")
  45 + child2 = create(Article, :parent_id => article.id, :profile_id => user.person.id, :name => "Some Child2")
46 get "/api/v1/articles/#{article.id}/children?#{params.to_query}" 46 get "/api/v1/articles/#{article.id}/children?#{params.to_query}"
47 json = JSON.parse(last_response.body) 47 json = JSON.parse(last_response.body)
  48 +
48 assert_equivalent [child1.id, child2.id], json["articles"].map { |a| a["id"] } 49 assert_equivalent [child1.id, child2.id], json["articles"].map { |a| a["id"] }
49 end 50 end
50 51
test/unit/api/search_test.rb
@@ -23,6 +23,16 @@ class SearchTest < ActiveSupport::TestCase @@ -23,6 +23,16 @@ class SearchTest < ActiveSupport::TestCase
23 assert_not_empty json['articles'] 23 assert_not_empty json['articles']
24 end 24 end
25 25
  26 + should 'list only articles that has children' do
  27 + article = fast_create(Article, :profile_id => person.id)
  28 + parent = create(Article, :profile_id => person.id, :name => 'parent article')
  29 + child = create(Article, :profile_id => person.id, :parent_id => parent.id, :name => 'child article')
  30 +
  31 + get "/api/v1/search/article?has_children=true"
  32 + json = JSON.parse(last_response.body)
  33 + assert_equal parent.id, json['articles'].first['id']
  34 + end
  35 +
26 should 'invalid search string articles' do 36 should 'invalid search string articles' do
27 fast_create(Article, :profile_id => person.id, :name => 'some article') 37 fast_create(Article, :profile_id => person.id, :name => 'some article')
28 get "/api/v1/search/article?query=test" 38 get "/api/v1/search/article?query=test"