From 85b3df889c03d8b86fdeb2e060ccf74f30fecbe2 Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Sat, 13 Aug 2016 18:35:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=BE=AE=E4=BF=A1=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E8=AF=BE=E7=A8=8B=E5=A4=A7=E7=BA=B2=E7=9A=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/syllabuses.rb | 5 +++-- app/api/mobile/entities/syllabus.rb | 4 ++-- app/services/syllabuses_service.rb | 19 ++++++++----------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/app/api/mobile/apis/syllabuses.rb b/app/api/mobile/apis/syllabuses.rb index 8538fae99..100760f8e 100644 --- a/app/api/mobile/apis/syllabuses.rb +++ b/app/api/mobile/apis/syllabuses.rb @@ -28,9 +28,10 @@ module Mobile ss = SyllabusesService.new sy = ::Syllabus.find(params[:id]) - sy.courses = sy.courses.not_deleted - present :data, sy, with: Mobile::Entities::Syllabus,user: current_user + result = {:title=>sy.title, :id=>sy.id , :user_id=>sy.user_id,:courses=>sy.courses.not_deleted} + + present :data, result, with: Mobile::Entities::Syllabus,user: current_user present :status, 0 end diff --git a/app/api/mobile/entities/syllabus.rb b/app/api/mobile/entities/syllabus.rb index e4dd2e07a..4e9f858c8 100644 --- a/app/api/mobile/entities/syllabus.rb +++ b/app/api/mobile/entities/syllabus.rb @@ -5,8 +5,8 @@ module Mobile expose :id expose :can_setting, if: lambda { |instance, options| options[:user] } do |instance, options| current_user = options[:user] - can_setting = instance.user_id == current_user.id ? true : false - can_setting = false if instance.id.nil? + can_setting = instance[:user_id] == current_user.id ? true : false + can_setting = false if instance[:id].nil? can_setting end expose :courses, using: Mobile::Entities::Course diff --git a/app/services/syllabuses_service.rb b/app/services/syllabuses_service.rb index 1c72529dd..cbc29e790 100644 --- a/app/services/syllabuses_service.rb +++ b/app/services/syllabuses_service.rb @@ -36,24 +36,21 @@ class SyllabusesService # courses.each do |c| # other.courses << c[:course] unless c[:course].syllabus # end + result = [] + i = 0 courses = user.courses.not_deleted syllabus_ids = courses.empty? ? '(-1)' : "(" + courses.map{|course| !course.syllabus_id.nil? && course.syllabus_id}.join(",") + ")" syllabuses = Syllabus.where("id in #{syllabus_ids} or user_id = #{user.id}").order("updated_at desc") syllabuses.each do |syllabus| - syllabus.courses = courses.where("syllabus_id = #{syllabus.id}").select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS updatetime").order("time desc") + tmp_courses = courses.where("syllabus_id = #{syllabus.id}").select("courses.*,(SELECT MAX(updated_at) FROM `course_activities` WHERE course_activities.course_id = courses.id) AS updatetime").order("updatetime desc") + result[i] = {:title=>syllabus.title, :id=>syllabus.id , :user_id=>syllabus.user_id,:courses=>tmp_courses } + i = i + 1 end - - # syllabuses.to_a << other - - syllabuses.to_a - #管理权限 can_setting - # syllabuses.each do |s| - # s = judge_can_setting(s,user) - # end - - syllabuses + # syllabuses.to_a + # syllabuses + result end def after_create_course(course, user)