You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/libraries_controller.rb

87 lines
2.3 KiB

class LibrariesController < ApplicationController
include PaginateHelper
before_action :require_login, :check_auth, except: %i[index show]
helper_method :current_library, :library_manageable?
def index
libraries = Library.all
libraries =
if User.current&.logged? && params[:type] == 'mine'
libraries.where(user_id: current_user.id).order(created_at: :desc)
else
libraries.where(status: :published).order(visited_count: :desc)
end
keyword = params[:keyword].to_s.strip
if keyword.present?
libraries = libraries.where('title LIKE :keyword OR author_name LIKE :keyword OR author_school_name LIKE :keyword',
keyword: "%#{keyword}%")
end
@count = libraries.count
@libraries = paginate libraries.includes(:library_tags, :praise_tread_cache, user: :user_extension)
ids = @libraries.map(&:id)
@download_count_map = Attachment.where(container_type: 'Library', container_id: ids)
.group(:container_id).sum(:downloads)
end
def show
unless current_library.published? || library_manageable?(current_library)
return render_forbidden
end
end
def create
library = current_user.libraries.new
Libraries::SaveService.call(library, current_user, save_params)
render_ok
rescue Libraries::SaveService::Error => ex
render_error(ex.message)
end
def update
return render_forbidden unless library_manageable?(current_library)
Libraries::SaveService.call(current_library, current_user, save_params)
render_ok
rescue Libraries::SaveService::Error => ex
render_error(ex.message)
end
def destroy
if admin_or_business?
current_library.destroy!
elsif current_library.user_id == current_user&.id
unless current_library.pending?
render_error('只有草稿才能删除')
return
end
current_library.destroy!
else
render_forbidden
return
end
render_ok
end
private
def current_library
@_current_library ||= Library.find(params[:id])
end
def library_manageable?(library)
current_user&.id == library.user_id || admin_or_business?
end
def save_params
params.permit(:title, :content, :author_name, :author_school_name,
:cover_id, :publish, attachment_ids: [], tag_ids: [])
end
end