shopping cart

This commit is contained in:
Karol Selak 2021-03-22 02:25:17 +01:00
parent 733870ce96
commit df7585c5e1
11 changed files with 79 additions and 11 deletions

View file

@ -1,5 +1,5 @@
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update]
before_action :set_book, only: [:show, :edit, :update, :add_to_cart]
before_action :ensure_admin, only: [:edit, :update]
def index
@ -23,6 +23,17 @@ class BooksController < ApplicationController
end
end
def add_to_cart
@book = Book.find(params[:id])
current_user.books << @book
@book.decrement!(:quantity)
redirect_to '/books', notice: 'Book added to your cart'
end
def shopping_cart
@books = current_user.books.map { |book| BooksPresenter.new(book) }
end
private
def set_book