From af2bd66847f9d04b8db350a967da7d05d1f45792 Mon Sep 17 00:00:00 2001 From: Yiang Gan Date: Fri, 1 Apr 2016 18:23:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E5=A4=8Dapi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/mobile/api.rb | 2 + app/api/mobile/apis/comments.rb | 2 +- app/api/mobile/apis/issues.rb | 1 + app/api/mobile/apis/new_comment.rb | 118 +++++++++++++++++++++++++++ app/api/mobile/entities/issue.rb | 3 + app/api/mobile/entities/jours.rb | 6 +- app/api/mobile/entities/whomework.rb | 3 + app/helpers/api_helper.rb | 41 ++++++++++ app/models/journal.rb | 2 +- 9 files changed, 174 insertions(+), 4 deletions(-) create mode 100644 app/api/mobile/apis/new_comment.rb diff --git a/app/api/mobile/api.rb b/app/api/mobile/api.rb index 8f79d977c..7235a447b 100644 --- a/app/api/mobile/api.rb +++ b/app/api/mobile/api.rb @@ -14,6 +14,7 @@ module Mobile require_relative 'apis/journal_for_messages' require_relative 'apis/messages' require_relative 'apis/blog_comments' + require_relative 'apis/new_comment' class API < Grape::API version 'v1', using: :path @@ -54,6 +55,7 @@ module Mobile mount Apis::JournalForMessages mount Apis::Messages mount Apis::BlogComments + mount Apis::NewComment #add_swagger_documentation ({api_version: 'v1', base_path: 'http://u06.shellinfo.cn/trustie/api'}) #add_swagger_documentation ({api_version: 'v1', base_path: '/api'}) if Rails.env.development? diff --git a/app/api/mobile/apis/comments.rb b/app/api/mobile/apis/comments.rb index 7fa676161..15f020bc2 100644 --- a/app/api/mobile/apis/comments.rb +++ b/app/api/mobile/apis/comments.rb @@ -115,7 +115,7 @@ module Mobile desc '通知评论列表' params do - requires :token, type: String + #requires :token, type: String requires :notice_id,type:Integer,desc:'通知id' optional :page,type:Integer,desc:'页码' end diff --git a/app/api/mobile/apis/issues.rb b/app/api/mobile/apis/issues.rb index 1c7a9ef3d..1815c85f3 100644 --- a/app/api/mobile/apis/issues.rb +++ b/app/api/mobile/apis/issues.rb @@ -4,6 +4,7 @@ module Mobile module Apis class Issues< Grape::API resources :issues do + include IssuesHelper desc "get special issuse" get ':id' do diff --git a/app/api/mobile/apis/new_comment.rb b/app/api/mobile/apis/new_comment.rb new file mode 100644 index 000000000..63775c12d --- /dev/null +++ b/app/api/mobile/apis/new_comment.rb @@ -0,0 +1,118 @@ +#coding=utf-8 + +module Mobile + module Apis + class NewComment< Grape::API + include ApplicationHelper + include ApiHelper + resources :new_comment do + + desc "add a new comment" + params do + requires :type, type: String + requires :content, type: String + end + get ':id' do + type = params[:type] + result = 1 + current_user = User.find 8686 + if params[:content]!="" && current_user + case type + when "HomeworkCommon" + homework_common = HomeworkCommon.find(params[:id]) + feedback = HomeworkCommon.add_homework_jour(current_user, params[:content], params[:id]) + if (feedback.errors.empty?) + homework_common.update_attributes(:updated_at => Time.now) + result = 2 + else + result = 3 + end + when "News" + news = News.find(params[:id]) + comment = Comment.new + comment.comments = params[:content] + comment.author = current_user + if news.comments << comment + result = 2 + else + result = 3 + end + when "Message" + message = Message.find(params[:id]) + board = Board.find(message.board_id) + topic = message.root + reply = Message.new + reply.author = current_user + reply.board = board + reply.content = params[:content] + reply.parent_id = params[:id] + reply.subject = "RE: #{topic.subject}" + if topic.children << reply + result = 2 + else + result = 3 + end + when "JournalsForMessage" + jour = JournalsForMessage.find params[:id] + parent_id = params[:id] + author_id = current_user.id + reply_user_id = jour.user_id + reply_id = params[:id] + content = params[:content] + options = {:user_id => author_id, + :status => true, + :m_parent_id => parent_id, + :m_reply_id => reply_id, + :reply_id => reply_user_id, + :notes => content, + :is_readed => false} + jfm = jour.user.add_jour(nil, nil, nil, options) + if jfm.errors.empty? + (JournalsForMessage.find parent_id).update_attribute(:updated_on,Time.now) + result = 2 + else + result = 3 + end + when 'Issue' + issue = Issue.find params[:id] + jour = Journal.new + jour.user_id = current_user.id + jour.notes = params[:content] + jour.journalized_id = params[:id] + jour.journalized_type = "Issue" + if jour.save + result = 2 + else + result = 3 + end + when 'BlogComment' + blog = BlogComment.find(params[:id]).root + blogComment = BlogComment.new + blogComment.author = current_user + blogComment.blog = blog.blog + blogComment.content = params[:content] + blogComment.title = "RE: #{blog.title}" + blog.children << blogComment + if blog.save + result = 2 + else + result = 3 + end + end + if result == 2 + update_course_activity_api(type,params[:id]) + update_user_activity_api(type,params[:id]) + update_org_activity_api(type,params[:id]) + update_forge_activity_api(type,params[:id]) + update_principal_activity_api(type,params[:id]) + end + else + result = 4 + end + present :data, result + present :status, 0 + end + end + end + end +end diff --git a/app/api/mobile/entities/issue.rb b/app/api/mobile/entities/issue.rb index cd0cbb0ce..55923f468 100644 --- a/app/api/mobile/entities/issue.rb +++ b/app/api/mobile/entities/issue.rb @@ -22,6 +22,8 @@ module Mobile (get_user(issue.assigned_to_id)).login when :issue_status IssueStatus.find(issue.status_id).name + when :journals_count + issue.journals.count end end end @@ -35,6 +37,7 @@ module Mobile issue_expose :issue_priority issue_expose :issue_assigned_to issue_expose :issue_status + issue_expose :journals_count expose :issue_journals, using: Mobile::Entities::Journal do |f, opt| if f.is_a?(::Issue) f.journals diff --git a/app/api/mobile/entities/jours.rb b/app/api/mobile/entities/jours.rb index 257721d3e..e3ce04cf6 100644 --- a/app/api/mobile/entities/jours.rb +++ b/app/api/mobile/entities/jours.rb @@ -3,7 +3,6 @@ module Mobile #普通留言 class Jours < Grape::Entity include Redmine::I18n - include WordsHelper def self.jours_expose(field) expose field do |f,opt| if f.is_a?(Hash) && f.key?(field) @@ -18,6 +17,8 @@ module Mobile case field when :lasted_comment time_from_now f.created_on + when :reply_count + f.children.count end end end @@ -33,6 +34,7 @@ module Mobile jours_expose :notes jours_expose :m_reply_id jours_expose :m_parent_id + jours_expose :reply_count expose :course,using:Mobile::Entities::Course do |f,opt| if f.is_a?(::JournalsForMessage) && f[:jour_type] == "Course" f.course @@ -43,7 +45,7 @@ module Mobile end expose :child_reply,using: Mobile::Entities::Jours do |f, opt| if f.is_a?(::JournalsForMessage) - fetch_user_leaveWord_reply(f) + f.children end end end diff --git a/app/api/mobile/entities/whomework.rb b/app/api/mobile/entities/whomework.rb index 562e3a1a7..b24c50aa2 100644 --- a/app/api/mobile/entities/whomework.rb +++ b/app/api/mobile/entities/whomework.rb @@ -41,6 +41,9 @@ module Mobile w.user end end + expose :current_user, using: Mobile::Entities::User do |w, opt| + current_user + end expose :name expose :description expose :publish_time diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb index f3f347ae3..1955b78c0 100644 --- a/app/helpers/api_helper.rb +++ b/app/helpers/api_helper.rb @@ -409,4 +409,45 @@ module ApiHelper l(:text_journal_deleted, :label => label, :old => old_value).html_safe end end + + #课程动态的更新 + def update_course_activity_api type, id + course_activity = CourseActivity.where("course_act_type=? and course_act_id =?", type.to_s, id).first + if course_activity + course_activity.updated_at = Time.now + course_activity.save + end + end + #首页动态更新 + def update_user_activity_api type, id + user_activity = UserActivity.where("act_type=? and act_id =?", type.to_s, id).first + if user_activity + user_activity.updated_at = Time.now + user_activity.save + end + end + #项目动态更新 + def update_forge_activity_api type, id + forge_activity = ForgeActivity.where("forge_act_type=? and forge_act_id=?", type.to_s, id).first + if forge_activity + forge_activity.updated_at = Time.now + forge_activity.save + end + end + #组织动态更新 + def update_org_activity_api type , id + org_activity = OrgActivity.where("org_act_type=? and org_act_id =?", type.to_s, id).first + if org_activity + org_activity.updated_at = Time.now + org_activity.save + end + end + #个人动态更新 + def update_principal_activity_api type, id + principal_activity = PrincipalActivity.where("principal_act_type=? and principal_act_id =?", type.to_s, id).first + if principal_activity + principal_activity.updated_at = Time.now + principal_activity.save + end + end end \ No newline at end of file diff --git a/app/models/journal.rb b/app/models/journal.rb index 2432f1db1..49fb0aa35 100644 --- a/app/models/journal.rb +++ b/app/models/journal.rb @@ -54,7 +54,7 @@ class Journal < ActiveRecord::Base # fq after_save :act_as_activity,:be_user_score, :act_as_forge_message, act_as_at_message(:notes, :user_id) - after_create :update_issue_timeissue, :issue_wechat_message + after_create :issue_wechat_message # end #after_destroy :down_user_score #before_save :be_user_score