diff --git a/app/api/mobile/apis/courses.rb b/app/api/mobile/apis/courses.rb index 0ae3bba1d..d3f0e8cef 100644 --- a/app/api/mobile/apis/courses.rb +++ b/app/api/mobile/apis/courses.rb @@ -116,8 +116,15 @@ module Mobile delete ":id" do authenticate! cs = CoursesService.new - cs.exit_course({"object_id" => params[:id]}, current_user) - {status: 0} + status = cs.exit_course({:object_id => params[:id]}, current_user) + out = {status: status} + message = case status + when 0; "退出成功" + when 1; "您不在课程中" + when 2; "您还未登录" + else; "未知错误,请稍后再试" + end + out.merge(message: message) end desc "搜索课程" diff --git a/app/services/courses_service.rb b/app/services/courses_service.rb index 23c25d302..d8dd8299b 100644 --- a/app/services/courses_service.rb +++ b/app/services/courses_service.rb @@ -197,14 +197,27 @@ class CoursesService #退出课程 #object_id: 课程id #user:当前用户 + #@state == 0 退出成功 + #@state == 1 不在课程中 + #@state == 2 您还未登录 + #@state 其他 未知错误,请稍后再试 def exit_course params,user + if user.nil? + @state = 2 + return @state + end @member = Member.where('course_id = ? and user_id = ?', params[:object_id], user.id) + if @member.nil? || @member.count == 0 + @state = 1 + return @state + end @member.first.destroy - joined = StudentsForCourse.where('student_id = ? and course_id = ?', user.id, params[:object_id]) joined.each do |join| join.delete + @state = 0 end + @state end #加入课程