From c787bb5bcdc7d005a5317d6b47856a704000a471 Mon Sep 17 00:00:00 2001 From: yuanke <249218296@qq.com> Date: Tue, 9 Aug 2016 10:55:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A8=E6=80=81=E5=8E=BB=E6=8E=89=E8=AE=A4?= =?UTF-8?q?=E8=AF=81=EF=BC=8C=E5=8A=A0=E4=B8=8A=E5=87=BA=E9=94=99=E6=8F=90?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/apis/activities.rb | 79 +++++++++++---------- app/api/mobile/apis/blog_comments.rb | 23 +++--- app/api/mobile/apis/issues.rb | 25 ++++--- app/api/mobile/apis/journal_for_messages.rb | 25 ++++--- app/api/mobile/apis/messages.rb | 25 ++++--- app/api/mobile/apis/newss.rb | 25 ++++--- app/api/mobile/apis/whomeworks.rb | 33 +++++---- 7 files changed, 135 insertions(+), 100 deletions(-) diff --git a/app/api/mobile/apis/activities.rb b/app/api/mobile/apis/activities.rb index b4fb90ea7..4d07c23f8 100644 --- a/app/api/mobile/apis/activities.rb +++ b/app/api/mobile/apis/activities.rb @@ -13,50 +13,55 @@ module Mobile requires :container_type, type: String end post do - authenticate! + begin + authenticate! - user = current_user + user = current_user - container_type = params[:container_type] ? params[:container_type] : "All" + container_type = params[:container_type] ? params[:container_type] : "All" - if container_type == "All" || container_type == "Project" - shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id) - user_project_ids = (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).join(",") + ")" - project_types = "('Message','Issue','Project')" - end + if container_type == "All" || container_type == "Project" + shield_project_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Project'").map(&:shield_id) + user_project_ids = (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).empty? ? "(-1)" : "(" + (user.projects.where("status = 1").map{|project| project.id}-shield_project_ids).join(",") + ")" + project_types = "('Message','Issue','Project')" + end - if container_type == "All" || container_type == "Course" - shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id) - user_course_ids = (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).join(",") + ")" - course_types = "('Message','News','HomeworkCommon','Poll','Course')" - end + if container_type == "All" || container_type == "Course" + shield_course_ids = ShieldActivity.select("shield_id").where("container_type='User' and container_id=#{user.id} and shield_type='Course'").map(&:shield_id) + user_course_ids = (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).empty? ? "(-1)" : "(" + (user.courses.where("is_delete = 0").map{|course| course.id}-shield_course_ids).join(",") + ")" + course_types = "('Message','News','HomeworkCommon','Poll','Course')" + end - page = params[:page] ? params[:page] : 0 - principal_types = "JournalsForMessage" - watched_user_ids = User.watched_by(user.id).count == 0 ? " " : ("," + User.watched_by(user.id).map{|u| u.id.to_s }.join(',')) - user_ids = "(" + user.id.to_s + watched_user_ids + ")" - watched_user_blog_ids = Blog.select("id").where("author_id in #{user_ids}").count == 0 ? " " :Blog.select("id").where("author_id in #{user_ids}").map { |blog| blog.id}.join(",") - blog_ids = "(" + watched_user_blog_ids + ")" + page = params[:page] ? params[:page] : 0 + principal_types = "JournalsForMessage" + watched_user_ids = User.watched_by(user.id).count == 0 ? " " : ("," + User.watched_by(user.id).map{|u| u.id.to_s }.join(',')) + user_ids = "(" + user.id.to_s + watched_user_ids + ")" + watched_user_blog_ids = Blog.select("id").where("author_id in #{user_ids}").count == 0 ? " " :Blog.select("id").where("author_id in #{user_ids}").map { |blog| blog.id}.join(",") + blog_ids = "(" + watched_user_blog_ids + ")" - if container_type == "Course" - activities = UserActivity.where("(container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})").order('updated_at desc') - elsif container_type == "Project" - activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})").order('updated_at desc') - else - activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" + - "or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+ - "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{user.id}) " + - "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc') + if container_type == "Course" + activities = UserActivity.where("(container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types})").order('updated_at desc') + elsif container_type == "Project" + activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})").order('updated_at desc') + else + activities = UserActivity.where("(container_type = 'Project' and container_id in #{user_project_ids} and act_type in #{project_types})" + + "or (container_type = 'Course' and container_id in #{user_course_ids} and act_type in #{course_types}) "+ + "or (container_type = 'Principal' and act_type= '#{principal_types}' and container_id = #{user.id}) " + + "or (container_type = 'Blog' and act_type= 'BlogComment' and container_id in #{blog_ids})").order('updated_at desc') + end + all_count = activities.count + activities = activities.limit(10).offset(page * 10) + count = activities.count + present :data, activities, with: Mobile::Entities::Activity,user: user + present :all_count, all_count + present :count, count + present :page, page + present :status, 0 + present :container_type, container_type + rescue Exception=>e + present :status, -1 + present :message, e.message end - all_count = activities.count - activities = activities.limit(10).offset(page * 10) - count = activities.count - present :data, activities, with: Mobile::Entities::Activity,user: user - present :all_count, all_count - present :count, count - present :page, page - present :status, 0 - present :container_type, container_type end end end diff --git a/app/api/mobile/apis/blog_comments.rb b/app/api/mobile/apis/blog_comments.rb index 39b6a486a..ddfd65524 100644 --- a/app/api/mobile/apis/blog_comments.rb +++ b/app/api/mobile/apis/blog_comments.rb @@ -7,17 +7,22 @@ module Mobile desc "get special topic" post ':id' do - user = current_user + begin + user = current_user - #0一级回复的更多 1 二级回复的更多 - type = params[:type] || 0 - page = params[:page] || 0 + #0一级回复的更多 1 二级回复的更多 + type = params[:type] || 0 + page = params[:page] || 0 - blog = BlogComment.find params[:id] - present :data, blog, with: Mobile::Entities::BlogComment,user: user,type: type,page: page - present :type, type - present :page, page - present :status, 0 + blog = BlogComment.find params[:id] + present :data, blog, with: Mobile::Entities::BlogComment,user: user,type: type,page: page + present :type, type + present :page, page + present :status, 0 + rescue Exception=>e + present :status, -1 + present :message, e.message + end end end end diff --git a/app/api/mobile/apis/issues.rb b/app/api/mobile/apis/issues.rb index c0f705f4f..9223332e9 100644 --- a/app/api/mobile/apis/issues.rb +++ b/app/api/mobile/apis/issues.rb @@ -8,17 +8,22 @@ module Mobile desc "get special issuse" post ':id' do - # authenticate! - user = current_user + begin + # authenticate! + user = current_user - #0一级回复的更多 1 二级回复的更多 - type = params[:type] || 0 - page = params[:page] || 0 - issue = Issue.find params[:id] - present :data, issue, with: Mobile::Entities::Issue,user: user,type: type,page: page - present :type, type - present :page, page - present :status, 0 + #0一级回复的更多 1 二级回复的更多 + type = params[:type] || 0 + page = params[:page] || 0 + issue = Issue.find params[:id] + present :data, issue, with: Mobile::Entities::Issue,user: user,type: type,page: page + present :type, type + present :page, page + present :status, 0 + rescue Exception=>e + present :status, -1 + present :message, e.message + end end end end diff --git a/app/api/mobile/apis/journal_for_messages.rb b/app/api/mobile/apis/journal_for_messages.rb index 138e93340..d835d61c5 100644 --- a/app/api/mobile/apis/journal_for_messages.rb +++ b/app/api/mobile/apis/journal_for_messages.rb @@ -7,18 +7,23 @@ module Mobile desc "get special journal" post ':id' do - # authenticate! - user = current_user + begin + # authenticate! + user = current_user - #0一级回复的更多 1 二级回复的更多 - type = params[:type] || 0 - page = params[:page] || 0 + #0一级回复的更多 1 二级回复的更多 + type = params[:type] || 0 + page = params[:page] || 0 - jour = JournalsForMessage.find params[:id] - present :data, jour, with: Mobile::Entities::Jours,user: user,type: type,page: page - present :type, type - present :page, page - present :status, 0 + jour = JournalsForMessage.find params[:id] + present :data, jour, with: Mobile::Entities::Jours,user: user,type: type,page: page + present :type, type + present :page, page + present :status, 0 + rescue Exception=>e + present :status, -1 + present :message, e.message + end end end end diff --git a/app/api/mobile/apis/messages.rb b/app/api/mobile/apis/messages.rb index 98bd6bd5d..1e20ea38f 100644 --- a/app/api/mobile/apis/messages.rb +++ b/app/api/mobile/apis/messages.rb @@ -7,17 +7,22 @@ module Mobile desc "get special topic" post ':id' do - # authenticate! - user = current_user + begin + # authenticate! + user = current_user - #0一级回复的更多 1 二级回复的更多 - type = params[:type] || 0 - page = params[:page] || 0 - message = Message.find params[:id] - present :data, message, with: Mobile::Entities::Message,user: user,type: type,page: page - present :type, type - present :page, page - present :status, 0 + #0一级回复的更多 1 二级回复的更多 + type = params[:type] || 0 + page = params[:page] || 0 + message = Message.find params[:id] + present :data, message, with: Mobile::Entities::Message,user: user,type: type,page: page + present :type, type + present :page, page + present :status, 0 + rescue Exception=>e + present :status, -1 + present :message, e.message + end end end end diff --git a/app/api/mobile/apis/newss.rb b/app/api/mobile/apis/newss.rb index 63194e6f6..dfe6b5718 100644 --- a/app/api/mobile/apis/newss.rb +++ b/app/api/mobile/apis/newss.rb @@ -7,17 +7,22 @@ module Mobile desc "get special news" post ':id' do - # authenticate! - user = current_user + begin + # authenticate! + user = current_user - #0一级回复的更多 1 二级回复的更多 - type = params[:type] || 0 - page = params[:page] || 0 - news = News.find params[:id] - present :data, news, with: Mobile::Entities::News,user: user,type: type,page: page - present :type, type - present :page, page - present :status, 0 + #0一级回复的更多 1 二级回复的更多 + type = params[:type] || 0 + page = params[:page] || 0 + news = News.find params[:id] + present :data, news, with: Mobile::Entities::News,user: user,type: type,page: page + present :type, type + present :page, page + present :status, 0 + rescue Exception=>e + present :status, -1 + present :message, e.message + end end end end diff --git a/app/api/mobile/apis/whomeworks.rb b/app/api/mobile/apis/whomeworks.rb index 5924c158b..79d04739f 100644 --- a/app/api/mobile/apis/whomeworks.rb +++ b/app/api/mobile/apis/whomeworks.rb @@ -7,23 +7,28 @@ module Mobile desc "get one homework" post ':id' do - # authenticate! - user = current_user + begin + # authenticate! + user = current_user - #0一级回复的更多 1 二级回复的更多 - type = params[:type] || 0 - page = params[:page] || 0 + #0一级回复的更多 1 二级回复的更多 + type = params[:type] || 0 + page = params[:page] || 0 - if type == 0 - homework = HomeworkCommon.find params[:id] - present :data, homework, with: Mobile::Entities::Whomework,user: user,type: type,page: page,comment_type: "homework" - else - jour = JournalsForMessage.find params[:id] - present :data, jour, with: Mobile::Entities::Jours,user: user,type: type,page: page,comment_type: "homework" + if type == 0 + homework = HomeworkCommon.find params[:id] + present :data, homework, with: Mobile::Entities::Whomework,user: user,type: type,page: page,comment_type: "homework" + else + jour = JournalsForMessage.find params[:id] + present :data, jour, with: Mobile::Entities::Jours,user: user,type: type,page: page,comment_type: "homework" + end + present :type, type + present :page, page + present :status, 0 + rescue Exception=>e + present :status, -1 + present :message, e.message end - present :type, type - present :page, page - present :status, 0 end end end