books edition by admin
This commit is contained in:
parent
5095f63fd7
commit
e36cc36947
10 changed files with 119 additions and 20 deletions
5
app/controllers/authors_controller.rb
Normal file
5
app/controllers/authors_controller.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AuthorsController < ApplicationController
|
||||
def index
|
||||
@authors = Author.all
|
||||
end
|
||||
end
|
|
@ -1,5 +1,43 @@
|
|||
class BooksController < ApplicationController
|
||||
before_action :set_book, only: [:show, :edit, :update]
|
||||
before_action :ensure_admin, only: [:edit, :update]
|
||||
|
||||
def index
|
||||
@books = Book.published.map { |book| BooksPresenter.new(book) }
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue