59 lines
1.2 KiB
Ruby
59 lines
1.2 KiB
Ruby
class Admin::BooksController < ApplicationController
|
|
before_action :authenticate_user!
|
|
before_action :authorized_user!
|
|
|
|
def index
|
|
@books = Book.all.order(created_at: :desc).page params[:page]
|
|
end
|
|
|
|
def new
|
|
@book = Book.new
|
|
end
|
|
|
|
def create
|
|
@book = Book.new(book_params)
|
|
if @book.save
|
|
flash[:notice] = 'Book created successfully.'
|
|
redirect_to admin_books_path
|
|
else
|
|
flash[:alert] = @book.errors.full_messages.join(', ')
|
|
render :new
|
|
end
|
|
end
|
|
|
|
def edit
|
|
@book = Book.find(params[:id])
|
|
end
|
|
|
|
def update
|
|
@book = Book.find(params[:id])
|
|
if @book.update(book_params)
|
|
flash[:notice] = 'Book updated successfully.'
|
|
redirect_to admin_books_path
|
|
else
|
|
flash[:alert] = @book.errors.full_messages.join(', ')
|
|
render :edit
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@book = Book.find(params[:id])
|
|
if @book.destroy
|
|
flash[:notice] = 'Book removed successfully.'
|
|
else
|
|
flash[:alert] = @book.errors.full_messages.join(', ')
|
|
end
|
|
redirect_to admin_books_path
|
|
end
|
|
|
|
def show
|
|
@book = Book.find(params[:id])
|
|
end
|
|
|
|
private
|
|
|
|
def book_params
|
|
params.require(:book).permit(:title, :author, :genre, :sub_genre, :pages, :copies)
|
|
end
|
|
end
|