commit
d34b0ef91e
@ -1,8 +1,27 @@
|
||||
class Weapps::CoursesController < Weapps::BaseController
|
||||
before_action :require_wechat_login!
|
||||
# before_action :require_wechat_login!
|
||||
|
||||
def create
|
||||
return render_error("只有老师身份才能创建课堂") unless current_user.is_teacher?
|
||||
course = Course.new(tea_id: current_user.id)
|
||||
Weapps::CreateCourseService.call(course, course_params)
|
||||
render_ok
|
||||
|
||||
rescue ApplicationService::Error => ex
|
||||
render_error(ex.message)
|
||||
end
|
||||
|
||||
def edit
|
||||
|
||||
end
|
||||
|
||||
def update
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def course_params
|
||||
params.permit(:name, :course_list_name, :credit, course_module_types: [])
|
||||
end
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
class Weapps::CreateCourseForm
|
||||
include ActiveModel::Model
|
||||
|
||||
attr_accessor :course
|
||||
attr_accessor :name, :course_list_name, :credit, :course_module_types
|
||||
|
||||
validates :name, presence: true
|
||||
validates :course_list_name, presence: true
|
||||
|
||||
validate :course_name_prefix
|
||||
validate :check_course_modules
|
||||
|
||||
def course_name_prefix
|
||||
raise '课堂名称应以课程名称开头' unless name.index(course_list_name) && name.index(course_list_name) == 0
|
||||
end
|
||||
|
||||
def check_course_modules
|
||||
raise '请至少添加一个课堂模块' if course_module_types.blank?
|
||||
end
|
||||
end
|
@ -0,0 +1,30 @@
|
||||
class Weapps::CreateCourseService < ApplicationService
|
||||
attr_reader :course, :params
|
||||
|
||||
def initialize(course, params)
|
||||
@course = course
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
Weapps::CreateCourseForm.new(form_params).validate!
|
||||
|
||||
ActiveRecord::Base.transaction do
|
||||
course.name = params[:name].to_s.strip
|
||||
course.school_id = course.teacher&.school_id
|
||||
course.is_public = 0
|
||||
course.credit = params[:credit].blank? ? nil : params[:credit]
|
||||
course.save!
|
||||
|
||||
course.generate_invite_code
|
||||
CourseMember.create!(course_id: course.id, user_id: course.tea_id, role: 1)
|
||||
course.create_course_modules(params[:course_module_types])
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def form_params
|
||||
params.merge(course: course)
|
||||
end
|
||||
end
|
Loading…
Reference in new issue