ecs: modify add course manager api

dev_hjm_a
p31729568 5 years ago
parent d35e54e948
commit 564b0bdd95

@ -3,7 +3,8 @@ class Ecs::CourseManagersController < Ecs::CourseBaseController
before_action :check_major_manager_permission!, only: [:create, :destroy]
def create
@user = Ecs::CreateCourseManagerService.call(current_course, params[:user_id])
Ecs::CreateCourseManagerService.call(current_course, params[:user_ids])
render_ok
rescue Ecs::CreateCourseManagerService::Error => ex
render_error(ex.message)
end

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

@ -1 +0,0 @@
json.partial! 'ecs/shared/user', user: @user
Loading…
Cancel
Save