Second Commit
This commit is contained in:
58
app/controllers/admin/books_controller.rb
Normal file
58
app/controllers/admin/books_controller.rb
Normal file
@@ -0,0 +1,58 @@
|
||||
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
|
||||
16
app/controllers/application_controller.rb
Normal file
16
app/controllers/application_controller.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
def after_sign_in_path_for(resource)
|
||||
if resource.admin?
|
||||
stored_location_for(resource) || admin_books_path
|
||||
else
|
||||
stored_location_for(resource) || dashboard_index_path
|
||||
end
|
||||
end
|
||||
|
||||
def authorized_user!
|
||||
unless current_user && current_user.admin?
|
||||
flash[:alert] = 'You are not authorized to access this page.'
|
||||
redirect_to root_path
|
||||
end
|
||||
end
|
||||
end
|
||||
5
app/controllers/books_controller.rb
Normal file
5
app/controllers/books_controller.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class BooksController < ApplicationController
|
||||
def index
|
||||
@books = Book.all.page params[:page]
|
||||
end
|
||||
end
|
||||
19
app/controllers/check_outs_controller.rb
Normal file
19
app/controllers/check_outs_controller.rb
Normal file
@@ -0,0 +1,19 @@
|
||||
class CheckOutsController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
|
||||
def borrow
|
||||
@book = Book.find(params[:id])
|
||||
current_user.books << @book
|
||||
|
||||
respond_to do |format|
|
||||
format.js
|
||||
end
|
||||
end
|
||||
|
||||
def returns
|
||||
@book = current_user.books.find(params[:id])
|
||||
user_book = UsersBook.find_by(book_id: @book.id, user_id: current_user.id)
|
||||
user_book.update(status: :returned, returned_at: Time.zone.now)
|
||||
redirect_to dashboard_index_path
|
||||
end
|
||||
end
|
||||
0
app/controllers/concerns/.keep
Normal file
0
app/controllers/concerns/.keep
Normal file
7
app/controllers/dashboard_controller.rb
Normal file
7
app/controllers/dashboard_controller.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
class DashboardController < ApplicationController
|
||||
before_action :authenticate_user!
|
||||
|
||||
def index
|
||||
@books = current_user.books.page params[:page]
|
||||
end
|
||||
end
|
||||
5
app/controllers/home_controller.rb
Normal file
5
app/controllers/home_controller.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class HomeController < ApplicationController
|
||||
def index
|
||||
@books = Book.last(9)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user