Commit 9d557a5d by Nguyen Quoc Kien

Validate - admin

parent ad1fccc0
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
// Place all the styles related to the search controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the shopping_history controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class Admin::CartsController < ApplicationController
before_action :authenticate_admin!
def index
@users = User.all
end
......
class Admin::ProductsController < ApplicationController
before_action :find_product, only: [:destroy, :edit,:update]
before_action :authenticate_admin!
def index
@products = Product.paginate(page: params[:page]).per_page(21)
......@@ -10,7 +11,7 @@ class Admin::ProductsController < ApplicationController
if @product.destroy
flash[:success] = "Delete product : Success"
else
flash[:success] = "Create product : Error"
flash[:danger] = "Delete product : Error - Product add to carts"
end
redirect_to admin_products_path
end
......@@ -30,21 +31,27 @@ class Admin::ProductsController < ApplicationController
price: params[:product][:price].to_i,
image: params[:product][:image],
description: params[:product][:description])
if @product.save
if @product.check_valid()
@product.save
flash[:success] = "Create product : Success"
redirect_to admin_products_path
else
flash[:danger] = "Error: Create product"
flash[:danger] = "Error"
redirect_to new_admin_product_path
end
end
def update
if params[:product][:price].to_i > 0
if@product.check_valid()
@product.update(product_params)
flash[:success] = "Update product : Success"
redirect_to admin_products_path
else
flash[:danger] = "Error"
redirect_to edit_admin_product_path(id: params[:id])
end
else
flash[:danger] = "Error: Price"
redirect_to edit_admin_product_path(id: params[:id])
end
......
class Admin::UsersController < ApplicationController
before_action :authenticate_admin!
def index
@users = User.paginate(page: params[:page]).per_page(21)
end
def destroy
@user = User.find(params[:id])
if @user.destroy
flash[:success] = "Delete User : Success"
else
flash[:danger] = "Delete User : Error"
end
redirect_to admin_users_path
end
end
class CartProductsController < ApplicationController
before_action :set_cart, only: [:create]
before_action :check_quantity?, only: [:create]
def create
product = Product.find(params[:product_id])
if check_quantity?
......
class CartsController < ApplicationController
#before_action :find_card, only: [ :create ]
#before_action :check_phone, only: [ :create ]
def update
end
......@@ -21,6 +20,7 @@ class CartsController < ApplicationController
@cart.update(user_id: "", status: "Checkout")
user_id = 'guess'
end
if @cart.save
session[user_id].each do |key, value|
@product = Product.find(key)
......@@ -29,19 +29,14 @@ class CartsController < ApplicationController
total += @product.price * value.to_f
end
@cart.update( total_price: total)
update_info_user()
OrderNotifier.received(@cart).deliver
respond_to do |format|
format.html { redirect_to products_path,
notice: 'Email to send' }
format.json { head :no_content }
end
session[user_id] = nil
flash[:success] = "Email to send"
redirect_to products_path
else
respond_to do |format|
format.html { redirect_to carts_path,
notice: 'Errors' }
format.json { head :no_content }
end
flash[:danger] = "Error"
redirect_to carts_path
end
end
......@@ -59,8 +54,6 @@ class CartsController < ApplicationController
redirect_to cart_path(user_id)
end
private
def cart_params
......@@ -71,8 +64,16 @@ class CartsController < ApplicationController
@cart = Cart.find(params[:id])
end
def check_phone
params[:phone].is_a?
def update_info_user
if user_signed_in?
user = User.find(current_user.id)
if user.phone == nil
user.update(phone: params[:cart][:phone])
end
if user.address == nil
user.update(address: params[:cart][:address])
end
end
end
end
class SearchController < ApplicationController
def search
if params[:keyword].nil?
@products = []
else
@products = Product.where("name like '%#{params[:keyword]}%'").paginate(page: params[:page]).per_page(18)
end
end
end
class ShoppingHistoryController < ApplicationController
def index
@user = User.find(current_user.id)
@carts_to_user = Cart.where(user_id: @user.id)
end
end
module SearchHelper
end
module ShoppingHistoryHelper
end
......@@ -11,6 +11,7 @@ class Admin < ActiveRecord::Base
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
......@@ -25,4 +26,5 @@ class Admin < ActiveRecord::Base
def downcase_email
self.email = email.downcase
end
end
class Cart < ActiveRecord::Base
has_many :cart_products, dependent: :destroy
VALID_PHONE_REGEX = /\d[0-9]\)*\z/
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
validates :phone, presence: true, length: { maximum: 15 },
format: { with: VALID_PHONE_REGEX }
validates :total_price, presence: true, format: { with: VALID_NUMBER_REGEX }
validates :full_name, presence: true, length: { maximum: 50 }
validates :address, presence: true, length: { maximum: 1000 }
before_save :downcase_email
private
def downcase_email
self.email = email.downcase
end
def check_phone
if self.phone.is_a
return true
else
return false
end
def check_valid
end
end
......@@ -2,6 +2,11 @@ class CartProduct < ActiveRecord::Base
belongs_to :product
belongs_to :cart
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :number, presence: true, format: { with: VALID_NUMBER_REGEX }
validates :price, presence: true, format: { with: VALID_NUMBER_REGEX }
def total_price
product.price * number
end
......
......@@ -6,10 +6,24 @@ class Product < ActiveRecord::Base
has_many :cart_products
before_destroy :ensure_not_referenced_by_any_cart_product
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :category_id, presence: true
validates :image, presence: true, length: { maximum: 1000 }
validates :description, presence: true, length: { maximum: 65535 }
validates :price, presence: true, format: { with: VALID_NUMBER_REGEX }
def check_valid
if self.price < 0
return false
end
if self.description == nil
return false
end
if self.image == nil
return false
end
end
private
......
......@@ -7,14 +7,19 @@ class User < ActiveRecord::Base
:registerable, :recoverable,
:rememberable, :trackable, :validatable,
:authentication_keys => [:login]
VALID_PHONE_REGEX = /\d[0-9]\)*\z/
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
before_save :downcase_email
validates :phone, length: { maximum: 12 },
format: { with: VALID_PHONE_REGEX }
validates :username, :presence => true, length: { maximum: 50 }, :uniqueness => { :case_sensitive => false }
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
before_save :downcase_email
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
......
<div class="col-md-3">
<div class="col-md-12">
<p class="lead">Admin - Carts</p>
<div id="searchlist" class="list-group">
<%= link_to "Buyers", admin_cart_path(id: "buyers"), :class => "list-group-item "%>
......
......@@ -18,30 +18,35 @@
<tbody>
<% product = Product.find(cart_product.product_id) %>
<tr>
<td><%= product.name %>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= cart_product.number %></td>
<td><%= number_to_currency(cart_product.price/100.00 * cart_product.number) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total</td>
<td><b><%= number_to_currency(cart.total_price) %></b></td>
<td colspan="3">Total:</td>
<td><b><%= number_to_currency(cart.total_price/100.00) %></b></td>
</tr>
<tr>
<td>Name: </td>
<td colspan="3"> <%= cart.full_name %>
</tr>
<tr>
<td colspan="2">Name: </td>
<td colspan="2"> <%= cart.full_name %>
<td>E-Mail: </td>
<td colspan="3"> <%= cart.email %>
</tr>
<tr>
<td colspan="2">Phone: </td>
<td colspan="2"> <%= cart.phone %>
<td>Phone: </td>
<td colspan="3"> <%= cart.phone %>
</tr>
<tr>
<td colspan="2">Address: </td>
<td colspan="2"> <%= cart.address %>
<td>Address: </td>
<td colspan="3"> <%= cart.address %>
</tr>
<tr>
<td colspan="2">Status: </td>
<td colspan="2"><%= cart.status %></td>
<td>Status: </td>
<td colspan="3"><%= cart.status %></td>
</tr>
<tr>
<% if cart.status != "Finish" %>
......
......@@ -18,30 +18,35 @@
<tbody>
<% product = Product.find(cart_product.product_id) %>
<tr>
<td><%= product.name %>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= cart_product.number %></td>
<td><%= number_to_currency(cart_product.price/100.00 * cart_product.number) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total</td>
<td><b><%= number_to_currency(cart_to_user.total_price) %></b></td>
<td><b><%= number_to_currency(cart_to_user.total_price/100.00) %></b></td>
</tr>
<tr>
<td>Name: </td>
<td colspan="3"> <%= cart_to_user.full_name %>
</tr>
<tr>
<td colspan="2">Name: </td>
<td colspan="2"> <%= cart_to_user.full_name %>
<td>E-Mail: </td>
<td colspan="3"> <%= cart_to_user.email %>
</tr>
<tr>
<td colspan="2">Phone: </td>
<td colspan="2"> <%= cart_to_user.phone %>
<td>Phone: </td>
<td colspan="3"> <%= cart_to_user.phone %>
</tr>
<tr>
<td colspan="2">Address: </td>
<td colspan="2"> <%= cart_to_user.address %>
<td>Address: </td>
<td colspan="3"> <%= cart_to_user.address %>
</tr>
<tr>
<td colspan="2">Status: </td>
<td colspan="2"><%= cart_to_user.status %></td>
<td>Status: </td>
<td colspan="3"><%= cart_to_user.status %></td>
</tr>
<tr>
<% if cart_to_user.status != "Finish" %>
......
<% if @user %>
<% render 'admin/carts/show_to_user_id' %>
<%= render 'admin/carts/show_to_user_id' %>
<% else %>
<%= render 'admin/carts/show_to_buyers' %>
<% end %>
\ No newline at end of file
<tr>
<tr>
<td><%= product.id %></td>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
......@@ -7,4 +7,4 @@
<th><%= link_to "Delete", admin_product_path(id: product.id), method: :delete, data: { confirm: 'Are you sure?' } %>
<%= link_to "Edit", edit_admin_product_path(id: product.id) %>
</th>
</tr>
</tr>
<% provide(:title, "Edit Products") %>
<h2 style="text-align: center;">Edit products <%= @product.id %></h2>
<div class="col-md-9 col-md-offset-3">
<div class="col-9">
<%= form_for [:admin, @product] do |f| %>
<div class="field">
<%= f.label :name %><br />
......
......@@ -5,7 +5,6 @@
<div class="col-lg-12">
<h2 class="text-left">All Products</h2>
<%= will_paginate @products %>
<h2>Your Cart</h2>
<table class="table table-hover">
<thead>
<tr>
......
<% provide(:title, "New Products") %>
<h2 style="text-align: center;">New products </h2>
<div class="col-md-9 col-md-offset-3">
<div class="col-9 ">
<%= form_for [:admin, @product] do |f| %>
<div class="field">
<%= f.label :name %><br />
......
<% provide(:title, "All Users") %>
<% if notice %>
<p id="notice"><%= notice %></p>
<% end %>
<div class="col-lg-12">
<h2 class="text-left">All Users</h2>
<%= will_paginate @users %>
<table class="table table-hover">
<thead>
<tr>
<th>ID User</th>
<th>Username</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th>Admin</th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<td><%= user.id %></td>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= user.phone %></td>
<td><%= user.address %> </td>
<td><%= link_to "Delete", admin_user_path(id: user.id), method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @users %>
......@@ -14,7 +14,7 @@
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
<%= f.number_field :phone, class: 'form-control' %>
<br>
<%= f.submit "Save changes", class: "btn btn-primary" %>
......
......@@ -21,6 +21,7 @@
</ul>
<ul class="nav navbar-nav navbar-right">
<% if user_signed_in? %>
<li><%= link_to "Shopping history", shopping_history_index_path %></li>
<li><%= link_to "Users", "" %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
......@@ -38,14 +39,11 @@
<% end %>
<% if admin_signed_in? %>
<%= render 'layouts/header_admin' %>
<% else %>
<% end %>
<% if !admin_signed_in? && !user_signed_in? %>
<%= link_to "Log in", new_user_session_path, class: "btn btn-lg btn-primary", style: "margin: 2px" %>
<% end %>
</ul>
<%= form_for("#", html: { class: 'navbar-form navbar-left', role: 'search' }) do |f| %>
<%= f.text_field :search, class: 'form-group form-control', placeholder: 'Search' %>
<%= f.submit "Submit", class: "btn btn-default" %>
<% end %>
</div>
</div>
</nav>
......@@ -4,15 +4,14 @@
<%= link_to "Admin - products", admin_products_path %>
</li>
<li>
<%= link_to "Admin - Categories" %>
<%= link_to "Admin - Users", admin_users_path %>
</li>
<li><%= link_to "Admin - Carts", admin_carts_path %></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Admin", "" %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
Admin <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Edit my user", edit_admin_registration_path %></li>
......
......@@ -14,6 +14,18 @@
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% end %>
<% if !admin_signed_in? %>
<%= form_tag search_path, method: :get do %>
<div class="row">
<div class="col-md-10">
<%= text_field_tag :keyword, nil, class: 'form-control', placeholder: 'Search' %>
</div>
<div class="col-md-2">
<%= submit_tag "Search", :class => "btn btn-primary" %>
</div>
</div>
<% end %>
<% end %>
<% if notice %>
<p class="alert alert-success"><%= notice %></p>
<% end %>
......
......@@ -6,13 +6,19 @@
<%= image_tag(@product.image, alt: @product.name, class: "img-responsive") %><br>
<hr>
<div class="caption-full">
<h3 class="pull-right">
<b>Price: $</b><%= (@product.price/100.00) %>
<%= button_to "Add to cart", cart_products_path(product_id: @product.id), :class => "btn btn-primary" %>
</h3>
<br/>
<h4><b><%= @product.name %></b></h4>
<%= simple_format(@product.description) %>
<p><%= simple_format(@product.description) %></p>
<h3>
<b>Price: $</b><%= (@product.price/100.00) %>
<%= form_tag cart_products_path do %>
<p>
<%= hidden_field_tag :product_id, @product.id %>
Quantity: <%= number_field_tag :quantity, "1", class: 'form-control', :style => "width: 100px;" %><br/>
<%= submit_tag "Add To cart", :class => "btn btn-primary", :style => "width: 100px" %>
</p>
<% end %>
</h3>
</div>
</div>
</div>
......
<% provide(:title, "Search Products") %>
<h2 class="text-left">Search Products with keyword: <%= params[:keyword] %></h2>
<div class="col-md-12" style="text-align: center;">
<% if @products!= [] %>
<% @products.each do |product| %>
<div class="col-sm-4 col-lg-4 col-md-4">
<div class="thumbnail">
<%= image_tag(product.image, alt: product.name, style: 'height: 300px') %>
<div class="caption">
<div class="div_product_name" style="width: 235px; height: 53px;">
<h3 title="<%= product.name %>"><%= truncate(product.name, length: 25) %></h3>
</div>
<p><b>Price: </b><%= number_to_currency(product.price/100.000) %></p>
<%= form_tag cart_products_path do %>
<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>
<% end %>
<%= link_to "More Info", product, :class => "btn btn-default" %>
</div>
</div>
</div>
<% end %>
<% else %>
<h1> Not found</h1>
<% end %>
</div>
<%= will_paginate @products %>
\ No newline at end of file
<% provide(:title, "Shopping History") %>
<h2>Shopping History: <%= @user.username %>-<%= @user.email %></h2>
<table class="table table-hover">
<thead>
<tr>
<th>Product name</th>
<th>Price</th>
<th>Quantity</th>
<th>Total price</th>
</tr>
</thead>
<% @carts_to_user.each do |cart_to_user| %>
<% cart_products = cart_to_user.cart_products %>
<tr>
<td colspan="4"><h4><b>Cart: <%= cart_to_user.created_at %></b></h4></td>
</tr>
<% cart_products.each do |cart_product| %>
<tbody>
<% product = Product.find(cart_product.product_id) %>
<tr>
<td><%= product.name %></td>
<td><%= number_to_currency(product.price/100.00) %></td>
<td><%= cart_product.number %></td>
<td><%= number_to_currency(cart_product.price/100.00 * cart_product.number) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total:</td>
<td><b><%= number_to_currency(cart_to_user.total_price/100.00) %></b></td>
</tr>
<tr>
<td>Name: </td>
<td colspan="3"> <%= cart_to_user.full_name %>
</tr>
<tr>
<td>E-Mail: </td>
<td colspan="3"> <%= cart_to_user.email %>
</tr>
<tr>
<td>Phone: </td>
<td colspan="3"> <%= cart_to_user.phone %>
</tr>
<tr>
<td>Address: </td>
<td colspan="3"> <%= cart_to_user.address %>
</tr>
<tr>
<td>Status: </td>
<td colspan="3"><%= cart_to_user.status %></td>
</tr>
<% end %>
<tr>
<td><%= link_to 'Back', :back, class: "btn btn-info" %></td>
</tr>
</tbody>
</table>
<% provide(:title, "Home") %>
<br/>
<header class="jumbotron hero-spacer">
<h1>Home - AMAZON PRODUCT API</h1>
<h1>Home - AZIGExN VeNtura</h1>
<p>
Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API Home - AMAZON PRODUCT API
Home - AMAZON PRODUCT API Home - AZIGExN VeNtura - ABring Innovation from Vietnam to the World!
</p>
<p><%= link_to "Sign up now!", new_user_registration_path, class: "btn btn-lg btn-primary" %></p>
<p><%= link_to "Admin", "#", class: "btn btn-lg btn-primary" %></p>
</header>
......@@ -3,6 +3,7 @@ Rails.application.routes.draw do
devise_for :users
get 'carts/index'
get 'search' => 'search#search'
root to: "static_pages#home"
......@@ -13,9 +14,10 @@ Rails.application.routes.draw do
resources :products
resources :carts
resources :cart_products, only: [:create, :destroy]
resources :shopping_history, only: [:index]
namespace :admin do
resources :products, :carts
resources :products, :carts, :users
end
# The priority is based upon order of creation: first created -> highest priority.
......
......@@ -5,7 +5,7 @@ class CreateCarts < ActiveRecord::Migration
t.decimal :total_price, :default => 0
t.string :status
t.string :full_name
t.integer :phone
t.string :phone, limit: 15
t.string :email
t.text :address
......
class AddPhoneToUsers < ActiveRecord::Migration
def change
add_column :users, :phone, :string
add_column :users, :phone, :string, limit: 15
end
end
......@@ -47,7 +47,7 @@ ActiveRecord::Schema.define(version: 20150730023057) do
t.decimal "total_price", precision: 10, default: 0
t.string "status", limit: 255
t.string "full_name", limit: 255
t.integer "phone", limit: 4
t.string "phone", limit: 15
t.string "email", limit: 255
t.text "address", limit: 65535
t.datetime "created_at", null: false
......@@ -88,7 +88,7 @@ ActiveRecord::Schema.define(version: 20150730023057) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username", limit: 255
t.string "phone", limit: 255
t.string "phone", limit: 15
t.string "address", limit: 255
end
......
require 'test_helper'
class SearchControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
require 'test_helper'
class ShoppingHistoryControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
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