Commit ba3ecacb by Nguyen Quoc Kien

Cars: finish

parent 43e93f27
...@@ -8,14 +8,13 @@ class ApplicationController < ActionController::Base ...@@ -8,14 +8,13 @@ class ApplicationController < ActionController::Base
private private
def set_cart def set_cart
@cart = Cart.find(session[:cart_id]) @session = session
rescue ActiveRecord::RecordNotFound
if current_user if current_user
@cart = Cart.create(user_id: current_user.id) @user_id = current_user.id
else else
@cart = Cart.create() @user_id = "guess"
end end
session[:cart_id] = @cart.id @session[@user_id] ||= {}
end end
def configure_permitted_parameters def configure_permitted_parameters
......
class CartProductsController < ApplicationController class CartProductsController < ApplicationController
before_action :set_cart, only: [:create] before_action :set_cart, only: [:create]
before_action :check_quantity?, only: [:create]
def create def create
product = Product.find(params[:product_id]) product = Product.find(params[:product_id])
@cart_product = @cart.add_product(product.id, product.price) if check_quantity?
add_product_to_cart(product.id.to_i, params[:quantity].to_i )
respond_to do |format| respond_to do |format|
if @cart_product.save format.html { redirect_to cart_path(id: @user_id),
format.html { redirect_to @cart_product.cart } notice: 'Products add to cart' }
format.json { render json: @cart_product, format.json { head :no_content }
status: :created, location: @cart_product } end
else else
format.html { render action: "new" } respond_to do |format|
format.json { render json: @cart_product.errors, format.html { redirect_to cart_path(id: @user_id),
status: :unprocessable_entity } notice: 'Errors: Quantity' }
format.json { head :no_content }
end end
end end
end end
def destroy def destroy
@cart_product = CartProduct.find(params[:id]) session[params[:id]].delete(params[:product_id])
@cart_product.destroy redirect_to cart_path(id: params[:id])
redirect_to @cart_product.cart end
private
def add_product_to_cart(product_id, number)
number ||= 1
i = 0
@session[@user_id].each do |key, value|
if (key == product_id.to_s)
@session[@user_id][key] = number +value
i = 1
break
end
end
if (i == 0)
@session[@user_id][product_id] = number
end
end
def check_quantity?
if ( params[:quantity].to_i && params[:quantity].to_i > 0 )
return true
else false
end
end end
end end
class CartsController < ApplicationController class CartsController < ApplicationController
before_action :find_card, only: [ :update ] #before_action :find_card, only: [ :create ]
def show #before_action :check_phone, only: [ :create ]
@cart = Cart.find(session[:cart_id])
total_price = @cart.total_price def update
@cart.update(total_price: total_price)
end end
def edit def new
@cart = Cart.find(session[:cart_id]) @cart = Cart.new
end end
def update def create
@cart = Cart.find(session[:cart_id]) total = 0
@user = User.find(@cart.user_id)
@user.update(address: params['cart']['address'], phone: params['cart']['phone']) @cart = Cart.new(cart_params)
@cart.update(cart_params) @cart.save
@cart.update(status: "checkout") if current_user
@cart.update(user_id: current_user.id, status: "Checkout")
user_id = current_user.id
else
@cart.update(user_id: "", status: "Checkout")
user_id = 'guess'
end
if @cart.save
session[user_id].each do |key, value|
@product = Product.find(key)
@cart_product = CartProduct.new(cart_id: @cart.id, product_id: key.to_i, number: value.to_i, price: @product.price)
@cart_product.save
total += @product.price * value.to_f
end
@cart.update( total_price: total)
OrderNotifier.received(@cart).deliver OrderNotifier.received(@cart).deliver
session[:cart_id] = nil
respond_to do |format| respond_to do |format|
format.html { redirect_to products_path, format.html { redirect_to products_path,
notice: 'Email to send' } notice: 'Email to send' }
format.json { head :no_content } format.json { head :no_content }
end end
end session[user_id] = nil
else
def destroy
@cart.destroy if @cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format| respond_to do |format|
format.html { redirect_to products_path, format.html { redirect_to carts_path,
notice: 'Your cart is currently empty' } notice: 'Errors' }
format.json { head :no_content } format.json { head :no_content }
end end
end end
end
private def destroy
def cart_params session[params[:id]] = nil
params.require(:cart).permit(:full_name, :email, :address, :phone) redirect_to cart_path(params[:id])
end end
def find_card def index
@cart = Cart.find(params[:id]) if current_user
user_id = current_user.id
else
user_id = 'guess'
end
redirect_to cart_path(user_id)
end end
def add_product_to_cart(product_id, quantity)
quantity ||= 1
product_id = product_id.to_s
current_quantity = cart_products_hash.fetch(product_id, {}).fetch('quantity', 0)
quantity += current_quantity
cart_products_hash[product_id] = { 'quantity' => quantity } private
end
def remove_product_from_cart(product_id) def cart_params
product_id = product_id.to_s params.require(:cart).permit(:full_name, :email, :address, :phone)
cart_products_hash.delete(product_id)
end end
def cart_hash def find_card
@session['cart'] @cart = Cart.find(params[:id])
end end
def cart_products_hash def check_phone
@session['cart']['products'] params[:phone].is_a?
end end
end end
...@@ -11,9 +11,13 @@ class ProductsController < ApplicationController ...@@ -11,9 +11,13 @@ class ProductsController < ApplicationController
@categories = Category.all @categories = Category.all
end end
def new
end
private private
def find_product def find_product
@product = Product.find(params[:id]) @product = Product.find(params[:id])
end end
end end
class Cart < ActiveRecord::Base class Cart < ActiveRecord::Base
has_many :cart_products, dependent: :destroy has_many :cart_products, dependent: :destroy
def add_product(product_id, price) VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
current_item = cart_products.find_by(product_id: product_id) validates :email, presence: true, length: { maximum: 255 },
if current_item format: { with: VALID_EMAIL_REGEX }
current_item.number += 1 validates :full_name, presence: true, length: { maximum: 50 }
validates :address, presence: true, length: { maximum: 1000 }
before_save :downcase_email
def downcase_email
self.email = email.downcase
end
def check_phone
if self.phone.is_a
return true
else else
current_item = cart_products.build(product_id: product_id, price: price) return false
end end
current_item
end end
def total_price
cart_products.to_a.sum { |item| item.total_price }
end
end end
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<div class="row"> <div class="row">
<div class="col-md-6 col-md-offset-3"> <div class="col-md-6 col-md-offset-3">
<h1>Đăng ký thông tin nhận hàng</h1> <h1>Đăng ký thông tin nhận hàng</h1>
<%= form_for(@cart) do |f| %> <%= form_for @cart do |f| %>
<%= f.label :full_name %> <%= f.label :full_name %>
<%= f.text_field :full_name, class: 'form-control' %> <%= f.text_field :full_name, class: 'form-control' %>
......
<% provide(:title, "Your Carts") %> <% provide(:title, "Your Carts") %>
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<h2>Your Cart</h2> <h2>Your Cart</h2>
<table class="table table-hover"> <table class="table table-hover">
<% if session[params[:id]] != nil && session[params[:id]] != {} %>
<thead> <thead>
<tr> <tr>
<th>Product name</th> <th>Product name</th>
...@@ -14,30 +12,34 @@ ...@@ -14,30 +12,34 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% @cart.cart_products.each do |item| %> <% total = 0 %>
<% session[params[:id]].each do |key, value| %>
<tr> <tr>
<td><%= item.product.name %></td> <td><%= Product.find(key).name %>
<td><%= number_to_currency(item.product.price/100.000) %></td> <td><%= number_to_currency(Product.find(key).price/100.00) %></td>
<td><%= item.number %></td> <td><%= value %></td>
<td><%= number_to_currency((item.product.price * item.number)/100.000) %></td> <td><%= number_to_currency(Product.find(key).price/100.000 * value.to_f) %></td>
<td > <% total += Product.find(key).price/100.000 * value.to_f %>
<%= link_to 'Delete', cart_product_path(id: item.id), data: { confirm: 'Are you sure?' }, method: :delete, <td><%= link_to 'Delete', cart_product_path(product_id: key, id: params[:id]), data: { confirm: 'Are you sure?' }, method: :delete,
data: { confirm: 'Are you sure?' } %> | data: { confirm: 'Are you sure?' } %> </td>
<%= link_to 'Update', "#" %>
</td>
</tr> </tr>
<% end %> <% end %>
<tr> <tr>
<td colspan="3">Total: </td> <td>Total</td>
<td><%= number_to_currency(@cart.total_price/100.000) %></td> <td></td>
<td></td>
<td> <%= number_to_currency(total) %></td>
</tr> </tr>
<tr> <tr>
<td><%= link_to 'Back', products_path, class: "btn btn-danger" %></td> <td><%= link_to 'Back', products_path, class: "btn btn-danger" %></td>
<td> </td> <td> </td>
<td><%= button_to 'Empty cart', @cart, method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td> <td><%= button_to 'Empty cart', cart_path(id: params[:id]), method: :delete, data: { confirm: 'Are you sure?' }, class: "btn btn-danger" %></td>
<td><%= button_to 'Checkout', edit_cart_path(@cart), method: :get, class: "btn btn-danger" %></td> <td><%= link_to 'Checkout', new_cart_path, class: "btn btn-danger" %></td>
</tr> <td> </td>
</tr> </tr>
</tbody> </tbody>
<% else %>
<h1>Cart Empty</h1>
<% end %>
</table> </table>
\ No newline at end of file
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<li> <li>
<%= link_to "Categories", categories_path %> <%= link_to "Categories", categories_path %>
</li> </li>
<li><%= link_to "Carts", "#" %></li> <li><%= link_to "Carts", carts_path %></li>
</ul> </ul>
<ul class="nav navbar-nav navbar-right"> <ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %> <% if user_signed_in? %>
......
<h1>OrderNotifier#shipped</h1> <h1>OrderNotifier#shipped</h1>
<p> <p>
<%= @greeting %>, find me in app/views/order_notifier/shipped.html.erb <%= @greeting %>, 45
</p> </p>
...@@ -6,9 +6,14 @@ ...@@ -6,9 +6,14 @@
<h3 title="<%= product.name %>"><%= truncate(product.name, length: 25) %></h3> <h3 title="<%= product.name %>"><%= truncate(product.name, length: 25) %></h3>
</div> </div>
<p><b>Price: </b><%= number_to_currency(product.price/100.000) %></p> <p><b>Price: </b><%= number_to_currency(product.price/100.000) %></p>
<p>Quanlity: <%= text_field :number, class: 'form-control' %> <%= form_tag cart_products_path do %>
<%= button_to "Add To cart", cart_products_path(product_id: product), :class => "btn btn-primary", :style => "width: 100px" %> <br/><%= link_to "More Info", product, :class => "btn btn-default" %> <p>
<%= hidden_field_tag :product_id, product.id %>
Quantity:<b> <%= number_field_tag :quantity, "1", class: 'form-control' %></b><br/>
<%= submit_tag "Add To cart", :class => "btn btn-primary", :style => "width: 100px" %>
</p> </p>
<% end %>
<%= link_to "More Info", product, :class => "btn btn-default" %>
</div> </div>
</div> </div>
</div> </div>
...@@ -5,4 +5,5 @@ ...@@ -5,4 +5,5 @@
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
</p> </p>
<p><%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-lg btn-primary" %></p> <p><%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-lg btn-primary" %></p>
<p><%= link_to "Admin", admins_path, class: "btn btn-lg btn-primary" %></p>
</header> </header>
Rails.application.routes.draw do Rails.application.routes.draw do
devise_for :users
get 'carts/index' get 'carts/index'
devise_for :users
root to: "static_pages#home" root to: "static_pages#home"
get 'help' => 'static_pages#help' get 'help' => 'static_pages#help'
...@@ -11,6 +11,7 @@ Rails.application.routes.draw do ...@@ -11,6 +11,7 @@ Rails.application.routes.draw do
resources :products resources :products
resources :carts resources :carts
resources :cart_products, only: [:create, :destroy] resources :cart_products, only: [:create, :destroy]
resources :admins
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes". # See how all your routes lay out with "rake routes".
......
...@@ -3,7 +3,7 @@ class CreateCarts < ActiveRecord::Migration ...@@ -3,7 +3,7 @@ class CreateCarts < ActiveRecord::Migration
create_table :carts do |t| create_table :carts do |t|
t.integer :user_id t.integer :user_id
t.decimal :total_price, :default => 0 t.decimal :total_price, :default => 0
t.string :status, :default => "Prosess" t.string :status
t.string :full_name t.string :full_name
t.integer :phone t.integer :phone
t.string :email t.string :email
......
...@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do ...@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150727094623) do
create_table "carts", force: :cascade do |t| create_table "carts", force: :cascade do |t|
t.integer "user_id", limit: 4 t.integer "user_id", limit: 4
t.decimal "total_price", precision: 10, default: 0 t.decimal "total_price", precision: 10, default: 0
t.string "status", limit: 255, default: "Prosess" t.string "status", limit: 255
t.string "full_name", limit: 255 t.string "full_name", limit: 255
t.integer "phone", limit: 4 t.integer "phone", limit: 4
t.string "email", limit: 255 t.string "email", limit: 255
......
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