From 553c713a991ef2eaa38320dded283bf9adb093fa Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Sat, 23 Apr 2011 02:15:57 -0300 Subject: [PATCH] Controller and fixes --- plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+), 0 deletions(-) create mode 100644 plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb new file mode 100644 index 0000000..7615491 --- /dev/null +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_profile_controller.rb @@ -0,0 +1,162 @@ +include ActionView::Helpers::NumberHelper + +class ShoppingCartPluginProfileController < ProfileController + + def add + session[:cart] = { :enterprise_id => profile.id, :items => {} } if session[:cart].nil? + if validate_same_enterprise && product = validate_enterprise_has_product(params[:id]) + session[:cart][:items][product.id] = 0 if session[:cart][:items][product.id].nil? + session[:cart][:items][product.id] += 1 + render :text => { + :ok => true, + :error => {:code => 0}, + :products => [{ + :id => product.id, + :name => product.name, + :price => get_price(product), + :description => product.description, + :picture => product.default_image(:minor), + :quantity => session[:cart][:items][product.id] + }] + }.to_json + end + end + + def remove + id = params[:id].to_i + if validate_cart_has_product(id) + session[:cart][:items].delete(id) + session[:cart] = nil if session[:cart][:items].empty? + render :text => { + :ok => true, + :error => {:code => 0}, + :product_id => id + }.to_json + end + end + + def list + if validate_cart_presence + products = session[:cart][:items].collect do |id, quantity| + product = Product.find(id) + { :id => product.id, + :name => product.name, + :price => get_price(product), + :description => product.description, + :picture => product.default_image(:minor), + :quantity => quantity + } + end + render :text => { + :ok => true, + :error => {:code => 0}, + :enterprise => Enterprise.find(session[:cart][:enterprise_id]).identifier, + :products => products + }.to_json + end + end + + def update_quantity + quantity = params[:quantity].to_i + id = params[:id].to_i + if validate_cart_presence && validate_cart_has_product(id) && validate_item_quantity(quantity) + product = Product.find(id) + session[:cart][:items][product.id] = quantity + render :text => { + :ok => true, + :error => {:code => 0}, + :product_id => id, + :quantity => quantity + }.to_json + end + end + + def clean + session[:cart] = nil + render :text => { + :ok => true, + :error => {:code => 0} + }.to_json + end + + private + + def validate_same_enterprise + if profile.id != session[:cart][:enterprise_id] + render :text => { + :ok => false, + :error => { + :code => 1, + :message => _("Can't join items from different enterprises.") + } + }.to_json + return false + end + true + end + + def validate_cart_presence + if session[:cart].nil? + render :text => { + :ok => false, + :error => { + :code => 2, + :message => _("There is no cart.") + } + }.to_json + return false + end + true + end + + def validate_enterprise_has_product(id) + begin + product = profile.products.find(id) + rescue + render :text => { + :ok => false, + :error => { + :code => 3, + :message => _("This enterprise doesn't have this product.") + } + }.to_json + return nil + end + product + end + + def validate_cart_has_product(id) + if !session[:cart][:items].has_key?(id) + render :text => { + :ok => false, + :error => { + :code => 4, + :message => _("The cart doesn't have this product.") + } + }.to_json + return false + end + true + end + + def validate_item_quantity(quantity) + if quantity.to_i < 1 + render :text => { + :ok => false, + :error => { + :code => 5, + :message => _("Invalid quantity.") + } + }.to_json + return false + end + true + end + + def get_price(product) + float_to_currency( product.discount ? product.price_with_discount : product.price ) + end + + +end + -- libgit2 0.21.2