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.
pgfqe6ch8/app/controllers/libraries_controller.rb

146 lines
4.2 KiB

class LibrariesController < ApplicationController
5 years ago
layout 'base_library'
5 years ago
before_filter :require_login, :except => [:index, :show]
5 years ago
after_filter :increment_visit_count, only: [:show, :create, :edit, :update]
def index
libraries = Library.where(nil)
libraries =
5 years ago
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
search = params[:search].to_s.strip
5 years ago
if search.present?
libraries = libraries.where('title LIKE :search OR author_name LIKE :search OR author_school_name LIKE :search',
search: "%#{search}%")
end
5 years ago
per_page = params[:per_page].to_i <= 0 ? 20 : params[:per_page].to_i
5 years ago
@libraries = paginateHelper libraries.includes(:library_tags, :praise_tread_cache, user: :user_extensions), per_page
@download_count_map = Attachment.where(container_type: 'Library', container_id: @libraries.map(&:id)).group(:container_id).count
end
def show
@library = current_library
5 years ago
return render_403 unless admin_or_self? || @library.published?
5 years ago
@library_applies = @library.library_applies.where(status: :refused).order('created_at desc')
end
def new
@library = current_user.libraries.new
end
def create
@library = current_user.libraries.new
5 years ago
Libraries::SaveService.new(@library, current_user, form_params).call
if with_publish?
Libraries::SubmitService.new(@library).call
5 years ago
redirect_to publish_success_libraries_path(id: @library.id)
5 years ago
else
5 years ago
redirect_to library_path(id: @library.id)
5 years ago
end
5 years ago
rescue ActiveRecord::RecordInvalid => e
flash[:message] = e.record.errors.full_messages.join(',')
render 'new'
5 years ago
rescue Libraries::SubmitService::Error, Libraries::SaveService::Error => ex
5 years ago
flash[:message] = ex.message
render 'new'
end
def edit
return render_403 unless admin_or_self?
@library = current_library
end
def update
return render_403 unless admin_or_self?
@library = current_library
5 years ago
Libraries::SaveService.new(@library, current_user, form_params).call
if with_publish?
Libraries::SubmitService.new(@library).call
5 years ago
redirect_to publish_success_libraries_path(id: @library.id)
5 years ago
else
5 years ago
redirect_to library_path(id: @library.id)
5 years ago
end
5 years ago
rescue ActiveRecord::RecordInvalid => e
flash[:message] = e.record.errors.full_messages.join(',')
render 'edit'
5 years ago
rescue Libraries::SubmitService::Error, Libraries::SaveService::Error => ex
5 years ago
flash[:message] = ex.message
render 'edit'
end
def destroy
if admin_or_business?
current_library.destroy
elsif current_library.user_id == current_user.id
unless current_library.pending?
render json: { status: -1, message: '只有草稿才能删除' }
return
end
current_library.destroy
else
render_403
return
end
render json: { status: 0, message: 'success' }
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
5 years ago
def publish_success
end
private
def current_library
@_current_library ||= Library.find(params[:id])
end
5 years ago
def form_params
@_form_params ||= begin
hash = params[:library].presence || {}
5 years ago
hash[:tag_ids] = params[:tag_ids].to_s.split(',')
5 years ago
hash[:attachment_ids] = (params[:attachments].presence || []).values.map{|h| h[:attachment_id]}
5 years ago
hash[:cover_id] = save_cover.try(:id) || hash[:cover_id]
5 years ago
hash
end
end
def with_publish?
params[:apply_publish].to_s == 'true'
end
def admin_or_self?
5 years ago
current_library.try(:user_id) == current_user.try(:id) || admin_or_business?
5 years ago
end
5 years ago
def increment_visit_count
@library.increment_visited_count! if @library && @library.id
end
5 years ago
def save_cover
return if params[:cover_file].blank?
attachment = Attachment.new(file: params[:cover_file])
attachment.author = User.current
attachment.filename = Redmine::Utils.random_hex(16)
attachment.save
attachment
end
end