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 {
margin-bottom: 30px;
text-align: center;
font-weight: normal;
color: $grayLight;
color: $fff;
}
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.
// 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
protect_from_forgery
include SessionsHelper
#include SessionsHelper
# Force signout to prevent CSRF attacks
def handle_unverified_request
......
class HomeController < ApplicationController
def index
@products = Product.all
end
end
class ProductController < ApplicationController
before_filter :signed_in_user, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
class ProductsController < ApplicationController
#before_filter :signed_in_user, only: [:create, :destroy]
#before_filter :correct_user, only: :destroy
def show
@item = Product.find(params[:id])
end
#def show
#@item = Product.find(params[:id])
#end
def index
@items = Product.all.select do |i|
i.category==params[:id]
end
@item = Product.find(:all)
#@items = Product.all.select do |i|
# i.category==params[:id]
#end
end
def new
@item= Product.new
......
module ProductsHelper
end
class Category < ActiveRecord::Base
has_many :products
attr_accessible :name
end
class Product < ActiveRecord::Base
attr_accessible :description, :image, :name, :price
belongs_to :user
has_and_belongs_to_many :carts
# attr_accessible :description, :image, :name, :price
# belongs_to :user
# has_and_belongs_to_many :carts
belongs_to :category
default_scope order: 'items.created_at DESC'
# default_scope order: 'items.created_at DESC'
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">
<div class="navbar-inner">
<div class="container">
<%= link_to "VenShop", root_path, id: "logo" %>
<!--%= link_to "VenShop", root_path, id: "logo" %-->
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
......
......@@ -4,7 +4,7 @@
<ul class="nav nav-list">
<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>
<%end%>
</ul>
......
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
<title><!--%= full_title(yield(:title)) %--></title>
<!--%= stylesheet_link_tag "application", media: "all" %-->
<!--%= javascript_include_tag "application" %-->
<!--%= csrf_meta_tags %-->
<!--%= render 'layouts/shim' %-->
</head>
<body>
<%= render 'layouts/header' %>
<!--%= render 'layouts/header' %-->
<div class="container">
<% flash.each do |key, value| %>
<div class="alert alert-<%= key %>"><%= value %></div>
......@@ -17,7 +17,7 @@
<div class="center hero-unit">
<h1 style="color:blue">Welcome to VenShop</h1>
<% 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 %>
</div>
<%= render 'layouts/search' %>
......@@ -29,7 +29,7 @@
<% end %>
<div style= "clear: both"></div>
<div class="row-fluid">
<%= render 'layouts/sidebar' %>
<!--%= render 'layouts/sidebar' %-->
<%= yield %>
</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,37 +5,25 @@
<div class="headline-newest">Add new product</div>
<div class="list-Browse-NewsRv">
<div class="span6 offset3" style="margin-left:0px">
<%= form_for(@product) do |f| %>
<%= f.label :Title %>
<%= f.text_field :title %>
<%= 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>
</div>
</div>
</div></div>
\ No newline at end of file
<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
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.autoload_paths << Rails.root.join("lib")
end
end
VenShop::Application.routes.draw do
get 'categories/new'
get 'product/new'
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 '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
......
......@@ -15,7 +15,7 @@ ActiveRecord::Schema.define(version: 20150721071537) do
create_table "categories", force: :cascade do |t|
t.string "name", limit: 255
t.string "description", limit: 65535
t.string "description", limit: 255
t.string "image", limit: 255
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
......@@ -25,7 +25,7 @@ ActiveRecord::Schema.define(version: 20150721071537) do
t.string "name", limit: 255
t.string "image", limit: 255
t.integer "price", limit: 4
t.string "description", limit: 65535
t.string "description", limit: 255
t.integer "category_id", limit: 4
t.datetime "created_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
desc 'Get products'
task import: :environment do
Amazon.new.import_product
ImportAmazon.new.import_product
end
end
require 'test_helper'
class ProductControllerTest < ActionController::TestCase
test "should get new" do
get :new
class HomeControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
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