63 lines
2.3 KiB
Ruby
63 lines
2.3 KiB
Ruby
|
|
|
|
def prepare_items_for_mass_display(items)
|
|
items.to_json(
|
|
:except => [:created_at, :current_input_price, :stock, :on_display],
|
|
:include => [
|
|
:unit ,
|
|
:multi_media_descriptions ,
|
|
:sub_category
|
|
])
|
|
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
|
|
|
|
|
|
# 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
|
|
|
|
items = Item.best_selling_in_category(category_id, offset,limit)
|
|
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
|
|
|
|
items = Item.best_selling_in_sub_category(sub_category_id, offset, limit)
|
|
prepare_items_for_mass_display(items)
|
|
end |