Commit d38e9224 by Dao Minh Nhut

solr search, insert, update, delete

parent 9452905f
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# Use mysql as the database for Active Record
......@@ -22,25 +20,29 @@ gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'will_paginate'
# Use Unicorn as the app server
# gem 'unicorn'
gem 'unicorn'
gem 'bootstrap-will_paginate'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'bcrypt-ruby'
gem 'rsolr'
gem 'faker'
gem 'solr-ruby'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
......
......@@ -87,6 +87,7 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
kgio (2.9.3)
loofah (2.0.2)
nokogiri (>= 1.5.9)
mail (2.6.3)
......@@ -127,10 +128,13 @@ GEM
activesupport (= 4.2.3)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
raindrops (0.15.0)
rake (10.4.2)
rdoc (4.2.0)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rsolr (1.0.12)
builder (>= 2.1.2)
sass (3.4.16)
sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0)
......@@ -141,6 +145,7 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
solr-ruby (0.0.8)
spring (1.3.6)
sprockets (3.2.0)
rack (~> 1.0)
......@@ -158,6 +163,10 @@ GEM
uglifier (2.7.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
unicorn (4.9.0)
kgio (~> 2.6)
rack
raindrops (~> 0.7)
vacuum (1.3.0)
jeff (~> 1.0)
multi_xml (~> 0.5.0)
......@@ -185,11 +194,17 @@ DEPENDENCIES
jquery-rails
mysql2
rails (= 4.2.3)
rsolr
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
solr-ruby
spring
turbolinks
uglifier (>= 1.3.0)
unicorn
vacuum
web-console (~> 2.0)
will_paginate
BUNDLED WITH
1.10.5
# 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/
......@@ -6,7 +6,6 @@ class Admins::CartsController < ApplicationController
if admin_signed_in? && !params[:cart][:status].nil? && !params[:cart][:status].empty?
@cart = Cart.find(params[:id])
if @cart.status == params[:cart][:status]
flash[:danger] = "Notthing change!"
redirect_to :back
else
@cart.status = params[:cart][:status]
......@@ -15,7 +14,6 @@ class Admins::CartsController < ApplicationController
redirect_to :back
end
else
flash[:danger] = "Notthing change!"
redirect_to action: :index
end
end
......
class Admins::ProductsController < ApplicationController
before_action :init, only: [ :create, :edit, :destroy ]
def init
@solr = RSolr.connect :url => Rails.configuration.solr_host.to_s
end
def insert
@product = Product.new
@categories = Category.all
......@@ -15,6 +22,12 @@ class Admins::ProductsController < ApplicationController
price: params[:product][:price].to_i, image: params[:product][:image],
description: params[:product][:description])
if @product.save
item = [{:id => @product.id, :price => @product.price, :name => @product.name,
:category_id => @product.category_id, :price => @product.price,
:image => @product.image, :description => @product.description}]
@solr.add item
@solr.commit
@solr.optimize
flash[:success] = "Create product : Success"
redirect_to admins_product_path(id: "1")
else
......@@ -28,25 +41,33 @@ class Admins::ProductsController < ApplicationController
@product = Product.find(params[:id])
@product.update(product_params)
if @product.save
flash[:success] = "Update product : Success"
redirect_to admins_product_path(id: "1")
%x{curl 'localhost:8080/solr/core0/update?commit=true' -H 'Content-type:application/json' -d '
[{
"id":"#{params[:id]}",
"name":{"set":"#{params[:product][:name]}"}
}]'}
flash[:success] = "Update product : Success"
redirect_to admins_product_path(id: "1")
else
flash[:danger] = "Error: Price"
render :update
flash[:danger] = "Error"
render :update
end
end
def delete
def destroy
@product = Product.all
@product = Product.find(params[:id])
@product.destroy
redirect_to :back
@product.destroy
@solr.delete_by_query "id:#{params[:id]}"
@solr.commit
@solr.optimize
redirect_to :back
end
def show
@products = Product.paginate(page: params[:page])
end
def cart
@products = Product.paginate(page: params[:page])
end
def product_params
......
class Admin::UsersController < ApplicationController
class Admins::UsersController < ApplicationController
before_action :authenticate_admin!
def show
@users = User.all
@users = User.paginate(page: params[:page]).per_page(25)
end
end
\ No newline at end of file
def delete
@user = User.find(params[:id])
@user.destroy
redirect_to :back
end
end
require 'solr'
class ApplicationController < ActionController::Base
protect_from_forgery
before_action :sign_out_all, if: :devise_controller?
before_action :call_category , only: [:show, :index]
before_action :configure_permitted_parameters, if: :devise_controller?
# Prevent CSRF attacks by raising an exception.
......@@ -7,7 +10,15 @@ class ApplicationController < ActionController::Base
include CartsHelper
include CategoriesHelper
def sign_out_all
sign_out current_user if current_user
sign_out current_admin if current_admin
end
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
#devise_parameter_sanitizer.for(:sign_up) << :username
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:fullname, :email, :password, :password_confirmation, :phone, :address) }
#devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
#devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end
end
......@@ -6,23 +6,26 @@ class CartProductsController < ApplicationController
if params[:id].nil? || params[:quantity].nil?
flash[:danger] = "Product not found!"
redirect_to :action => :index
end
if params[:quantity].to_i > 0
id = params[:id]
if session[:cart] then
else
if params[:quantity].to_i > 0
id = params[:id]
if session[:cart] then
cart_product = session[:cart]
else
session[:cart] ={}
cart_product = session[:cart]
else
session[:cart] ={}
cart_product = session[:cart]
end
end
if cart_product[id] then
cart_product[id] = cart_product[id] + params[:quantity].to_i
if cart_product[id] then
cart_product[id] = cart_product[id] + params[:quantity].to_i
else
cart_product[id] = params[:quantity].to_i
end
flash[:success] = "success"
redirect_to :action => :index
else
cart_product[id] = params[:quantity].to_i
redirect_to product_path
end
flash[:success] = "success"
redirect_to :action => :index
end
end
......@@ -35,9 +38,11 @@ class CartProductsController < ApplicationController
if params[:new_quantity].nil? || params[:id].nil?
flash[:danger] = "Product not found!"
else
session[:cart][params[:id].to_s] = params[:new_quantity].to_i
if params[:quantity].to_i > 0
session[:cart][params[:id].to_s] = params[:new_quantity].to_i
end
end
redirect_to :action => :index
redirect_to :action => :index
end
def clear
session[:cart] = nil
......
......@@ -7,6 +7,7 @@ class CartsController < ApplicationController
end
def info
@new_cart = Cart.new
if user_signed_in?
@current_user
end
......@@ -14,17 +15,13 @@ class CartsController < ApplicationController
def create
if !session[:cart].nil?
new_cart = Cart.new(cart_params)
new_cart.total_price = calculate_total_price
new_cart.status = "new cart"
if user_signed_in?
new_cart.user_id = current_user.id
end
new_cart.save
if !new_cart.id.nil?
@new_cart = Cart.new(fullname: params[:cart][:fullname], email: params[:cart][:email],
address: params[:cart][:address], phone: params[:cart][:phone],
total_price: calculate_total_price, status: "new cart", user_id: cart_user_id)
if @new_cart.save
session[:cart].each do |id, quantity|
cart_product = CartProduct.new
cart_product.cart_id = new_cart.id
cart_product.cart_id = @new_cart.id
cart_product.product_id = id
cart_product.price = Product.find_by_id(id).price
cart_product.quantity = quantity
......@@ -33,14 +30,16 @@ class CartsController < ApplicationController
flash[:success] = "Success!"
Emailer.send_email_to(cart_params[:email].to_s,session[:cart]).deliver
session[:cart] = nil
redirect_to cart_path
else
render :info
end
end
flash[:danger] = "Wrong input please input again!"
render :info
end
def cart_params
params.require(:session).permit(:fullname, :email, :address, :phone)
params.require(:cart).permit(:fullname, :email, :address, :phone)
end
private
......@@ -49,12 +48,20 @@ class CartsController < ApplicationController
total = 0
if !session[:cart].nil?
session[:cart].each do |id, quantity|
product = Product.find_by_id(id)
if !product.nil?
total = total + product.price * quantity
product = Product.find_by_id(id)
if !product.nil?
total = total + product.price * quantity
end
end
end
end
total
end
def cart_user_id
user_id = ""
if user_signed_in?
user_id = current_user.id
end
user_id
end
end
class CategoriesController < ApplicationController
include CategoriesHelper
def show
category = Category.find(params[:id])
@products = category.products
begin
category = Category.find(params[:id])
@products = category.products
rescue
redirect_to root_path
end
end
def index
redirect_to root_path
end
end
\ No newline at end of file
......@@ -2,7 +2,11 @@ class ProductsController < ApplicationController
include CategoriesHelper
include CartProductsHelper
def index
@products = Product.paginate(page: params[:page])
begin
@products = Product.paginate(page: params[:page])
rescue
@products = Product.paginate(page: "1")
end
end
def show
begin
......
class SearchController < ApplicationController
#before_action :check_page, only: [:search]
def show
@solr = Solr::Connection.new(Rails.configuration.solr_host.to_s, :autocommit => :on)
check_params(params["search_params"])
query = "((name:#{@keyword}))"
select_obj = Solr::Request::Select.new(nil, {'q' => query})
begin
results = @solr.send(select_obj).data['response']['docs']
@products = Array.new
results.each do |result|
id = result.to_h['id']
product = Product.find(id)
@products.insert(-1,product)
end
rescue Exception => e
redirect_to root_path
end
end
def check_params(search_params)
check = ["`", "~", "!", "#", "$", "%", "^", "&", "*", "(", ")", "-", "+", ":",'\\']
@keyword = ""
search_params.each_char do |key|
check.each do |ch|
key = "\\#{ch}" if key == ch
end
@keyword += key
end
end
end
module ProductsHelper
def call_product
@products = Product.all
end
end
module SearchHelper
end
......@@ -2,11 +2,10 @@ class Cart < ActiveRecord::Base
has_many :cart_product
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 :email, presence: true, length: { maximum: 100 }, 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 :total_price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 99999999 }
validates :fullname, presence: true, length: { maximum: 50 }
validates :address, presence: true, length: { maximum: 1000 }
end
class Category < ActiveRecord::Base
has_many :products
def new
@category = Category.new
end
end
class Product < ActiveRecord::Base
belongs_to :category
has_many :cart_products
VALID_NUMBER_REGEX = /\A[+-]?\d+\Z/
validates :category_id, presence: true
validates :name, :image, presence: true, length: { maximum: 1000 }
validates :description, length: { maximum: 65535 }
validates :price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 9999 }
validates :price, :numericality => { :greater_than_or_equal_to => 0, :less_than_or_equal_to => 99999999 }
end
......@@ -25,7 +25,7 @@
<td><%= f.text_field :status, value: cart.status.to_s, size: 10 , class: "create_input" %>
</td>
<td>
<%= f.submit "Submit" , class: "btn btn-primary" %>
<%= f.submit "Update" %>
</td>
<% end %>
</tr>
......
<h2>New products </h2>
<% if @product.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@product.errors.count, "error") %>.
<% if admin_signed_in? %>
<h2>New products </h2>
<% if @product.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@product.errors.count, "error") %>.
</div>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form_for(@product, url: {action: "create"}) do |f| %>
<% end %>
<div>
<%= form_for(@product, url: {action: "create"}) do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true %>
<%= f.label :category_id %>
<%= f.collection_select :category_id, @categories, :id, :name %>
<%= f.label :category_id %>
<%= f.collection_select :category_id, @categories, :id, :name %>
<%= f.label :price %>
<%= f.number_field :price, min: "1", max: "100" %>
<%= f.label :price %>
<%= f.number_field :price, min: "1", max: "100" %>
<%= f.label :image %>
<%= f.text_field :image %>
<%= f.label :image %>
<%= f.text_field :image %>
<%= f.label :description %>
<%= f.text_area :description, rows: '15' %>
<%= f.label :description %>
<%= f.text_area :description, rows: '15' %>
<%= f.submit "New product", class: "btn btn-primary" %>
<%= f.submit "New product", class: "btn btn-primary" %>
<% end %>
</div>
<% end %>
</div>
<% end %>
\ No newline at end of file
<% if admin_signed_in? %>
<div class="span12">
<h2>Products</h2>
<%= link_to "Insert Product", insert_product_path %>
<div class="span12">
<h2>Products</h2>
<table class="table table-striped table-hover">
<thead>
<tr>
<td></td>
<td></td>
<td></td>
<th><h4><%= link_to "Insert", insert_product_path %></h4></th>
</tr>
<tr>
<th>Product Name</th>
<th>Image</th>
<th>Edit</th>
<th>Delete</th>
</tr>
......@@ -14,12 +20,15 @@
<% @products.each do |product| %>
<tr>
<td><%= product.name %></td>
<td><%= image_tag product.image, height: '60', width: '60' %></td>
<td><%= link_to "Edit", "/update_product/#{product.id}" %></td>
<td><%= link_to "Delete", "/delete_product/#{product.id}" %></td>
<td><%= link_to "Delete", "/delete_product/#{product.id}", data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @products%>
<div align="center">
<%= will_paginate @products%>
</div>
</div>
<% end %>
<h2>Edit products</h2>
<% if @product.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@product.errors.count, "error") %>.
<% if admin_signed_in? %>
<h2>Edit products</h2>
<% if @product.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@product.errors.count, "error") %>.
</div>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<ul>
<% @product.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form_for(@product, url: {action: "edit"}) do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %>
<%= f.label :category_id %>
<%= f.collection_select :category_id, @categories, :id, :name, :selected => @product.category_id %>
<%= f.label :price %>
<%= f.number_field :price, class: 'form-control' %>
<%= f.label :image %>
<%= f.text_field :image, class: 'form-control' %>
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control', rows: '15' %>
<%= f.submit "Edit", class: "btn btn-primary" %>
<% end %>
</div>
<% end %>
<div>
<%= form_for(@product, url: {action: "edit"}) do |f| %>
<%= f.label :name %><br />
<%= f.text_field :name, autofocus: true, class: 'form-control' %>
<%= f.label :category_id %>
<%= f.collection_select :category_id, @categories, :id, :name, :selected => @product.category_id %>
<%= f.label :price %>
<%= f.number_field :price, class: 'form-control' %>
<%= f.label :image %>
<%= f.text_field :image, class: 'form-control' %>
<%= f.label :description %>
<%= f.text_area :description, class: 'form-control', rows: '15' %>
<%= f.submit "Edit", class: "btn btn-primary" %>
<% end %>
</div>
<% end %>
\ No newline at end of file
<h2>Log in</h2>
<h2>Log ind</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
......
<% if admin_signed_in? %>
<div class="row">
<h1>All users</h1>
<table class="table">
<thead>
<% 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>Full Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<tr>
<th>Id</th>
<th>Email</th>
<td><%= user.id %></td>
<td><%= user.fullname %></td>
<td><%= user.email %></td>
<td><%= user.phone %></td>
<td><%= user.address %> </td>
<td><%= link_to "Delete", "/delete_user/#{user.id}", data: { confirm: 'Are you sure?' } %></td>
</tr>
</thead>
<tbody>
<% @users.each do |user|%>
<tr>
<td><%= user.id.to_s %></td>
<td><%= user.email.to_s %></td>
</tr>
<% end -%>
</tbody>
</table>
</div>
<% end %>
\ No newline at end of file
<% end %>
</tbody>
</table>
</div>
<%= will_paginate @users %>
......@@ -2,55 +2,59 @@
<div class="span9">
<h2>Shopping Cart</h2>
<div>
<ul>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Update</th>
<th>Delete</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<% total = 0 %>
<% if !@cart_product.nil? %>
<% @cart_product.each do |id, quantity| %>
<% product = Product.find_by_id(id) %>
<% if !product.nil? %>
<% total = total + product.price * quantity.to_i %>
<tr>
<td><%= link_to truncate(product.name, length:20), "/products/#{product.id}" %></td>
<form action="/cart_product/update" >
<td><input name="new_quantity" min="1" max="100" type="number" class="span1" value= <%= quantity %> /></td>
<td><input type="submit" value="Update" /><input type="hidden" name="id" value="<%= product.id %>"/></td>
</form>
<td><form action="/cart_product/remove" ><input type="submit" value="Delete" /><input type="hidden" name="id" value="<%= product.id %>"/></form></td>
<td><%= (product.price/100.to_f).to_s + "$" %></td>
<td><%= ((product.price * quantity)/100.to_f).to_s + "$" %></td>
</tr>
<% if !session[:cart].nil? && !session[:cart].empty? %>
<div>
<ul>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Update</th>
<th>Delete</th>
<th>Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<% total = 0 %>
<% if !@cart_product.nil? %>
<% @cart_product.each do |id, quantity| %>
<% product = Product.find_by_id(id) %>
<% if !product.nil? %>
<% total = total + product.price * quantity.to_i %>
<tr>
<td><%= link_to truncate(product.name, length:20), "/products/#{product.id}" %></td>
<form action="/cart_product/update" >
<td><input name="new_quantity" min="1" max="100" type="number" class="span1" value= <%= quantity %> /></td>
<td><input type="submit" value="Update" /><input type="hidden" name="id" value="<%= product.id %>"/></td>
</form>
<td><form action="/cart_product/remove" ><input type="submit" value="Delete" /><input type="hidden" name="id" value="<%= product.id %>"/></form></td>
<td><%= (product.price/100.to_f).to_s + "$" %></td>
<td><%= ((product.price * quantity)/100.to_f).to_s + "$" %></td>
</tr>
<% end %>
<% end %>
<% else %>
<h2>Shopping Cart Empty</h2>
<% end %>
<% else %>
<h2>Shopping Cart Empty</h2>
<% end %>
</tbody>
</table>
</ul>
</div>
</tbody>
</table>
</ul>
</div>
<dl class="dl-horizontal pull-right">
<dt>Sub-total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd>
<dt>Total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd>
</dl>
<div class="clearfix"></div>
<dl class="dl-horizontal pull-right">
<dt>Sub-total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd>
<dt>Total:</dt>
<dd><%=(total/100.to_f).to_s + "$" %></dd>
</dl>
<div class="clearfix"></div>
<%= link_to "Check Out", "/cart", class: "btn btn-success pull-right"%>&nbsp;&nbsp;
<%= link_to "Delete All" ,'/cart_product/clear', class: "btn btn-lg btn-danger pull-right" %>
<%else%>
<h1>Your Cart is Empty!</h1>
<% end %>
<%= link_to "Continue Shopping " , root_path, class: "btn btn-primary" %>
<%= link_to "Check Out", "/cart", class: "btn btn-success pull-right"%>&nbsp;&nbsp;
<%= link_to "Delete All" ,'/cart_product/clear', class: "btn btn-lg btn-danger pull-right" %>
<!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%-->
</div>
\ No newline at end of file
......@@ -32,25 +32,36 @@
<dd><%= number_to_currency(@total/100.to_f, :unit => '$')%></dd>
</dl></div><br><br><br>
<div align="center">
<% flash.each do |message_type, message| %>
<div class="alert alert-<%= message_type %>"><%= message %></div>
<% if @new_cart.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(@new_cart.errors.count, "error") %>.
</div>
<ul>
<% @new_cart.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form_for(@new_cart, url: create_cart_path) do |f| %>
<%= f.label :email %>
<% if user_signed_in? %>
<% email = @current_user.email %>
<% end %>
<%= form_for(:session, url: create_cart_path) do |f| %>
<%= f.label :email %>
<%= f.email_field :email, value: email, class: 'form-control'%>
<%= f.label :fullname %>
<%= f.text_field :fullname, class: 'form-control' %>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
</br>
<%= f.submit "Submit" , class: "btn btn-primary" %>
<% else %>
<%= f.email_field :email, class: 'form-control'%>
<% end %>
<%= f.label :fullname %>
<%= f.text_field :fullname, class: 'form-control' %>
<%= f.label :address %>
<%= f.text_field :address, class: 'form-control' %>
<%= f.label :phone %>
<%= f.text_field :phone, class: 'form-control' %>
</br>
<%= f.submit "Submit" , class: "btn btn-primary" %>
<% end %>
</div>
<%else%>
<h1>Your Cart is Empty. Thank you for order</h1>
......
<h1>show cart</h1>
<ul>
<% if !@show_cart.nil? %>
<% @show_cart.each do |cart| %>
<h4>
<%= cart.id.to_s + " | "%>
<%= cart.mail.to_s + " | " %>
<%= cart.name.to_s + " | " %>
<%= cart.total_price.to_s + " | " %>
<%= cart.status.to_s + " | " %>
<%= cart.address %>
</h4>
<% end -%>
<% else -%>
<h2> Your cart Empty </h2>
<% end -%>
<%= render 'categories/view' %>
</ul>
<div class="span9">
<h2>Order Cart</h2>
<div>
<ul>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<% if !@show_cart.nil? %>
<% @show_cart.each do |show_cart| %>
<% cart_product = CartProduct.find_by_id(show_cart.id) %>
<% product = Product.find_by_id(cart_product.product_id) %>
<tr>
<td><%= truncate(product.name, length:20) %></td>
<td><%= cart_product.quantity %></td>
<td><%= (cart_product.price/100.to_f).to_s + "$" %></td>
<td><%= (show_cart.total_price/100.to_f).to_s + "$" %></td>
<td><%= show_cart.status %></td>
</tr>
<% end %>
<% else %>
<h2>Your Order Empty</h2>
<% end %>
</tbody>
</table>
</ul>
</div>
<div class="clearfix"></div>
<%= link_to "Continue Shopping " , root_path, class: "btn btn-primary pull-right" %>
<!--%= link_to "info", "/carts", class: "btn btn-lg btn-success"%-->
</div>
\ No newline at end of file
<h2>Resend confirmation instructions</h2>
<%= form_for(resource, as: resource_name, url: confirmation_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true, value: (resource.pending_reconfirmation? ? resource.unconfirmed_email : resource.email) %>
</div>
<div class="actions">
<%= f.submit "Resend confirmation instructions" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<p>Welcome <%= @email %>!</p>
<p>You can confirm your account email through the link below:</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
<p>Hello <%= @resource.email %>!</p>
<p>Someone has requested a link to change your password. You can do this through the link below.</p>
<p><%= link_to 'Change my password', edit_password_url(@resource, reset_password_token: @token) %></p>
<p>If you didn't request this, please ignore this email.</p>
<p>Your password won't change until you access the link above and create a new one.</p>
<p>Hello <%= @resource.email %>!</p>
<p>Your account has been locked due to an excessive number of unsuccessful sign in attempts.</p>
<p>Click the link below to unlock your account:</p>
<p><%= link_to 'Unlock my account', unlock_url(@resource, unlock_token: @token) %></p>
<h2>Change your password</h2>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<div class="field">
<%= f.label :password, "New password" %><br />
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autofocus: true, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirm new password" %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Change my password" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<h2>Forgot your password?</h2>
<%= form_for(resource, as: resource_name, url: password_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Send me reset password instructions" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<% if admin_signed_in? %>
<% else %>
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div class="field">
<%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<%= link_to "Back", :back %>
<% end %>
\ No newline at end of file
<div class="span12">
<div align="center">
<% if admin_signed_in? %>
<h2>Admin Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<% else %>
<h2>User Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :fullname %><br />
<%= f.text_field :fullname, autofocus: true %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if @minimum_password_length %>
<em>(<%= @minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :phone %><br />
<%= f.text_field :phone, autofocus: true %>
</div>
<div class="field">
<%= f.label :address %><br />
<%= f.text_field :address, autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
<% end %>
</div>
</div>
\ No newline at end of file
<div class="span12">
<div align="center">
<h2>Log in</h2>
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<% end -%>
<div class="actions">
<%= f.submit "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
</div>
\ No newline at end of file
<%- if controller_name != 'sessions' %>
<%= link_to "Log in", new_session_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
<%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
<%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
<% end -%>
<%- if devise_mapping.omniauthable? %>
<%- resource_class.omniauth_providers.each do |provider| %>
<%= link_to "Sign in with #{provider.to_s.titleize}", omniauth_authorize_path(resource_name, provider) %><br />
<% end -%>
<% end -%>
<h2>Resend unlock instructions</h2>
<%= form_for(resource, as: resource_name, url: unlock_path(resource_name), html: { method: :post }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="actions">
<%= f.submit "Resend unlock instructions" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
......@@ -6,19 +6,19 @@
<div class="nav-collapse collapse">
<!--%= link_to "VenShop", root_path, id: "logo" %-->
<ul class="nav">
<li><%= link_to "Home", root_path %></li>
<!-- li><%= link_to "Home", root_path %></li -->
<li><%= link_to "About", about_path %></li>
<li><%= link_to "Contact", contact_path %></li>
</ul>
<!--form class="navbar-form form-search pull-right">
<input id="Search" name="Search" type="text" placeholder="type text to search for" class="input-medium search-query">
<form action= "/search/show" method="get" class="navbar-form form-search pull-right">
<input id="Search" name="search_params" type="text" placeholder="type text to search for" class="input-medium search-query">
<button type="submit" class="btn">Search</button>
</form-->
</form>
<ul class="nav pull-right">
<% if user_signed_in? %>
<!--li><%= link_to "Carts", "" %></li-->
<!--li><%= link_to "Profile", "" %></li-->
<li><%= link_to "Settings", edit_user_registration_path %></li>
<li><%= link_to "Cart", carts_path %></li>
<li><%= link_to "Order", carts_show_path %></li>
<li><%= link_to "Setting", edit_user_registration_path %></li>
<li><%= link_to "Log out", destroy_user_session_path, method: "delete" %></li>
<% elsif admin_signed_in? %>
<li><%= link_to "Products", admins_product_path(id: "1") %></li>
......
<%= render 'categories/view' %>
<div class="span9">
<div class="hero-unit">
<h1 class="">Ventura Trainee</h1>
<p class="">Ruby on Rails</p>
......
......@@ -19,7 +19,7 @@
<label>Quantity:</label>
<div>
<form action="/cart_products">
<input type="number" name="quantity" value= "1" min="1" max="100" value="1">
<input type="number" name="quantity" value= "1" min="1" max="100">
<input type="hidden" name="id" value="<%= @product.id %>"/>
<input type="submit" value="Add to cart" class = "btn btn-lg btn-success" />
</form>
......
<%= render 'categories/view' %>
<div class="span9">
<div class="hero-unit">
<h1 class="">Ventura Trainee</h1>
<p class="">Ruby on Rails</p>
<p><%= link_to "Learn more »", about_path, class: "btn btn-primary btn-large"%></p>
</div>
<ul class="thumbnails">
<% if !@products.empty? %>
<% @products.each do |product| %>
<li class="span3">
<div class="thumbnail">
<%= image_tag product.image %>
<div class="caption">
<div style="height: 40px">
<h4><%= truncate(product.name, :length => 30, :omission => '...') %></h4>
</div>
<p><%= (product.price/100.to_f).to_s + "$" %></p>
<%= link_to "View Info", "/products/#{product.id}", class: "btn btn-primary" %>
<form action="/cart_products" class="pull-right" >
<input type="hidden" name="quantity" value= "1">
<input type="hidden" name="id" value="<%= product.id %>"/>
<input type="submit" value="Add to Cart" class = "btn btn-success" />
</form>
<!--%= link_to "Add to Cart", "/cart_products/#{product.id}", class: "btn btn-success" %-->
</div>
</div>
</li>
<% end %>
<% else %>
<li>
<h1> Product not found</h1></li>
<% end %>
</ul>
</div>
\ No newline at end of file
VenShop::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
config.solr_host = "http://localhost:8080/solr/core0"
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
......
VenShop::Application.routes.draw do
namespace :admins do
get 'users/show'
end
namespace :admins do
get 'users/new'
end
devise_for :users
devise_for :admins
namespace :admins do
namespace :users do
resources :products
end
namespace :admins do
resources :products, :users
end
root 'products#index'
get 'show_product' => 'products#index'
get 'insert_product' => 'admins/products#insert'
get 'update_product/:id' => 'admins/products#update'
get 'delete_product/:id' => 'admins/products#delete'
get 'delete_product/:id' => 'admins/products#destroy', as: 'destroy_product'
get 'show_cart' => 'admins/carts#show'
get 'show_user' => 'admins/users#show'
patch '/admins/products/:id/edit'=>'admins/products#edit'
patch '/admins/carts/:id/edit' => 'admins/carts#edit'
get 'delete_user/:id' => 'admins/users#delete'
get 'search/show'
get 'cart_products' => 'cart_products#add'
get 'cart_product/remove' => 'cart_products#remove'
......
class CreateCarts < ActiveRecord::Migration
def change
create_table :carts do |t|
t.integer :User_id
t.integer :user_id
t.integer :total_price
t.string :status
t.string :fullname
t.integer :phone
t.string :phone
t.string :address
t.string :email
......
class ChangeLimit < ActiveRecord::Migration
def change
change_column :products, :price, :integer, :limit => 5
change_column :users, :phone, :string
change_column :cart_products, :cart_id, :integer, :limit => 5
change_column :cart_products, :product_id, :integer, :limit => 5
change_column :cart_products, :quantity, :integer, :limit => 5
change_column :cart_products, :price, :integer, :limit => 5
change_column :carts, :user_id, :integer, :limit => 5
change_column :carts, :total_price, :integer, :limit => 5
change_column :products, :name, :string, :limit => 1000
change_column :products, :category_id, :integer, :limit => 5
end
end
class AddFieldToUser < ActiveRecord::Migration
class AddFieldUser < ActiveRecord::Migration
def change
add_column :users, :fullname, :string
add_column :users, :phone, :integer
add_column :users, :address, :string
add_column :users, :fullname, :string
add_column :users, :phone, :string
add_column :users, :address, :string
end
end
class AddFieldAdmin < ActiveRecord::Migration
def change
add_column :admins, :fullname, :string
add_column :admins, :phone, :string
add_column :admins, :address, :string
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150730075349) do
ActiveRecord::Schema.define(version: 20150811014432) do
create_table "admins", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false
......@@ -26,26 +26,29 @@ ActiveRecord::Schema.define(version: 20150730075349) do
t.string "last_sign_in_ip", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "fullname", limit: 255
t.string "phone", limit: 255
t.string "address", limit: 255
end
add_index "admins", ["email"], name: "index_admins_on_email", unique: true, using: :btree
add_index "admins", ["reset_password_token"], name: "index_admins_on_reset_password_token", unique: true, using: :btree
create_table "cart_products", force: :cascade do |t|
t.integer "cart_id", limit: 4
t.integer "product_id", limit: 4
t.integer "quantity", limit: 4
t.integer "price", limit: 4
t.integer "cart_id", limit: 8
t.integer "product_id", limit: 8
t.integer "quantity", limit: 8
t.integer "price", limit: 8
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "carts", force: :cascade do |t|
t.integer "User_id", limit: 4
t.integer "total_price", limit: 4
t.integer "user_id", limit: 8
t.integer "total_price", limit: 8
t.string "status", limit: 255
t.string "fullname", limit: 255
t.integer "phone", limit: 4
t.string "phone", limit: 255
t.string "address", limit: 255
t.string "email", limit: 255
t.datetime "created_at", null: false
......@@ -59,11 +62,11 @@ ActiveRecord::Schema.define(version: 20150730075349) do
end
create_table "products", force: :cascade do |t|
t.string "name", limit: 255
t.string "name", limit: 1000
t.string "image", limit: 255
t.integer "price", limit: 4
t.integer "price", limit: 8
t.string "description", limit: 20000
t.integer "category_id", limit: 4
t.integer "category_id", limit: 8
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
......@@ -82,7 +85,7 @@ ActiveRecord::Schema.define(version: 20150730075349) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "fullname", limit: 255
t.integer "phone", limit: 4
t.string "phone", limit: 255
t.string "address", limit: 255
end
......
......@@ -3,17 +3,17 @@ class ImportAmazon
def initialize
@request = Vacuum.new('US')
@request.configure(
aws_access_key_id: "AKIAJ77C4CTZOP7TUVWQ",
aws_secret_access_key: "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",
associate_tag: "zigexn6400-22"
aws_access_key_id: 'AKIAIAJR65JO6EIPQWTA',
aws_secret_access_key: '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',
associate_tag: 'microv'
)
@request.associate_tag = 'tag'
@request.associate_tag = 'tag'
end
def import_product
def import_product
(1..100).each do |page|
get_response(page).each do |item|
begin
get_response(page).each do |item|
begin
category = Category.find_or_create_by(name: item["ItemAttributes"]["ProductGroup"])
products = Product.find_or_create_by(name: item["ItemAttributes"].to_h["Title"]) do |products|
products.image = item["LargeImage"]["URL"]
......@@ -41,7 +41,7 @@ class ImportAmazon
'ItemPage' => page,
}
)
response.to_h["ItemSearchResponse"].to_h["Items"].to_h["Item"]
response.to_h["ItemSearchResponse"].to_h["Items"].to_h["Item"]
end
end
\ No newline at end of file
require 'test_helper'
class Admins::UsersControllerTest < ActionController::TestCase
test "should get show" do
get :show
test "should get new" do
get :new
assert_response :success
end
......
require 'test_helper'
class SearchControllerTest < ActionController::TestCase
test "should get show" do
get :show
assert_response :success
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