paging done, needs some additional refactoring

This commit is contained in:
Edin Dazdarevic
2015-02-15 14:21:50 +01:00
parent ba22ea8bd7
commit f675f8b024
11 changed files with 136 additions and 32 deletions

View File

@@ -16,6 +16,7 @@ before do
headers 'Access-Control-Allow-Origin' => 'http://localhost:3001',
'Access-Control-Allow-Methods' => ['OPTIONS', 'GET', 'POST','PUT'],
'Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type, Accept',
'Access-Control-Expose-Headers' => 'X-Total-Count',
'Access-Control-Allow-Credentials' => 'true'
request.body.rewind

View File

@@ -3,6 +3,10 @@ def get_querystring_hash()
Rack::Utils.parse_nested_query(request.query_string)
end
def add_total_count_header(total)
response.headers['X-Total-Count'] = total
end
# converts list of parameters to array of integers
def mass_to_i(*id_strings)
id_strings.map(&:to_i)

View File

@@ -21,7 +21,7 @@ end
def filter_by_traits(items)
get_querystring_hash.each do |k,v|
items = items.where(["traits ->> '#{k}' = '#{v}'"])
items = items.where(["traits ->> ? = ?", k, v])
end
items
end
@@ -54,9 +54,12 @@ get '/item/category/:category_id/offset/:offset/limit/:limit' do |category_id_s,
input_invalid = offset_and_limit_invalid?(offset,limit) or category_id <= 0
return [].to_json if input_invalid
all_in_cat = filter_by_traits(Item.all_in_category(category_id))
items = Item.best_selling_in_category(category_id, offset,limit)
items = filter_by_traits(items)
add_total_count_header(all_in_cat.count)
prepare_items_for_mass_display(items)
end

View File

@@ -10,7 +10,13 @@ class Item < ActiveRecord::Base
# TODO: change "best selling" algorithm when get some data - currently it's only sorted by earnings
scope :best_selling, -> (offset, limit) { where(:on_display => true).order("(list_price - current_input_price) DESC").limit(limit).offset(offset) }
scope :best_selling_in_sub_category, -> (sub_category_id, offset, limit) { best_selling(offset, limit).where(sub_category_id: sub_category_id) }
scope :best_selling_in_category, -> (category_id, offset, limit) { best_selling(offset, limit).joins( sub_category: [:category] ).where(["category_id = ?", category_id]) }
scope :best_selling_in_section, -> (section_id, offset, limit) { best_selling(offset, limit).joins( sub_category: [category: [ :section ]] ).where( ["section_id = ?", section_id] ) }
scope :all_in_category, -> (category_id) { where(on_display: true).joins( sub_category: [:category]).where(["category_id = ?", category_id]) }
end

View File

@@ -1,6 +1,7 @@
class SubCategory < ActiveRecord::Base
belongs_to :category
has_many :filter_criterias, as: :owner
has_many :items
end