Commit 738d396bf88ec6a853e624c2775be06be5921597
1 parent
7e575db7
Exists in
staging
and in
3 other branches
api: apply article filters in search method
Showing
3 changed files
with
60 additions
and
1 deletions
Show diff stats
lib/noosfero/api/v1/search.rb
| @@ -16,7 +16,7 @@ module Noosfero | @@ -16,7 +16,7 @@ module Noosfero | ||
| 16 | profile = environment.profiles.find(params[:profile_id]) if params[:profile_id] | 16 | profile = environment.profiles.find(params[:profile_id]) if params[:profile_id] |
| 17 | scope = profile.nil? ? environment.articles.is_public : profile.articles.is_public | 17 | scope = profile.nil? ? environment.articles.is_public : profile.articles.is_public |
| 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(make_conditions_with_parameter(params)) |
| 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 | scope = scope.where('children_count > 0') if params[:has_children].present? |
| 22 | query = params[:query] || "" | 22 | query = params[:query] || "" |
| @@ -0,0 +1,50 @@ | @@ -0,0 +1,50 @@ | ||
| 1 | +module Noosfero | ||
| 2 | + module API | ||
| 3 | + module V1 | ||
| 4 | + class Search < Grape::API | ||
| 5 | + | ||
| 6 | + resource :search do | ||
| 7 | + resource :article do | ||
| 8 | + paginate per_page: 20, max_per_page: 200 | ||
| 9 | + get do | ||
| 10 | + # Security checks | ||
| 11 | + sanitize_params_hash(params) | ||
| 12 | + # APIHelpers | ||
| 13 | + asset = :articles | ||
| 14 | + context = environment | ||
| 15 | + | ||
| 16 | + profile = environment.profiles.find(params[:profile_id]) if params[:profile_id] | ||
| 17 | + scope = profile.nil? ? environment.articles.is_public : profile.articles.is_public | ||
| 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? | ||
| 20 | + scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present? | ||
| 21 | +<<<<<<< HEAD | ||
| 22 | + | ||
| 23 | + scope = scope.where('children_count > 0') if params[:has_children].present? | ||
| 24 | + | ||
| 25 | +======= | ||
| 26 | +>>>>>>> staging | ||
| 27 | + query = params[:query] || "" | ||
| 28 | + order = "more_recent" | ||
| 29 | + | ||
| 30 | + options = {:filter => order, :template_id => params[:template_id]} | ||
| 31 | + | ||
| 32 | + paginate_options = params.select{|k,v| [:page, :per_page].include?(k.to_sym)}.symbolize_keys | ||
| 33 | + paginate_options.each_pair{|k,v| v=v.to_i} | ||
| 34 | + paginate_options[:page]=1 if !paginate_options.keys.include?(:page) | ||
| 35 | + | ||
| 36 | + search_result = find_by_contents(asset, context, scope, query, paginate_options, options) | ||
| 37 | + | ||
| 38 | + articles = search_result[:results] | ||
| 39 | + | ||
| 40 | + result = present_articles_paginated(articles) | ||
| 41 | + | ||
| 42 | + result | ||
| 43 | + end | ||
| 44 | + end | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | + end | ||
| 48 | + end | ||
| 49 | + end | ||
| 50 | +end |
test/unit/api/search_test.rb
| @@ -147,4 +147,13 @@ class SearchTest < ActiveSupport::TestCase | @@ -147,4 +147,13 @@ class SearchTest < ActiveSupport::TestCase | ||
| 147 | assert_includes ids, json['articles'].last["id"] | 147 | assert_includes ids, json['articles'].last["id"] |
| 148 | end | 148 | end |
| 149 | 149 | ||
| 150 | + should 'list only articles that was archived' do | ||
| 151 | + article1 = fast_create(Article, :profile_id => person.id) | ||
| 152 | + article2 = fast_create(Article, :profile_id => person.id, archived: true) | ||
| 153 | + | ||
| 154 | + get "/api/v1/search/article?archived=true" | ||
| 155 | + json = JSON.parse(last_response.body) | ||
| 156 | + assert_equal [article2.id], json['articles'].map {|a| a['id']} | ||
| 157 | + end | ||
| 158 | + | ||
| 150 | end | 159 | end |