#coding=utf-8 module Mobile module Apis class Courses < Grape::API resource :courses do desc "获取所有课程" params do optional :school_id, type: Integer, desc: '传入学校id,返回该学校课程列表' requires :per_page_count, type: Integer, desc: '每页总数' requires :page, type: Integer, desc: '当前页码' optional :token, type: String end get do cs = CoursesService.new courses = cs.course_list(params,current_user.nil? ? User.find(2):current_user) present :data, courses, with: Mobile::Entities::Course present :status, 0 end desc "新建课程" #current_user当前用户对象(不是id) # params[:course][:name]:课程名称 #params[:course][:password]:密码 #params[:course][:description]:描述 #params[:course][:is_public]:是否公开1公开,0私有 #params[:course][:open_student]:是否公开学生列表1公开,0不公开,不公开时非课程成员无法看到学生列表 #params[:course][:course_type]:暂时默认给1值。 #params[:term]:学期(秋季学期或春季学期) #params[:time]: 年份(例:2014) #params[:setup_time]:暂不传(貌似已经没用了) #params[:endup_time]: 暂不传(貌似已经没用了) #params[:class_period]:学时总数 params do requires :token, type: String requires :name, type: String, desc: '课程名称' requires :password, type: String, desc: '密码' requires :description, type: String, desc: '描述' requires :is_public, type: Integer, desc: '是否公开 1公开 0私有' requires :open_student, type: Integer, desc: '是否公开学生列表1公开,0不公开,不公开时非课程成员无法看到学生列表' requires :course_type, type:Integer, desc: '暂时传1' requires :term, type: String, desc: '学期(秋季学期或春季学期)' requires :time, type: String, desc: '年份' requires :class_period, type: String, desc: '学时总数' end post do authenticate! cs = CoursesService.new cs_params = { course: params.reject{|k,v| [:term,:time,:class_period].include?(k)}, term: params[:term], time: params[:time], class_period: params[:class_period] } courses = cs.create_course(cs_params, current_user) present :data, courses, with: Mobile::Entities::Course present :status, 0 end desc "编辑课程" params do requires :token, type: String requires :course_id, type: Integer, desc: '课程id' requires :name, type: String, desc: '课程名称' requires :password, type: String, desc: '密码' requires :description, type: String, desc: '描述' requires :is_public, type: Integer, desc: '是否公开 1公开 0私有' requires :open_student, type: Integer, desc: '是否公开学生列表1公开,0不公开,不公开时非课程成员无法看到学生列表' requires :course_type, type:Integer, desc: '暂时传1' requires :term, type: String, desc: '学期(秋季学期或春季学期)' requires :time, type: String, desc: '年份' requires :class_period, type: String, desc: '学时总数' end put do authenticate! cs = CoursesService.new cs_params = { course: params.reject{|k,v| [:term,:time,:class_period].include?(k)}, term: params[:term], time: params[:time], class_period: params[:class_period] } course = ::Course.find(params[:course_id]) # 如果没有传密码过来,那就把原来的密码给上,不然会不更新 if params[:password].nil? || params[:password].blank? cs_params[:course][:password] = course[:password] end cs.edit_course_authorize(current_user,course) course = cs.edit_course(cs_params, course,current_user) present :data, course, with: Mobile::Entities::Course present :status, 0 end post do end desc "加入课程" params do requires :token, type: String requires :course_password, type: String end post ":id" do authenticate! cs = CoursesService.new status = cs.join_course({:object_id => params[:id],:course_password => params[:course_password]},current_user) out = {status: status[:state]} message = case status[:state] when 0; "加入成功" when 1; "密码错误" when 2; "课程已过期 请联系课程管理员重启课程。(在配置课程处)" when 3; "您已经加入了课程" when 4; "您加入的课程不存在" when 5; "您还未登录" else; "未知错误,请稍后再试" end out.merge(message: message) end desc "退出课程" params do requires :token, type: String end delete ":id" do authenticate! cs = CoursesService.new 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 "搜索课程" params do requires :name, type: String, desc: "课程名" optional :token, type: String end get 'search' do cs = CoursesService.new courses = cs.search_course(params,current_user.nil? ? User.find(2):current_user) present :data, courses, with: Mobile::Entities::Course present :status, 0 end desc "课程老师列表" params do requires :token, type: String requires :course_id, type: Integer, desc: "课程id" end get 'teachers' do cs = CoursesService.new teachers = cs.course_teacher_or_student_list({role: '1'}, params[:course_id],current_user) present :data, teachers, with: Mobile::Entities::User present :status, 0 end desc "课程学生列表" params do requires :token, type: String requires :course_id, type: Integer, desc: "课程id" end get 'students' do cs = CoursesService.new students = cs.course_teacher_or_student_list({role: '2'}, params[:course_id],current_user) present :data, students, with: Mobile::Entities::User present :status, 0 end desc "返回单个课程" params do requires :id, type: Integer optional :token, type: String end route_param :id do get do cs = CoursesService.new course = cs.show_course(params,(current_user.nil? ? User.find(2):current_user)) #course = Course.find(params[:id]) present :data, course, with: Mobile::Entities::Course present :status, 0 end end desc "课程作业列表" params do requires :token, type: String end get "homeworks/:id" do cs = CoursesService.new homeworks = cs.homework_list params,current_user present :data, homeworks, with: Mobile::Entities::Homework present :status, 0 end desc "课程通知列表" params do optional :token, type: String end get ":course_id/news" do cs = CoursesService.new news = cs.course_news_list params,current_user.nil? ? User.find(2):current_user present :data, news, with: Mobile::Entities::News present :status, 0 end desc "显示课程通知" params do optional :token, type: String end get "news/:id" do cs = CoursesService.new cs.show_course_news_authorize(current_user.nil? ? User.find(2):current_user) news = cs.show_course_news params,current_user.nil? ? User.find(2):current_user present :data, news, with: Mobile::Entities::News present :status, 0 end desc '用户课程动态' params do requires :token, type: String end get "course_dynamic/:id" do cs = CoursesService.new count = cs.course_dynamic(params,current_user) present :data, count, with: Mobile::Entities::CourseDynamic present :status, 0 end desc '课程课件' params do requires :token, type: String requires :course_id,type: Integer,desc: '课程id' end get ":course_id/attachments" do cs = CoursesService.new count = cs.course_attachments params present :data, count, with: Mobile::Entities::Attachment present :status, 0 end end end end end