Commit b34e0fa06c4baeb3479e167a1c9cae11893b1d33
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Merge branch 'api-search' of gitlab.serpro:noosfero/noosfero into staging
Showing
3 changed files
with
24 additions
and
6 deletions
Show diff stats
lib/noosfero/api/helpers.rb
| @@ -118,7 +118,11 @@ require 'grape' | @@ -118,7 +118,11 @@ require 'grape' | ||
| 118 | 118 | ||
| 119 | def present_articles(asset, method = 'articles') | 119 | def present_articles(asset, method = 'articles') |
| 120 | articles = find_articles(asset, method) | 120 | articles = find_articles(asset, method) |
| 121 | - articles = paginate articles | 121 | + present_articles_paginated(articles) |
| 122 | + end | ||
| 123 | + | ||
| 124 | + def present_articles_paginated(articles, per_page=nil) | ||
| 125 | + articles = paginate(articles) | ||
| 122 | present articles, :with => Entities::Article, :fields => params[:fields] | 126 | present articles, :with => Entities::Article, :fields => params[:fields] |
| 123 | end | 127 | end |
| 124 | 128 |
lib/noosfero/api/v1/search.rb
| @@ -5,6 +5,7 @@ module Noosfero | @@ -5,6 +5,7 @@ module Noosfero | ||
| 5 | 5 | ||
| 6 | resource :search do | 6 | resource :search do |
| 7 | resource :article do | 7 | resource :article do |
| 8 | + paginate per_page: 20, max_per_page: 200 | ||
| 8 | get do | 9 | get do |
| 9 | # Security checks | 10 | # Security checks |
| 10 | sanitize_params_hash(params) | 11 | sanitize_params_hash(params) |
| @@ -25,9 +26,13 @@ module Noosfero | @@ -25,9 +26,13 @@ module Noosfero | ||
| 25 | 26 | ||
| 26 | options = {:filter => order, :template_id => params[:template_id], :category => category} | 27 | options = {:filter => order, :template_id => params[:template_id], :category => category} |
| 27 | 28 | ||
| 28 | - articles = find_by_contents(asset, context, scope, query, paginate_options, options) | 29 | + search_result = find_by_contents(asset, context, scope, query, paginate_options, options) |
| 29 | 30 | ||
| 30 | - present articles[:results], :with => Entities::Article | 31 | + articles = search_result[:results] |
| 32 | + | ||
| 33 | + result = present_articles_paginated(articles) | ||
| 34 | + | ||
| 35 | + result | ||
| 31 | end | 36 | end |
| 32 | end | 37 | end |
| 33 | end | 38 | end |
test/unit/api/search_test.rb
| @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/test_helper' | @@ -3,7 +3,7 @@ require File.dirname(__FILE__) + '/test_helper' | ||
| 3 | class SearchTest < ActiveSupport::TestCase | 3 | class SearchTest < ActiveSupport::TestCase |
| 4 | 4 | ||
| 5 | def create_article_with_optional_category(name, profile, category = nil) | 5 | def create_article_with_optional_category(name, profile, category = nil) |
| 6 | - fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category) | 6 | + fast_create(Article, {:name => name, :profile_id => profile.id }, :search => true, :category => category, :title => name) |
| 7 | end | 7 | end |
| 8 | 8 | ||
| 9 | should 'not list unpublished articles' do | 9 | should 'not list unpublished articles' do |
| @@ -64,7 +64,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -64,7 +64,7 @@ class SearchTest < ActiveSupport::TestCase | ||
| 64 | art = create_article_with_optional_category("Article #{n}", person) | 64 | art = create_article_with_optional_category("Article #{n}", person) |
| 65 | end | 65 | end |
| 66 | 66 | ||
| 67 | - get "/api/v1/search/article?query=Article&limit=3" | 67 | + get "/api/v1/search/article?query=Article&per_page=3" |
| 68 | json = JSON.parse(last_response.body) | 68 | json = JSON.parse(last_response.body) |
| 69 | 69 | ||
| 70 | assert_equal 3, json['articles'].count | 70 | assert_equal 3, json['articles'].count |
| @@ -76,7 +76,7 @@ class SearchTest < ActiveSupport::TestCase | @@ -76,7 +76,7 @@ class SearchTest < ActiveSupport::TestCase | ||
| 76 | art = create_article_with_optional_category("Article #{n}", person) | 76 | art = create_article_with_optional_category("Article #{n}", person) |
| 77 | end | 77 | end |
| 78 | 78 | ||
| 79 | - get "/api/v1/search/article?query=Article&limit=3&page=2" | 79 | + get "/api/v1/search/article?query=Article&per_page=3&page=2" |
| 80 | json = JSON.parse(last_response.body) | 80 | json = JSON.parse(last_response.body) |
| 81 | 81 | ||
| 82 | assert_equal 2, json['articles'].count | 82 | assert_equal 2, json['articles'].count |
| @@ -95,4 +95,13 @@ class SearchTest < ActiveSupport::TestCase | @@ -95,4 +95,13 @@ class SearchTest < ActiveSupport::TestCase | ||
| 95 | # Only for person1 | 95 | # Only for person1 |
| 96 | assert_equal 2, json['articles'].count | 96 | assert_equal 2, json['articles'].count |
| 97 | end | 97 | end |
| 98 | + | ||
| 99 | + should 'search with fields' do | ||
| 100 | + person = fast_create(Person) | ||
| 101 | + art = create_article_with_optional_category('an article to be found', person) | ||
| 102 | + get "/api/v1/search/article?fields=title" | ||
| 103 | + json = JSON.parse(last_response.body) | ||
| 104 | + assert_not_empty json['articles'] | ||
| 105 | + assert_equal ['title'], json['articles'].first.keys | ||
| 106 | + end | ||
| 98 | end | 107 | end |
| 99 | \ No newline at end of file | 108 | \ No newline at end of file |