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.
105 lines
2.7 KiB
105 lines
2.7 KiB
class LibrariesController < ApplicationController
|
|
layout 'base_library'
|
|
|
|
before_filter :require_login
|
|
|
|
def index
|
|
libraries = Library.where(nil)
|
|
|
|
libraries =
|
|
if params[:type] == 'mine'
|
|
libraries.where(user_id: current_user.id).order('created_at desc')
|
|
else
|
|
libraries.where(status: :published).order('visited_count desc')
|
|
end
|
|
|
|
search = params[:search].to_s.strip
|
|
libraries = libraries.where('title LIKE ?', "%#{search}%") if search.present?
|
|
|
|
per_page = params[:per_page].to_i <= 0 ? 20 : params[:per_page].to_i
|
|
@libraries = paginateHelper libraries.includes(user: :user_extensions), per_page
|
|
end
|
|
|
|
def show
|
|
@library = Library.find(params[:id])
|
|
return redirect_to libraries_path unless admin_or_self?
|
|
|
|
@library_applies = @library.library_applies.where(status: :refused).order('created_at desc')
|
|
@library.increment_visited_count!
|
|
end
|
|
|
|
def new
|
|
@library = current_user.libraries.new
|
|
end
|
|
|
|
def create
|
|
@library = current_user.libraries.new
|
|
Libraries::SaveService.new(@library, current_user, form_params).call
|
|
if with_publish?
|
|
Libraries::SubmitService.new(current_library).call
|
|
redirect_to publish_success_libraries_path
|
|
else
|
|
flash[:message] = '保存成功'
|
|
render 'new'
|
|
end
|
|
rescue ActiveRecord::RecordInvalid => _
|
|
render 'new'
|
|
rescue Libraries::SubmitService::Error => ex
|
|
flash[:message] = ex.message
|
|
render 'new'
|
|
end
|
|
|
|
def edit
|
|
@library = current_library
|
|
redirect_to library_path(id: @library.id) unless @library.editable?
|
|
end
|
|
|
|
def update
|
|
@library = current_library
|
|
Libraries::SaveService.new(@library, current_user, form_params).call
|
|
if with_publish?
|
|
Libraries::SubmitService.new(current_library).call
|
|
redirect_to publish_success_libraries_path
|
|
else
|
|
flash[:message] = '保存成功'
|
|
render 'edit'
|
|
end
|
|
rescue ActiveRecord::RecordInvalid => _
|
|
render 'edit'
|
|
rescue Libraries::SubmitService::Error => ex
|
|
flash[:message] = ex.message
|
|
render 'edit'
|
|
end
|
|
|
|
def publish
|
|
Libraries::SubmitService.new(current_library).call
|
|
render json: { status: 0 }
|
|
rescue Libraries::SubmitService::Error => ex
|
|
render json: { status: 0, message: ex.message }
|
|
end
|
|
|
|
def publish_success
|
|
end
|
|
|
|
private
|
|
|
|
def current_library
|
|
@_current_library ||= current_user.libraries.find(params[:id])
|
|
end
|
|
|
|
def form_params
|
|
@_form_params ||= begin
|
|
hash = params[:library].presence || {}
|
|
hash[:attachment_ids] = (params[:attachments].presence || []).values.map{|h| h[:attachment_id]}
|
|
hash
|
|
end
|
|
end
|
|
|
|
def with_publish?
|
|
params[:apply_publish].to_s == 'true'
|
|
end
|
|
|
|
def admin_or_self?
|
|
@library.user_id == current_user.id || current_user.admin?
|
|
end
|
|
end |