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.
31 lines
895 B
31 lines
895 B
6 years ago
|
class Ecs::CreateMajorManagerService < ApplicationService
|
||
|
Error = Class.new(StandardError)
|
||
|
|
||
|
MAJOR_MANAGER_COUNT_LIMIT = 5 # 专业管理员数量限制
|
||
|
|
||
|
attr_reader :major_school, :user_id
|
||
|
|
||
|
def initialize(major_school, user_id)
|
||
|
@major_school = major_school
|
||
|
@user_id = user_id
|
||
|
end
|
||
|
|
||
|
def call
|
||
|
raise Error, '示例专业不能添加管理员' if major_school.template_major?
|
||
|
|
||
|
user = User.find_by(id: params[:user_id])
|
||
|
raise Error, '该用户不存在' if user.blank?
|
||
|
|
||
|
if major_school.ec_major_school_users.exists?(user_id: user.id)
|
||
|
raise Error, '该用户已经是该专业的管理员了'
|
||
|
end
|
||
|
|
||
|
if major_school.ec_major_school_users.count >= MAJOR_MANAGER_COUNT_LIMIT
|
||
|
raise Error, '该专业管理员数量已达上限'
|
||
|
end
|
||
|
|
||
|
major_school.ec_major_school_users.create!(user: user)
|
||
|
|
||
|
user
|
||
|
end
|
||
|
end
|