Commit 2027742cb860eb3a9dacfb1cb25224abcc415dd2

Authored by Victor Costa
1 parent 27b6a398
Exists in staging and in 1 other branch production

api: accept order param in search endpoint

lib/noosfero/api/v1/search.rb
@@ -19,10 +19,14 @@ module Noosfero @@ -19,10 +19,14 @@ module Noosfero
19 scope = scope.where(make_conditions_with_parameter(params)) 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('articles.children_count > 0') if params[:has_children].present? 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 query = params[:query] || "" 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 search_result = find_by_contents(asset, context, scope, query, {:page => 1}, options) 31 search_result = find_by_contents(asset, context, scope, query, {:page => 1}, options)
28 32
test/api/search_test.rb
@@ -155,4 +155,12 @@ class SearchTest < ActiveSupport::TestCase @@ -155,4 +155,12 @@ class SearchTest < ActiveSupport::TestCase
155 assert_equal [article2.id], json['articles'].map {|a| a['id']} 155 assert_equal [article2.id], json['articles'].map {|a| a['id']}
156 end 156 end
157 157
  158 + should 'list articles with order' do
  159 + article1 = fast_create(Article, :profile_id => person.id, created_at: Time.now - 1.day)
  160 + article2 = fast_create(Article, :profile_id => person.id, created_at: Time.now)
  161 + params = {order: 'created_at DESC'}
  162 + get "/api/v1/search/article?#{params.to_query}"
  163 + json = JSON.parse(last_response.body)
  164 + assert_equal [article2.id, article1.id], json['articles'].map {|a| a['id']}
  165 + end
158 end 166 end