socialforge/app/api/mobile/apis/resources.rb

119 lines
3.1 KiB
Ruby
Raw Normal View History

2016-06-27 17:24:06 +08:00
#coding=utf-8
module Mobile
module Apis
class Resources < Grape::API
resource :resources do
desc '获取所有课件'
params do
requires :token, type: String
2016-07-15 14:44:37 +08:00
requires :page, type: Integer
2016-06-27 17:24:06 +08:00
end
2016-07-15 14:44:37 +08:00
post do
2016-06-27 17:24:06 +08:00
authenticate!
2016-07-15 14:44:37 +08:00
page = params[:page] ? params[:page] : 0
rs = ResourcesService.new
# data = current_user.course_attachments
data = rs.all_course_attachments current_user
2016-07-15 14:44:37 +08:00
all_count = data.count
data = data.limit(10).offset(page * 10)
count = data.count
present :data, data, with: Mobile::Entities::Attachment,user: current_user
2016-07-15 14:44:37 +08:00
present :all_count, all_count
present :count, count
present :page, page
2016-06-27 17:24:06 +08:00
present :status, 0
end
desc '获取所有作业'
params do
requires :token, type: String
2016-07-15 14:44:37 +08:00
requires :page, type: Integer
2016-06-27 17:24:06 +08:00
end
2016-07-15 14:44:37 +08:00
post 'homeworks' do
2016-06-27 17:24:06 +08:00
authenticate!
2016-07-15 14:44:37 +08:00
page = params[:page] ? params[:page] : 0
rs = ResourcesService.new
homeworks = rs.all_homework_commons current_user
2016-06-27 17:24:06 +08:00
2016-07-15 14:44:37 +08:00
all_count = homeworks.count
homeworks = homeworks.limit(10).offset(page * 10)
count = homeworks.count
present :data, homeworks, with: Mobile::Entities::Homework,user: current_user
2016-07-15 14:44:37 +08:00
present :all_count, all_count
present :count, count
present :page, page
2016-06-27 17:24:06 +08:00
present :status, 0
end
desc '获取所有测验'
params do
requires :token, type: String
2016-07-15 14:44:37 +08:00
requires :page, type: Integer
2016-06-27 17:24:06 +08:00
end
2016-07-15 14:44:37 +08:00
post 'exercises' do
2016-06-27 17:24:06 +08:00
authenticate!
2016-07-15 14:44:37 +08:00
page = params[:page] ? params[:page] : 0
rs = ResourcesService.new
exercises = rs.all_exercises current_user
2016-07-15 14:44:37 +08:00
all_count = exercises.count
exercises = exercises.limit(10).offset(page * 10)
count = exercises.count
present :data, exercises, with: Mobile::Entities::Exercise,user: current_user
2016-07-15 14:44:37 +08:00
present :all_count, all_count
present :count, count
present :page, page
2016-06-27 17:24:06 +08:00
present :status, 0
end
2016-07-02 23:46:45 +08:00
desc '发送资源'
params do
requires :token, type: String
requires :course_ids, type: Array[Integer]
requires :send_id, type: Integer
2016-07-18 11:08:02 +08:00
requires :send_type, type: Integer
2016-07-02 23:46:45 +08:00
end
post 'send' do
authenticate!
2016-06-27 17:24:06 +08:00
2016-07-02 23:46:45 +08:00
rs = ResourcesService.new
2016-07-18 11:08:02 +08:00
ori = nil
flag = false
save_message = nil
case params[:send_type]
when 1
ori, flag, save_message = rs.send_resource_to_course(current_user,params)
when 2
ori, flag, save_message = rs.send_homework_to_course(current_user,params)
when 3
ori, flag, save_message = rs.send_exercise_to_course(current_user,params)
end
2016-07-02 23:46:45 +08:00
if flag
present :status, 0
else
{status: -1, message: save_message.first}
end
end
2016-06-27 17:24:06 +08:00
2016-07-02 23:46:45 +08:00
end
2016-06-27 17:24:06 +08:00
end
end
end