filtering items by traits
This commit is contained in:
@@ -1,3 +1,7 @@
|
|||||||
|
# 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
|
# converts list of parameters to array of integers
|
||||||
def mass_to_i(*id_strings)
|
def mass_to_i(*id_strings)
|
||||||
|
|||||||
@@ -1,5 +1,3 @@
|
|||||||
|
|
||||||
|
|
||||||
def prepare_items_for_mass_display(items)
|
def prepare_items_for_mass_display(items)
|
||||||
items.to_json(
|
items.to_json(
|
||||||
:except => [:created_at, :current_input_price, :stock, :on_display],
|
: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)
|
prepare_items_for_mass_display(item)
|
||||||
end
|
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 )
|
# gets items regardless of classification ( useful for frontpage )
|
||||||
get '/item/offset/:offset/limit/:limit' do |offset_s, limit_s|
|
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)
|
prepare_items_for_mass_display(items)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# gets items in category ( useful for page showing single category )
|
# 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|
|
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)
|
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
|
return [].to_json if input_invalid
|
||||||
|
|
||||||
items = Item.best_selling_in_category(category_id, offset,limit)
|
items = Item.best_selling_in_category(category_id, offset,limit)
|
||||||
|
items = filter_by_traits(items)
|
||||||
|
|
||||||
prepare_items_for_mass_display(items)
|
prepare_items_for_mass_display(items)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user