mirror of
https://gitlink.org.cn/Gitlink/forgeplus.git
synced 2026-05-03 03:40:49 +08:00
init project
This commit is contained in:
32
app/services/libraries/agree_apply_service.rb
Normal file
32
app/services/libraries/agree_apply_service.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
class Libraries::AgreeApplyService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :library_apply, :library, :user
|
||||
|
||||
def initialize(library_apply, user)
|
||||
@library_apply = library_apply
|
||||
@library = library_apply.library
|
||||
@user = user
|
||||
end
|
||||
|
||||
def call
|
||||
raise Error, '该状态下不能进行此操作' unless library_apply.may_agree?
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
library_apply.agree!
|
||||
library_apply.library.publish!
|
||||
|
||||
# 将消息改为已处理
|
||||
Tiding.where(container_id: library.id, container_type: 'Library', tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||
notify_library_author!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def notify_library_author!
|
||||
Tiding.create!(user_id: library.user_id, trigger_user_id: 0,
|
||||
container_id: library.id, container_type: 'Library',
|
||||
tiding_type: 'System', status: 1)
|
||||
end
|
||||
end
|
||||
39
app/services/libraries/refuse_apply_service.rb
Normal file
39
app/services/libraries/refuse_apply_service.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
class Libraries::RefuseApplyService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :library_apply, :library, :user, :params
|
||||
|
||||
def initialize(library_apply, user, params)
|
||||
@library_apply = library_apply
|
||||
@library = library_apply.library
|
||||
@user = user
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
reason = params[:reason].to_s.strip
|
||||
raise Error, '原因不能为空' if reason.blank?
|
||||
raise Error, '该状态下不能进行此操作' unless library_apply.may_refuse?
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
library_apply.reason = reason
|
||||
library_apply.refused_at = Time.current
|
||||
library_apply.refuse
|
||||
library_apply.save!
|
||||
|
||||
library.refuse!
|
||||
|
||||
# 将消息改为已处理
|
||||
Tiding.where(container_id: library.id, container_type: 'Library', tiding_type: 'Apply', status: 0).update_all(status: 1)
|
||||
notify_library_author!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def notify_library_author!
|
||||
Tiding.create!(user_id: library.user_id, trigger_user_id: 0,
|
||||
container_id: library.id, container_type: 'Library',
|
||||
tiding_type: 'System', status: 2, extra: library_apply.reason)
|
||||
end
|
||||
end
|
||||
69
app/services/libraries/save_service.rb
Normal file
69
app/services/libraries/save_service.rb
Normal file
@@ -0,0 +1,69 @@
|
||||
class Libraries::SaveService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :library, :user, :params
|
||||
|
||||
def initialize(library, user, params)
|
||||
@library = library
|
||||
@user = user
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
Libraries::SaveForm.new(params).validate!
|
||||
|
||||
if library.new_record?
|
||||
library.user_id = user.id
|
||||
library.generate_uuid
|
||||
end
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
library.assign_attributes(library_params)
|
||||
library.save!
|
||||
|
||||
deal_library_tag!
|
||||
deal_attachments!
|
||||
|
||||
Libraries::SubmitService.call(library) if with_publish?
|
||||
end
|
||||
|
||||
library
|
||||
rescue Libraries::SubmitService::Error => ex
|
||||
raise Error, ex.message
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def deal_library_tag!
|
||||
new_tag_ids = LibraryTag.where(id: Array.wrap(params[:tag_ids]).compact).pluck(:id)
|
||||
old_tag_ids = library.library_library_tags.pluck(:library_tag_id)
|
||||
|
||||
# 删除标签
|
||||
destroy_ids = old_tag_ids - new_tag_ids
|
||||
library.library_library_tags.where(library_tag_id: destroy_ids).delete_all
|
||||
|
||||
# 创建标签
|
||||
created_ids = new_tag_ids - old_tag_ids
|
||||
created_ids.each do |id|
|
||||
library.library_library_tags.create!(library_tag_id: id)
|
||||
end
|
||||
end
|
||||
|
||||
def deal_attachments!
|
||||
attachment_ids = Array.wrap(params[:attachment_ids]).compact.map(&:to_i)
|
||||
old_attachment_id = library.attachments.pluck(:id)
|
||||
|
||||
destroy_ids = old_attachment_id - attachment_ids
|
||||
library.attachments.where(id: destroy_ids).delete_all
|
||||
|
||||
Attachment.where(id: attachment_ids, author_id: user.id).update_all(container_type: library.class.to_s, container_id: library.id)
|
||||
end
|
||||
|
||||
def library_params
|
||||
params.slice(*%i[title content author_name author_school_name cover_id])
|
||||
end
|
||||
|
||||
def with_publish?
|
||||
params[:publish].to_s == 'true'
|
||||
end
|
||||
end
|
||||
32
app/services/libraries/submit_service.rb
Normal file
32
app/services/libraries/submit_service.rb
Normal file
@@ -0,0 +1,32 @@
|
||||
class Libraries::SubmitService < ApplicationService
|
||||
Error = Class.new(StandardError)
|
||||
|
||||
attr_reader :library
|
||||
|
||||
def initialize(library)
|
||||
@library = library
|
||||
end
|
||||
|
||||
def call
|
||||
return if library.processing? || library.published?
|
||||
|
||||
raise Error, '该状态下不能提交审核' unless library.may_submit?
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
library.published_at = Time.current
|
||||
library.submit
|
||||
library.save!
|
||||
|
||||
library.library_applies.create!
|
||||
send_library_apply_notify!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def send_library_apply_notify!
|
||||
Tiding.create!(user_id: 1, trigger_user_id: library.user_id,
|
||||
container_id: library.id, container_type: 'Library',
|
||||
tiding_type: 'Apply', status: 0)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user