filtering items by traits
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
# converts querystring to hash
|
||||
def get_querystring_hash()
|
||||
Rack::Utils.parse_nested_query(request.query_string)
|
||||
end
|
||||
|
||||
# converts list of parameters to array of integers
|
||||
def mass_to_i(*id_strings)
|
||||
id_strings.map(&:to_i)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
def prepare_items_for_mass_display(items)
|
||||
items.to_json(
|
||||
:except => [:created_at, :current_input_price, :stock, :on_display],
|
||||
@@ -21,6 +19,13 @@ get '/item/:id' do |id_s|
|
||||
prepare_items_for_mass_display(item)
|
||||
end
|
||||
|
||||
def filter_by_traits(items)
|
||||
get_querystring_hash.each do |k,v|
|
||||
items = items.where(["traits ->> '#{k}' = '#{v}'"])
|
||||
end
|
||||
items
|
||||
end
|
||||
|
||||
|
||||
# gets items regardless of classification ( useful for frontpage )
|
||||
get '/item/offset/:offset/limit/:limit' do |offset_s, limit_s|
|
||||
@@ -42,6 +47,7 @@ get '/item/section/:section_id/offset/:offset/limit/:limit' do |section_id_s, of
|
||||
prepare_items_for_mass_display(items)
|
||||
end
|
||||
|
||||
|
||||
# gets items in category ( useful for page showing single category )
|
||||
get '/item/category/:category_id/offset/:offset/limit/:limit' do |category_id_s, offset_s, limit_s|
|
||||
category_id, offset, limit = mass_to_i(category_id_s, offset_s, limit_s)
|
||||
@@ -49,6 +55,8 @@ get '/item/category/:category_id/offset/:offset/limit/:limit' do |category_id_s,
|
||||
return [].to_json if input_invalid
|
||||
|
||||
items = Item.best_selling_in_category(category_id, offset,limit)
|
||||
items = filter_by_traits(items)
|
||||
|
||||
prepare_items_for_mass_display(items)
|
||||
end
|
||||
|
||||
@@ -60,4 +68,4 @@ get '/item/sub_category/:sub_category_id/offset/:offset/limit/:limit' do |sub_ca
|
||||
|
||||
items = Item.best_selling_in_sub_category(sub_category_id, offset, limit)
|
||||
prepare_items_for_mass_display(items)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user