Second Commit

This commit is contained in:
2021-04-15 13:06:31 +05:30
parent 7f1d1f54e7
commit 129cd9cbcc
169 changed files with 11137 additions and 0 deletions

View 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

View 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

View File

@@ -0,0 +1,5 @@
class BooksController < ApplicationController
def index
@books = Book.all.page params[:page]
end
end

View 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

View File

View File

@@ -0,0 +1,7 @@
class DashboardController < ApplicationController
before_action :authenticate_user!
def index
@books = current_user.books.page params[:page]
end
end

View File

@@ -0,0 +1,5 @@
class HomeController < ApplicationController
def index
@books = Book.last(9)
end
end