添加我的作品列表接口

This commit is contained in:
z9hang 2015-01-21 11:28:09 +08:00
parent b949f70c64
commit b13f2b2dc8
7 changed files with 75 additions and 3 deletions

View File

@ -20,8 +20,18 @@ module Mobile
present :status, 0
end
end
end
desc "我的作品列表"
params do
requires :token, type: String
end
get ':user_id/homework_attachs' do
ue = Homeworks.get_service.my_homework_list params,current_user.nil? ? User.find(2):current_user
present :data, ue,with: Mobile::Entities::Course
present :status, 0
end
end
end
end
end

View File

@ -51,7 +51,7 @@ module Mobile
desc '获取用户课程'
params do
optional :token, type: String
end
get ':id/courses' do

View File

@ -50,6 +50,9 @@ module Mobile
c[:course].teacher
end
end
expose :my_homework,using: Mobile::Entities::HomeworkAttach do |f, opt|
f[:my_homework] if f.is_a?(Hash) && f.key?(:my_homework)
end
end
end
end

View File

@ -5,6 +5,12 @@ module Mobile
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::Bid)
if f.respond_to?(field)
f.send(field)
else
end
end
end
end

View File

@ -0,0 +1,28 @@
module Mobile
module Entities
class HomeworkAttach < Grape::Entity
def self.homework_attach_expose(field)
expose field do |f,opt|
if f.is_a?(Hash) && f.key?(field)
f[field]
elsif f.is_a?(::HomeworkAttach)
if f.respond_to?(field)
f.send(field)
else
case field
when :homework_times
f.bid.courses.first.homeworks.index(f.bid) + 1 unless (f.bid.nil? || f.bid.courses.nil? || f.bid.courses.first.nil?)
#course.homeworks.index(@bid) + 1
end
end
end
end
end
homework_attach_expose :id
homework_attach_expose :name
homework_attach_expose :homework_times
#homework_attach_expose :user
end
end
end

View File

@ -153,4 +153,29 @@ class HomeworkService
reply_time = fulr.created_on
end
end
#我的作品列表
def my_homework_list params,current_user
@user = User.find(params[:user_id])
if !current_user.admin? && !@user.active?
raise '404'
return
end
if current_user == @user || current_user.admin?
membership = @user.coursememberships.all
else
membership = @user.coursememberships.all(:conditions => Course.visible_condition(current_user))
end
membership.sort! {|older, newer| newer.created_on <=> older.created_on }
course_list = []
membership.each do |mp|
my_homeworks = []
mp.course.homeworks.each do |bid|
hw = bid.homeworks.where("user_id = #{current_user.id}")
my_homeworks << hw[0] unless (hw.nil? || hw[0].nil?)
end
course_list << {:course => mp.course,:img_url => url_to_avatar(mp.course),:my_homework => my_homeworks}
end
course_list
end
end

View File

@ -129,7 +129,7 @@ class UsersService
if current_user == @user || current_user.admin?
membership = @user.coursememberships.all
else
membership = @user.coursememberships.all(:conditions => Course.visible_condition(User.current))
membership = @user.coursememberships.all(:conditions => Course.visible_condition(current_user))
end
membership.sort! {|older, newer| newer.created_on <=> older.created_on }
course_list = []