|
|
|
@ -3,27 +3,29 @@ class Ecs::CreateCourseManagerService < ApplicationService
|
|
|
|
|
|
|
|
|
|
COURSE_MANAGER_COUNT_LIMIT = 2 # 课程管理员数量限制
|
|
|
|
|
|
|
|
|
|
attr_reader :ec_course, :user_id
|
|
|
|
|
attr_reader :ec_course, :user_ids
|
|
|
|
|
|
|
|
|
|
def initialize(ec_course, user_id)
|
|
|
|
|
def initialize(ec_course, user_ids)
|
|
|
|
|
@ec_course = ec_course
|
|
|
|
|
@user_id = user_id
|
|
|
|
|
@user_ids = user_ids
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def call
|
|
|
|
|
user = User.find_by(id: params[:user_id])
|
|
|
|
|
raise Error, '该用户不存在' if user.blank?
|
|
|
|
|
users_count = User.where(id: user_ids).count
|
|
|
|
|
raise Error, '用户不存在' if users_count != user_ids.size
|
|
|
|
|
|
|
|
|
|
if ec_course.ec_course_users.exists?(user_id: user.id)
|
|
|
|
|
raise Error, '该用户已经是该课程的管理员了'
|
|
|
|
|
if ec_course.ec_course_users.exists?(user_id: user_ids)
|
|
|
|
|
raise Error, '用户已经是该课程的管理员'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if ec_course.ec_course_users.count >= COURSE_MANAGER_COUNT_LIMIT
|
|
|
|
|
raise Error, '该课程管理员数量已达上限'
|
|
|
|
|
if ec_course.ec_course_users.count + user_ids.size >= COURSE_MANAGER_COUNT_LIMIT
|
|
|
|
|
raise Error, "课程管理员数量过多(最多#{COURSE_MANAGER_COUNT_LIMIT})"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
ec_course.ec_course_users.create!(user: user)
|
|
|
|
|
|
|
|
|
|
user
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
|
user_ids.each do |user_id|
|
|
|
|
|
ec_course.ec_course_users.create!(user_id: user_id)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|