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...

29 lines
768 B

class Ecs::CreateCourseManagerService < ApplicationService
Error = Class.new(StandardError)
COURSE_MANAGER_COUNT_LIMIT = 2 # 课程管理员数量限制
attr_reader :ec_course, :user_id
def initialize(ec_course, user_id)
@ec_course = ec_course
@user_id = user_id
end
def call
user = User.find_by(id: params[:user_id])
raise Error, '该用户不存在' if user.blank?
if ec_course.ec_course_users.exists?(user_id: user.id)
raise Error, '该用户已经是该课程的管理员了'
end
if ec_course.ec_course_users.count >= COURSE_MANAGER_COUNT_LIMIT
raise Error, '该课程管理员数量已达上限'
end
ec_course.ec_course_users.create!(user: user)
user
end
end