Commit d465f6e8d75dcb41d500180dcd2d8ee6caddedcb

Authored by Victor Costa
1 parent f8559a31

api: accept order param in search endpoint

lib/noosfero/api/v1/search.rb
... ... @@ -19,10 +19,14 @@ module Noosfero
19 19 scope = scope.where(make_conditions_with_parameter(params))
20 20 scope = scope.joins(:categories).where(:categories => {:id => params[:category_ids]}) if params[:category_ids].present?
21 21 scope = scope.where('articles.children_count > 0') if params[:has_children].present?
  22 +
  23 + order = make_order_with_parameters(profile || environment, :articles, params)
  24 + scope = scope.reorder(order)
  25 +
22 26 query = params[:query] || ""
23   - order = "more_recent"
  27 + search_order = "more_recent"
24 28  
25   - options = {:filter => order, :template_id => params[:template_id]}
  29 + options = {:filter => search_order, :template_id => params[:template_id]}
26 30  
27 31 paginate_options = params.select{|k,v| [:page, :per_page].include?(k.to_sym)}.symbolize_keys
28 32 paginate_options.each_pair{|k,v| v=v.to_i}
... ...
test/unit/api/search_test.rb
... ... @@ -156,4 +156,12 @@ class SearchTest < ActiveSupport::TestCase
156 156 assert_equal [article2.id], json['articles'].map {|a| a['id']}
157 157 end
158 158  
  159 + should 'list articles with order' do
  160 + article1 = fast_create(Article, :profile_id => person.id, created_at: Time.now - 1.day)
  161 + article2 = fast_create(Article, :profile_id => person.id, created_at: Time.now)
  162 + params = {order: 'created_at DESC'}
  163 + get "/api/v1/search/article?#{params.to_query}"
  164 + json = JSON.parse(last_response.body)
  165 + assert_equal [article2.id, article1.id], json['articles'].map {|a| a['id']}
  166 + end
159 167 end
... ...