109 lines
3.6 KiB
Ruby
109 lines
3.6 KiB
Ruby
def prepare_items_for_mass_display(items)
|
|
items_json = items.to_json(
|
|
:except => [:created_at, :current_input_price, :stock, :on_display],
|
|
:include => [
|
|
:unit ,
|
|
:multi_media_descriptions ,
|
|
:sub_category,
|
|
:brand
|
|
])
|
|
etag xxhash(items_json)
|
|
items_json
|
|
end
|
|
|
|
def offset_and_limit_invalid?(offset, limit)
|
|
offset < 0 or limit <= 0 or limit > 100
|
|
end
|
|
|
|
|
|
# gets single item for detailed display (like comments etc. ) # TODO: change when comments are added
|
|
get '/item/:id' do |id_s|
|
|
item = Item.find(id_s.to_i)
|
|
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|
|
|
offset, limit = mass_to_i(offset_s, limit_s)
|
|
return [].to_json if offset_and_limit_invalid?(offset,limit)
|
|
|
|
items = Item.best_selling(offset,limit)
|
|
prepare_items_for_mass_display(items)
|
|
end
|
|
|
|
|
|
# gets items in section ( useful for page showing single section )
|
|
get '/item/section/:section_id/offset/:offset/limit/:limit' do |section_id_s, offset_s, limit_s|
|
|
section_id, offset, limit = mass_to_i(section_id_s, offset_s, limit_s)
|
|
input_invalid = offset_and_limit_invalid?(offset,limit) or section_id <= 0
|
|
return [].to_json if input_invalid
|
|
|
|
items = Item.best_selling_in_section(section_id, offset, limit)
|
|
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)
|
|
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
|
|
|
|
# gets items in sub category ( useful for page showing single sub_category )
|
|
get '/item/sub_category/:sub_category_id/offset/:offset/limit/:limit' do |sub_category_id_s, offset_s, limit_s|
|
|
|
|
sub_category_id, offset, limit = mass_to_i(sub_category_id_s, offset_s, limit_s)
|
|
input_invalid = offset_and_limit_invalid?(offset,limit) or sub_category_id <= 0
|
|
return [].to_json if input_invalid
|
|
|
|
all_in_sub_cat = filter_by_traits(Item.all_in_sub_category(sub_category_id))
|
|
|
|
items = Item.best_selling_in_sub_category(sub_category_id, offset, limit)
|
|
items = filter_by_traits(items)
|
|
|
|
add_total_count_header(all_in_sub_cat.count)
|
|
|
|
prepare_items_for_mass_display(items)
|
|
end
|
|
|
|
|
|
# gets list of items in cart without count
|
|
get '/cart/item_details' do
|
|
cart = Cart.find_or_create(anonymous_id, -1)
|
|
item_ids = cart.item_in_carts.map do |x|
|
|
x.item_id
|
|
end
|
|
items = []
|
|
items = Item.find(item_ids) if cart.item_in_carts.length > 0
|
|
prepare_items_for_mass_display(items)
|
|
end
|
|
|
|
|
|
# gets items in sub category ( useful for page showing single sub_category )
|
|
get '/item/item_group/:item_group_id/offset/:offset/limit/:limit' do |item_group_id_s, offset_s, limit_s|
|
|
item_group_id, offset, limit = mass_to_i(item_group_id_s, offset_s, limit_s)
|
|
input_invalid = offset_and_limit_invalid?(offset,limit) or item_group_id <= 0
|
|
return [].to_json if input_invalid
|
|
|
|
items = ItemGroup.find_by_id(item_group_id)
|
|
return [].to_json if items.nil?
|
|
prepare_items_for_mass_display(items.all_items(offset, limit))
|
|
end
|