Skip to content

Commit

Permalink
Merge pull request #8 from KOH6/checkout
Browse files Browse the repository at this point in the history
チェックアウト機能実装
  • Loading branch information
KOH6 authored Sep 12, 2023
2 parents fa67790 + 813384a commit a208bd8
Show file tree
Hide file tree
Showing 43 changed files with 577 additions and 215 deletions.
15 changes: 15 additions & 0 deletions app/controllers/admin/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Admin
class ApplicationController < ApplicationController
before_action :basic_auth

private

def basic_auth
authenticate_or_request_with_http_basic do |username, password|
username == ENV['BASIC_AUTH_USER'] && password == ENV['BASIC_AUTH_PASSWORD']
end
end
end
end
15 changes: 15 additions & 0 deletions app/controllers/admin/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Admin
class OrdersController < Admin::ApplicationController
def index
@orders = Order.all.order(created_at: :desc)
end

def show
@order = Order.find(params[:id])
@ordered_cart_products = @order.cart.cart_products.order(created_at: :desc)
@total = @ordered_cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal }
end
end
end
5 changes: 2 additions & 3 deletions app/controllers/admin/products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
# frozen_string_literal: true

module Admin
class ProductsController < ApplicationController
before_action :basic_auth
class ProductsController < Admin::ApplicationController
before_action :set_product, only: %i[show update destroy]

def index
@products = Product.kept.order(updated_at: :desc)
@products = Product.kept.order(created_at: :desc)
end

def show; end
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ def set_session_cart_id
def set_cart_size
@cart_size = Cart.find(session[:cart_id]).cart_products.size
end

def set_cart
@cart = Cart.find(session[:cart_id])
end
end
14 changes: 4 additions & 10 deletions app/controllers/cart_products_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# frozen_string_literal: true

class CartProductsController < ApplicationController
before_action :set_cart_id
before_action :set_cart

def index
@cart_products = Cart.find(@cart_id).cart_products.order(created_at: :desc)
@cart_products = Cart.find(@cart.id).cart_products.order(created_at: :desc)
@total = @cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal }
end

def create
product_id = params[:product_id]
quantity = params[:quantity].to_i
cart_product = CartProduct.find_by(product_id:, cart_id: @cart_id)
cart_product = CartProduct.find_by(product_id:, cart_id: @cart.id)

if cart_product
cart_product.increment(:quantity, quantity)
cart_product.save
else
CartProduct.create(product_id:, cart_id: @cart_id, quantity:)
CartProduct.create(product_id:, cart_id: @cart.id, quantity:)
end
redirect_to request.referer
end
Expand All @@ -26,10 +26,4 @@ def destroy
CartProduct.find(params[:id]).destroy
redirect_to request.referer
end

private

def set_cart_id
@cart_id = session[:cart_id]
end
end
42 changes: 42 additions & 0 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

class OrdersController < ApplicationController
before_action :set_cart
before_action :set_cart_products, only: %i[index create]

def index
@order = Order.new
end

def create
@order = Order.new(order_params)
@order[:cart_id] = @cart.id

error_messages = @cart.set_validate_error_messages
if error_messages.size.positive?
flash.now['danger'] = error_messages.join('<br/>')
render :index
return
end

if @order.save
session[:cart_id] = nil
flash[:success] = '購入ありがとうございます'
redirect_to products_path
else
render :index
end
end

private

def order_params
params.require(:order).permit(%i[last_name first_name user_name email country prefecture zip_code address1 address2
credit_name credit_number credit_expiration credit_cvv])
end

def set_cart_products
@cart_products = @cart.cart_products.order(created_at: :desc)
@total = @cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal }
end
end
2 changes: 1 addition & 1 deletion app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class ProductsController < ApplicationController
def index
@products = Product.kept.order(updated_at: :desc)
@products = Product.kept.order(created_at: :desc)
end

def show
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class ApplicationMailer < ActionMailer::Base
default from: '[email protected]'
default from: ENV['EMAIL']
layout 'mailer'
end
13 changes: 13 additions & 0 deletions app/mailers/order_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class OrderMailer < ApplicationMailer
helper ApplicationHelper

def complete(order:)
@order = order
recipient = @order.email
@ordered_cart_products = @order.cart.cart_products.order(created_at: :desc)
@total = @ordered_cart_products.inject(0) { |total, cart_product| total + cart_product.subtotal }
mail(to: recipient, subject: '注文内容ご確認')
end
end
21 changes: 21 additions & 0 deletions app/models/cart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,25 @@
class Cart < ApplicationRecord
has_many :cart_products, dependent: :destroy
has_many :products, through: :cart_products

def set_validate_error_messages
messages = []
messages << 'カートに商品が入っていません。' if cart_products.empty?

cart_products
.select { |cart_product| cart_product.quantity > cart_product.product.stock }
.map do |cart_product|
product = cart_product.product
messages << "#{product.name}が注文可能数を超えています。最大注文可能数:#{product.stock}個"
end

messages
end

def decrease_product_stock
cart_products.each do |cart_product|
quantity = cart_product.quantity
Product.update(cart_product.product.id, stock: cart_product.product.stock - quantity)
end
end
end
26 changes: 26 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class Order < ApplicationRecord
belongs_to :cart

with_options presence: true do
validates :last_name
validates :first_name
validates :user_name
validates :email
validates :country
validates :prefecture
validates :zip_code
validates :address1
validates :address2
validates :credit_name
validates :credit_number
validates :credit_expiration
validates :credit_cvv
end

before_create do
cart.decrease_product_stock
OrderMailer.complete(order: @order).deliver_later
end
end
40 changes: 40 additions & 0 deletions app/views/admin/orders/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<!-- Navigation-->
<%= render 'admin/shared/navigation' %>
<!-- Header-->
<header class="text-center py-5 my-5">
<h1 class="display-6 fw-bolder">購入明細一覧</h1>
</header>
<div class="container px-4 px-lg-5 mb-5">
<div class="table-responsive">
<table class="table table-striped ">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">お名前</th>
<th scope="col">メールアドレス</th>
<th scope="col">お届け先</th>
<th scope="col">購入日時</th>
</tr>
</thead>
<tbody>
<% @orders.each do |order| %>
<tr>
<th scope="row"><%= order.id %></th>
<td>
<%= link_to "#{order.last_name} #{order.first_name}様", admin_order_path(order), class: "link-dark" %>
</td>
<td><%= order.email %></td>
<td><%= "#{order.address1} #{order.address2}" %></td>
<td><%= l order.created_at %></td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
23 changes: 23 additions & 0 deletions app/views/admin/orders/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!-- Navigation-->
<%= render 'admin/shared/navigation' %>
<!-- Header-->
<header class="text-center py-5 my-5">
<h1 class="display-6 fw-bolder">購入明細詳細</h1>
</header>
<div class="container px-4 px-lg-5 mb-5">
<div class="row gx-4 gx-lg-5 align-items-center">
<h1 class="display-5 fw-bolder"><%= "#{@order.last_name} #{@order.first_name}様" %></h1>
<p class="lead">メールアドレス:<%= @order.email %></p>
<p class="lead">お届け先:<%= "#{@order.address1} #{@order.address2}" %></p>
<h3 class="fw-bolder">クレジットカード情報</h3>
<p class="lead">名義:<%= @order.credit_name %></p>
<p class="lead">有効期限:<%= @order.credit_expiration %></p>
</div>
<%= render 'shared/ordered_cart_products', :ordered_cart_products => @ordered_cart_products, :total => @total %>
</div>
<!-- Footer-->
<footer class="py-5 bg-dark">
<div class="container">
<p class="m-0 text-center text-white">Copyright &copy; KOH's Shop 2023</p>
</div>
</footer>
8 changes: 7 additions & 1 deletion app/views/admin/products/_product.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
<%= link_to product.name, admin_product_path(product), class: "link-dark" %>
</h5>
<!-- Product price-->
<%= convert_to_jpy(product.price) %>
<div>
<%= convert_to_jpy(product.price) %>
</div>
<!-- Product stock-->
<div>
在庫数<%= product.stock %>
</div>
</div>
</div>
<!-- Product actions-->
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/products/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- Navigation-->
<%= render 'navigation' %>
<%= render 'admin/shared/navigation' %>
<!-- ErrorMessages-->
<% if @updated_product %>
<%= render 'shared/error_messages', :resource => @updated_product %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/products/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- Navigation-->
<%= render 'navigation' %>
<%= render 'admin/shared/navigation' %>
<!-- Flash-->
<%= render 'shared/flash' %>
<!-- Header-->
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!-- Navigation-->
<%= render 'navigation' %>
<%= render 'admin/shared/navigation' %>
<!-- ErrorMessages-->
<%= render 'shared/error_messages', :resource => @product %>
<!-- Header-->
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Navigation-->
<%= render 'navigation' %>
<%= render 'admin/shared/navigation' %>
<!-- Display alert if product is discarded-->
<% if @product.discarded? %>
<p class="text-center alert alert-danger">削除済みの商品のため編集できません</p>
<p class="text-center alert alert-danger mt-5">削除済みの商品のため編集できません</p>
<% end %>
<!-- Product section-->
<section class="py-5 my-5">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="container px-4 px-lg-5">
<%= link_to "管理者用設定", admin_products_path, class: "navbar-brand fw-bolder" %>
<div class="justify-content-beetween">
<%= link_to "購入明細一覧", admin_products_path, class: "btn btn-primary mt-auto" %>
<%= link_to "購入明細一覧", admin_orders_path, class: "btn btn-primary mt-auto" %>
<%= link_to "新規作成", new_admin_product_path, class: "btn btn-primary mt-auto" %>
</div>
</div>
Expand Down
Loading

0 comments on commit a208bd8

Please sign in to comment.