bookstore/app/controllers/books_controller.rb

44 lines
851 B
Ruby
Raw Normal View History

2021-03-19 17:31:38 +02:00
class BooksController < ApplicationController
2021-03-21 20:30:25 +01:00
before_action :set_book, only: [:show, :edit, :update]
before_action :ensure_admin, only: [:edit, :update]
2021-03-19 17:31:38 +02:00
def index
2021-03-21 20:30:25 +01:00
if current_user.admin?
books = Book.all
else
books = Book.published
end
@books = books.map { |book| BooksPresenter.new(book) }
end
def show
end
def edit
end
def update
if @book.update(book_params)
redirect_to '/books'
end
end
private
def ensure_admin
unless current_user&.admin?
redirect_to '/welcome', notice: 'You are not allowed to perform this action'
end
end
def set_book
@book = BooksPresenter.new(Book.find(params[:id]))
end
def book_params
result = params.require(:book).permit(:title, :price, :published)
result['price'] = result['price'].to_d * 100
result
2021-03-19 17:31:38 +02:00
end
end