Commit 382224cd by vulehuan

move card update function to new action

parent 76c37c83
......@@ -8,64 +8,34 @@ class CardsController < ApplicationController
end
unless params[:product_id].blank?
if Product.where(id: params[:product_id]).exists?
if !params[:card_action].blank? && params[:card_action] == 'remove'
elsif !params[:card_action].blank? && params[:card_action] == 'update'
# Nothing to update
if @card_infos.empty?
flash[:error] = "Invalid request"
redirect_to cards_path and return
else
quantity = params[:quantity]
# Invalid request
if quantity.blank? || quantity.to_i <= 0
flash[:error] = "Invalid quantity"
redirect_to cards_path and return
else
card_items = @card_infos[:card_items]
card_items.each_with_index do |card_item, key|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
card_item[:quantity] = quantity.to_i
card_items[key] = card_item
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end
end
redirect_to cards_path and return
# if add a product to card
# first time add to card
if @card_infos.empty?
product = Product.find(params[:product_id])
card_items = Array.new
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = {}
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path and return
else
card_items = @card_infos[:card_items]
found = false
card_items.each do |card_item|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
found = true
break
end
end
else
# if add a product to card
# first time add to card
if @card_infos.empty?
product = Product.find(params[:product_id])
card_items = Array.new
# if a product not exist in card
unless found
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = {}
customer_info = @card_infos[:customer_info]
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
redirect_to cards_path and return
else
card_items = @card_infos[:card_items]
found = false
card_items.each do |card_item|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
found = true
break
end
end
# if a product not exist in card
unless found
card_items.push({ product_id: params[:product_id], quantity: 1 })
customer_info = @card_infos[:customer_info]
@card_infos = { card_items: card_items, customer_info: customer_info }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end
redirect_to cards_path and return
end
redirect_to cards_path and return
end
else
flash[:error] = "Invalid product"
......@@ -74,7 +44,7 @@ class CardsController < ApplicationController
end
end
# if remove a product from card
# remove a product from card
def remove
@card_infos = {}
unless session[:SHOPPING_CARD_SESSION_NAME].blank?
......@@ -102,6 +72,42 @@ class CardsController < ApplicationController
end
redirect_to cards_path and return
end
# update quantity
def update
@card_infos = {}
unless session[:SHOPPING_CARD_SESSION_NAME].blank?
@card_infos = session[:SHOPPING_CARD_SESSION_NAME]
end
unless params[:product_id].blank?
# Nothing to update
if @card_infos.empty?
flash[:error] = "Invalid request"
redirect_to cards_path and return
else
quantity = params[:quantity]
# Invalid request
if quantity.blank? || quantity.to_i <= 0
flash[:error] = "Invalid quantity"
redirect_to cards_path and return
else
card_items = @card_infos[:card_items]
card_items.each_with_index do |card_item, key|
# if a product exist in card
if card_item[:product_id] == params[:product_id]
card_item[:quantity] = quantity.to_i
card_items[key] = card_item
@card_infos = { card_items: card_items, customer_info: @card_infos[:customer_info] }
session[:SHOPPING_CARD_SESSION_NAME] = @card_infos
end
end
redirect_to cards_path and return
end
end
end
redirect_to cards_path and return
end
def checkout
add_breadcrumb "Checkout", url_for(action: 'checkout')
@card_infos = {}
......
......@@ -38,7 +38,7 @@
<% if can_edit %>
<td>
<div class="text-center">
<%= link_to(cards_path(product_id: card_item[:product_id], card_action: 'update'), class: 'btn btn-info btn-update-card-quantity') do %>
<%= link_to(url_for(controller: 'cards', action: 'update', product_id: card_item[:product_id]), class: 'btn btn-info btn-update-card-quantity') do %>
<span class="glyphicon glyphicon-plus"></span> Update quantity
<% end %>
<%= link_to(url_for(controller: 'cards', action: 'remove', product_id: card_item[:product_id]), class: 'btn btn-danger', dada: '{:confirm=>"Do you want to remove this product?"}') do %>
......
......@@ -11,6 +11,7 @@ VenshopApp::Application.routes.draw do
post "cards/confirm_checkout"
get "cards/thankyou"
get "cards/remove"
get "cards/update"
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :products
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment