From 55f041a252bc681419e4f87567ffbde0793e10c7 Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 23 Aug 2017 18:59:23 +0630 Subject: [PATCH] add checksum for menu sync --- .../api/restaurant/menu_controller.rb | 21 +++++++++++++++++-- app/models/menu_item_attribute.rb | 2 +- .../api/restaurant/menu/_menu.json.jbuilder | 1 - .../restaurant/menu/_menu_item.json.jbuilder | 12 +++++------ .../api/restaurant/menu/index.json.jbuilder | 3 ++- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/restaurant/menu_controller.rb b/app/controllers/api/restaurant/menu_controller.rb index b45e1189..ad8ad7f9 100644 --- a/app/controllers/api/restaurant/menu_controller.rb +++ b/app/controllers/api/restaurant/menu_controller.rb @@ -4,8 +4,25 @@ class Api::Restaurant::MenuController < Api::ApiController # Pull the default menu details and also other available (active) menus # Input Params - order_id def index - @menus = Menu.all - # @current_menu = Menu.current_menu + param_checksum = params[:checksum] + # checksum = File.readlines("public/checksums/menu_json.txt").pop.chomp + + all_menu = Menu.all + + # to hash + menu_array = [] + all_menu.each do |m| + menu_array.push(m.to_json(:include => {:menu_categories => { :include => { :menu_items => { :include => [:menu_item_sets, :menu_item_instances => {:include => :menu_instance_item_sets}]} } }})) + end + + #export Checksum file generate by md5 + menu_checksum = Digest::MD5.hexdigest(menu_array.to_json) + + if menu_checksum != param_checksum + response.headers['CHECKSUM'] = menu_checksum + @menus = all_menu + end + # @current_menu = Menu.current_menu end #Description diff --git a/app/models/menu_item_attribute.rb b/app/models/menu_item_attribute.rb index 54d2f34e..e57085cb 100644 --- a/app/models/menu_item_attribute.rb +++ b/app/models/menu_item_attribute.rb @@ -1,6 +1,6 @@ class MenuItemAttribute < ApplicationRecord validates_presence_of :attribute_type, :name, :value def self.collection - MenuItemAttribute.select("name, value").map { |e| [e.name, e.value] } + MenuItemAttribute.select("name").map { |e| [e.name, e.name] } end end diff --git a/app/views/api/restaurant/menu/_menu.json.jbuilder b/app/views/api/restaurant/menu/_menu.json.jbuilder index cfa950d7..29982631 100644 --- a/app/views/api/restaurant/menu/_menu.json.jbuilder +++ b/app/views/api/restaurant/menu/_menu.json.jbuilder @@ -17,5 +17,4 @@ if (menu.menu_categories) end end end - end diff --git a/app/views/api/restaurant/menu/_menu_item.json.jbuilder b/app/views/api/restaurant/menu/_menu_item.json.jbuilder index c76a4808..76b9267f 100644 --- a/app/views/api/restaurant/menu/_menu_item.json.jbuilder +++ b/app/views/api/restaurant/menu/_menu_item.json.jbuilder @@ -5,14 +5,14 @@ param_count = item.item_attributes.count item.item_attributes.each do|attr_id| menu_attr = MenuItemAttribute.find(attr_id) if attr_format.count == 0 - attr_format.push({ type: menu_attr.attribute_type, value: [] }) + attr_format.push({ type: menu_attr.attribute_type, values: [] }) end attr_format.each do |af| if menu_attr.attribute_type == af[:type] - af[:value].push(menu_attr.value) + af[:values].push(menu_attr.name) else - new_attr = {type: menu_attr.attribute_type, value: [ menu_attr.value ] } + new_attr = {type: menu_attr.attribute_type, values: [ menu_attr.name ] } attr_format.push(new_attr) end @@ -29,14 +29,14 @@ param_count = item.item_options.count item.item_options.each do|opt| menu_opt = MenuItemOption.find(opt) if opt_format.count == 0 - opt_format.push({ type: menu_opt.option_type, value: [] }) + opt_format.push({ type: menu_opt.option_type, values: [] }) end opt_format.each do |af| if menu_opt.option_type == af[:type] - af[:value].push(menu_opt.value) + af[:values].push(menu_opt.name) else - new_attr = {type: menu_opt.option_type, value: [ menu_opt.value ] } + new_attr = {type: menu_opt.option_type, values: [ menu_opt.name ] } opt_format.push(new_attr) end diff --git a/app/views/api/restaurant/menu/index.json.jbuilder b/app/views/api/restaurant/menu/index.json.jbuilder index 55e876f6..46f35e60 100644 --- a/app/views/api/restaurant/menu/index.json.jbuilder +++ b/app/views/api/restaurant/menu/index.json.jbuilder @@ -1,4 +1,4 @@ -json.array! @menus do |menu| +menu_json = json.array! @menus do |menu| json.id menu.id json.name menu.name json.valid_days menu.valid_days @@ -12,3 +12,4 @@ json.array! @menus do |menu| # end # end end +