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.
educoder/app/services/ecs/create_course_manager_servi...

31 lines
921 B

6 years ago
class Ecs::CreateCourseManagerService < ApplicationService
Error = Class.new(StandardError)
COURSE_MANAGER_COUNT_LIMIT = 2 # 课程管理员数量限制
attr_reader :ec_course, :user_ids
6 years ago
def initialize(ec_course, user_ids)
6 years ago
@ec_course = ec_course
@user_ids = user_ids
6 years ago
end
def call
users_count = User.where(id: user_ids).count
raise Error, '用户不存在' if users_count != user_ids.size
6 years ago
if ec_course.ec_course_users.exists?(user_id: user_ids)
raise Error, '用户已经是该课程的管理员'
6 years ago
end
5 years ago
if ec_course.ec_course_users.count + user_ids.size > COURSE_MANAGER_COUNT_LIMIT
raise Error, "课程管理员数量过多(最多#{COURSE_MANAGER_COUNT_LIMIT}"
6 years ago
end
ActiveRecord::Base.transaction do
user_ids.each do |user_id|
ec_course.ec_course_users.create!(user_id: user_id)
end
end
6 years ago
end
end