Commit 2cbd592f by Dao Minh Nhut

fixing import, create index page

parent b6d73d1a
# 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/
...@@ -66,7 +66,7 @@ h2 { ...@@ -66,7 +66,7 @@ h2 {
margin-bottom: 30px; margin-bottom: 30px;
text-align: center; text-align: center;
font-weight: normal; font-weight: normal;
color: $grayLight; color: $fff;
} }
p { p {
......
// Place all the styles related to the Product controller here. // Place all the styles related to the home controller here.
// They will automatically be included in application.css. // They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/ // You can use Sass (SCSS) here: http://sass-lang.com/
// Place all the styles related to the products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery protect_from_forgery
include SessionsHelper #include SessionsHelper
# Force signout to prevent CSRF attacks # Force signout to prevent CSRF attacks
def handle_unverified_request def handle_unverified_request
......
class HomeController < ApplicationController
def index
@products = Product.all
end
end
class ProductController < ApplicationController class ProductsController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy] #before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy #before_filter :correct_user, only: :destroy
def show #def show
@item = Product.find(params[:id]) #@item = Product.find(params[:id])
end #end
def index def index
@items = Product.all.select do |i| @item = Product.find(:all)
i.category==params[:id] #@items = Product.all.select do |i|
end # i.category==params[:id]
end #end
def new end
@item= Product.new def new
render layout: "another_layout" @item= Product.new
end render layout: "another_layout"
def create end
@item = current_user.items.new(params[:item]) def create
if @item.save @item = current_user.items.new(params[:item])
flash[:success] = "Your item has successfully posted!" if @item.save
redirect_to current_user, layout: "another_layout" flash[:success] = "Your item has successfully posted!"
else redirect_to current_user, layout: "another_layout"
render 'new', layout: "another_layout" else
end render 'new', layout: "another_layout"
end end
def destroy end
@item.destroy def destroy
redirect_to current_user @item.destroy
end redirect_to current_user
end
private private
def correct_user def correct_user
@item = current_user.items.find_by_id(params[:id]) @item = current_user.items.find_by_id(params[:id])
redirect_to root_url if @item.nil? redirect_to root_url if @item.nil?
end end
end end
module ProductsHelper
end
class Category < ActiveRecord::Base class Category < ActiveRecord::Base
has_many :products has_many :products
attr_accessible :name
end end
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
attr_accessible :description, :image, :name, :price # attr_accessible :description, :image, :name, :price
belongs_to :user # belongs_to :user
has_and_belongs_to_many :carts # has_and_belongs_to_many :carts
belongs_to :category belongs_to :category
default_scope order: 'items.created_at DESC' # default_scope order: 'items.created_at DESC'
end end
<ul class="products">
<% @products.each do |product| %>
<li>
<%= image_tag product.image %>
<%= product.name %>
</li>
<% end %>
</ul>
\ No newline at end of file
<header class="navbar navbar-fixed-top navbar-inverse"> <header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner"> <div class="navbar-inner">
<div class="container"> <div class="container">
<%= link_to "VenShop", root_path, id: "logo" %> <!--%= link_to "VenShop", root_path, id: "logo" %-->
<nav> <nav>
<ul class="nav pull-right"> <ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li> <li><%= link_to "Home", root_path %></li>
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
<ul class="nav nav-list"> <ul class="nav nav-list">
<li class="nav-header">Category</li> <li class="nav-header">Category</li>
<% category_list.each do |cat| %> <!--% category_list.each do |cat| %-->
<li><a href='/categories/<%= cat.id%>'><%=cat.name%></a></li> <li><a href='/categories/<%= cat.id%>'><%=cat.name%></a></li>
<%end%> <%end%>
</ul> </ul>
......
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title><%= full_title(yield(:title)) %></title> <title><!--%= full_title(yield(:title)) %--></title>
<%= stylesheet_link_tag "application", media: "all" %> <!--%= stylesheet_link_tag "application", media: "all" %-->
<%= javascript_include_tag "application" %> <!--%= javascript_include_tag "application" %-->
<%= csrf_meta_tags %> <!--%= csrf_meta_tags %-->
<%= render 'layouts/shim' %> <!--%= render 'layouts/shim' %-->
</head> </head>
<body> <body>
<%= render 'layouts/header' %> <!--%= render 'layouts/header' %-->
<div class="container"> <div class="container">
<% flash.each do |key, value| %> <% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div> <div class="alert alert-<%= key %>"><%= value %></div>
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<div class="center hero-unit"> <div class="center hero-unit">
<h1 style="color:blue">Welcome to VenShop</h1> <h1 style="color:blue">Welcome to VenShop</h1>
<% if !signed_in? %> <% if !signed_in? %>
<%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %> <!--%= link_to "Sign up now!", signup_path, class: "btn btn-large btn-primary" %-->
<% end %> <% end %>
</div> </div>
<%= render 'layouts/search' %> <%= render 'layouts/search' %>
...@@ -29,7 +29,7 @@ ...@@ -29,7 +29,7 @@
<% end %> <% end %>
<div style= "clear: both"></div> <div style= "clear: both"></div>
<div class="row-fluid"> <div class="row-fluid">
<%= render 'layouts/sidebar' %> <!--%= render 'layouts/sidebar' %-->
<%= yield %> <%= yield %>
</div> </div>
</div> </div>
......
<div class="span8 center">
<h3>Name: </h3><%= @item.name%><br><br>
<img src= <%= @item.image%> atl= "Image not found" width="250" height="250">
<!-- <p><a class="btn" href='/items/<%= @item.id %>'>Add to cart »</a></p> -->
<br><br>
<!-- <button onclick="addToCart()" class="btn btn-primary">Add to cart!</button> -->
<%= button_to "Add to cart!", '/user/add_to_cart/'+ @item.id.to_s,
:class => "btn btn-primary", :method => :get %>
<h3>Price: </h3><%= @item.price%><br>
<h3>Rate: </h3><%= @item.rate%><br>
<h3>Description: </h3><br>
<p><%= @item.description%></p>
</div><!--/span-->
\ No newline at end of file
...@@ -5,36 +5,24 @@ ...@@ -5,36 +5,24 @@
<div class="headline-newest">Add new product</div> <div class="headline-newest">Add new product</div>
<div class="list-Browse-NewsRv"> <div class="list-Browse-NewsRv">
<div class="span6 offset3" style="margin-left:0px">
<%= form_for(@product) do |f| %>
<div class="span6 offset3" style="margin-left:0px"> <%= f.label :Title %>
<%= form_for(@product) do |f| %> <%= f.text_field :title %>
<%= f.label :Price %>
<%= f.text_field :price %>
<%= f.label :Title %> <%= f.label :Image %>
<%= f.text_field :title %> <%= f.text_field :img %>
<%= f.label :Price %>
<%= f.text_field :price %>
<%= f.label :Stock %>
<%= f.text_field :date %>
<%= f.label :Desc %>
<%= f.text_field :desc %>
<%= f.label :Image %>
<%= f.text_field :img %>
<%= f.label :Category %>
<%= f.collection_select(:categoryid, Category.all, :id, :name) %>
<%= f.submit "Add new", class: "btn btn-large btn-primary" %>
<% end %>
</div>
<%= f.label :Category %>
<%= f.collection_select(:categoryid, Category.all, :id, :name) %>
<%= f.submit "Add new", class: "btn btn-large btn-primary" %>
<% end %>
</div>
</div> </div>
</div> </div>
......
<div class="span8 center">
<h3>Name: </h3><%= @item.name%><br><br>
</div><!--/span-->
\ No newline at end of file
...@@ -22,5 +22,6 @@ module VenShop ...@@ -22,5 +22,6 @@ module VenShop
# Do not swallow errors in after_commit/after_rollback callbacks. # Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true config.active_record.raise_in_transactional_callbacks = true
config.autoload_paths << Rails.root.join("lib")
end end
end end
VenShop::Application.routes.draw do VenShop::Application.routes.draw do
get 'categories/new'
get 'product/new'
devise_for :users devise_for :users
root to: 'static_pages#home' #root to: 'home#index'
get 'home/index'
get 'categories/new'
get 'products/show'
get '/help', to: 'static_pages#help' get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about' get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact' get '/contact', to: 'static_pages#contact'
......
...@@ -15,7 +15,7 @@ ActiveRecord::Schema.define(version: 20150721071537) do ...@@ -15,7 +15,7 @@ ActiveRecord::Schema.define(version: 20150721071537) do
create_table "categories", force: :cascade do |t| create_table "categories", force: :cascade do |t|
t.string "name", limit: 255 t.string "name", limit: 255
t.string "description", limit: 65535 t.string "description", limit: 255
t.string "image", limit: 255 t.string "image", limit: 255
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
...@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150721071537) do ...@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150721071537) do
t.string "name", limit: 255 t.string "name", limit: 255
t.string "image", limit: 255 t.string "image", limit: 255
t.integer "price", limit: 4 t.integer "price", limit: 4
t.string "description", limit: 65535 t.string "description", limit: 255
t.integer "category_id", limit: 4 t.integer "category_id", limit: 4
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
......
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"
)
@request.associate_tag = 'tag'
end
def import_product
(1..10).each do |page|
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"].to_h["URL"]
products.price = item["ItemAttributes"]["ListPrice"]["Amount"].to_i
if item["EditorialReviews"]["EditorialReview"]["Content"] == nil
products.description = item["EditorialReviews"]["EditorialReview"]["Content"]
else
products.description = "description"
end
products.category_id = category.id
end
rescue => e
p e.message
end
end
end
end
def get_response(page)
response = @request.item_search(
query: {
'Keywords' => '*',
'SearchIndex' => 'Blended',
'ResponseGroup' => 'Medium',
'ItemPage' => page,
}
)
response.to_h["ItemSearchResponse"].to_h["Items"].to_h["Item"]
end
end
\ No newline at end of file
class Amazon
attr_reader :request, :item_pages
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"
)
@item_pages = (1..10).to_a
end
public
def import_product(item)
begin
category = Category.find_or_create_by(name: item["ItemAttributes"]["ProductGroup"])
Product.find_or_create_by(name: item["ItemAttributes"]["Title"]) do |product|
product.image = item["LargeImage"]["URL"]
product.price = item["ItemAttributes"]["ListPrice"]["Amount"].to_i
product.description = item["EditorialReviews"]["EditorialReview"]["Content"]
product.category = category
end
rescue Exception => ex
puts ex
end
end
def import_products
item_pages.each do |page|
reponse = request.item_search(
query: {
"Keywords" => "*",
"SearchIndex" => "Blended",
"ResponseGroup" => "Medium",
"ItemPage" => page
}
)
items = reponse.to_h["ItemSearchResponse"]["Items"]["Item"]
items.each do |item|
import_product(item)
end
end
end
end
\ No newline at end of file
namespace :products do namespace :products do
desc 'Get products' desc 'Get products'
task import: :environment do task import: :environment do
Amazon.new.import_product ImportAmazon.new.import_product
end end
end end
require 'test_helper' require 'test_helper'
class ProductControllerTest < ActionController::TestCase class HomeControllerTest < ActionController::TestCase
test "should get new" do test "should get index" do
get :new get :index
assert_response :success assert_response :success
end end
......
require 'test_helper'
class ProductsControllerTest < 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