Merge remote-tracking branch 'origin/dev_aliyun' into dev_aliyun

dev_ysm^2
杨树明 5 years ago
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

@ -1,5 +1,5 @@
class Weapps::HomesController < Weapps::BaseController
# before_action :require_wechat_login!
before_action :require_wechat_login!
def show
# banner

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

@ -20,6 +20,7 @@ json.course_count @course_count
json.courses @courses.each do |course|
json.(course, :id, :name, :visits, :course_members_count)
json.creator course.teacher.real_name
json.avatar_url url_to_avatar(course.teacher)
json.school course.school&.name
course_member = @category == "study" ? course.students.where(user_id: @user.id).first : course.teachers.where(user_id: @user.id).first
json.sticky course_member.sticky

@ -879,6 +879,8 @@ Rails.application.routes.draw do
resources :course_stickies, only: [:create] do
post :cancel_sticky, on: :collection
end
resources :courses, only: [:create, :update, :edit]
end
resources :users_for_partners, only: [:index]

Loading…
Cancel
Save