修复退出课程接口

This commit is contained in:
z9hang 2015-01-07 11:37:48 +08:00
parent b711793d7c
commit 303752fc7e
2 changed files with 23 additions and 3 deletions

View File

@ -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 "搜索课程"

View File

@ -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
#加入课程