| @@ -1,8 +1,8 @@ | |||
| class Api::V1::Issues::JournalsController < Api::V1::IssuesController | |||
| before_action :require_public_and_member_above, only: [:index, :create, :destroy] | |||
| before_action :load_issue, only: [:index, :create, :destroy] | |||
| before_action :load_journal, only: [:destroy] | |||
| before_action :require_public_and_member_above, only: [:index, :create, :children_journals, :update, :destroy] | |||
| before_action :load_issue, only: [:index, :create, :children_journals, :update, :destroy] | |||
| before_action :load_journal, only: [:children_journals, :update, :destroy] | |||
| def index | |||
| @object_results = Api::V1::Issues::Journals::ListService.call(@issue, query_params, current_user) | |||
| @@ -13,6 +13,15 @@ class Api::V1::Issues::JournalsController < Api::V1::IssuesController | |||
| @object_result = Api::V1::Issues::Journals::CreateService.call(@issue, journal_params, current_user) | |||
| end | |||
| def children_journals | |||
| @object_results = Api::V1::Issues::Journals::ChildrenListService.call(@issue, @journal, query_params, current_user) | |||
| @journals = kaminari_paginate(@object_results) | |||
| end | |||
| def update | |||
| @object_result = Api::V1::Issues::Journals::UpdateService.call(@issue, @journal, journal_params, current_user) | |||
| end | |||
| def destroy | |||
| if @journal.destroy! | |||
| render_ok | |||
| @@ -28,11 +37,11 @@ class Api::V1::Issues::JournalsController < Api::V1::IssuesController | |||
| end | |||
| def journal_params | |||
| params.permit(:notes, :parent_id, :attachment_ids => []) | |||
| params.permit(:notes, :parent_id, :reply_id, :attachment_ids => []) | |||
| end | |||
| def load_journal | |||
| @journal = @issue.journals.find_by_id(params[:id]) | |||
| @journal = Journal.find_by_id(params[:id]) | |||
| return render_not_found("评论不存在!") unless @journal.present? | |||
| end | |||
| @@ -40,7 +40,7 @@ class Journal < ApplicationRecord | |||
| belongs_to :journalized, polymorphic: true | |||
| belongs_to :review, optional: true | |||
| belongs_to :resolveer, class_name: 'User', foreign_key: :resolveer_id, optional: true | |||
| belongs_to :parent_journal, class_name: 'Journal', foreign_key: :parent_id, optional: true | |||
| belongs_to :parent_journal, class_name: 'Journal', foreign_key: :parent_id, optional: true, counter_cache: :comments_count | |||
| belongs_to :reply_journal, class_name: 'Journal', foreign_key: :reply_id, optional: true | |||
| has_many :journal_details, :dependent => :delete_all | |||
| has_many :attachments, as: :container, dependent: :destroy | |||
| @@ -42,4 +42,7 @@ module Api::V1::Issues::Concerns::Checkable | |||
| end | |||
| end | |||
| def check_parent_journal(parent_id) | |||
| raise ApplicationService::Error, "ParentJournal不存在!" unless Journal.find_by_id(parent_id).present? | |||
| end | |||
| end | |||
| @@ -0,0 +1,42 @@ | |||
| class Api::V1::Issues::Journals::ChildrenListService < ApplicationService | |||
| include ActiveModel::Model | |||
| attr_reader :issue, :journal, :keyword, :sort_by, :sort_direction | |||
| attr_accessor :queried_journals | |||
| validates :sort_by, inclusion: {in: Journal.column_names, message: '请输入正确的SortBy'}, allow_blank: true | |||
| validates :sort_direction, inclusion: {in: %w(asc desc), message: '请输入正确的SortDirection'}, allow_blank: true | |||
| def initialize(issue, journal, params, current_user=nil) | |||
| @issue = issue | |||
| @journal = journal | |||
| @keyword = params[:keyword] | |||
| @sort_by = params[:sort_by].present? ? params[:sort_by] : 'created_on' | |||
| @sort_direction = (params[:sort_direction].present? ? params[:sort_direction] : 'asc').downcase | |||
| end | |||
| def call | |||
| raise Error, errors.full_messages.join(", ") unless valid? | |||
| begin | |||
| journal_query_data | |||
| return @queried_journals | |||
| rescue | |||
| raise Error, "服务器错误,请联系系统管理员!" | |||
| end | |||
| end | |||
| private | |||
| def journal_query_data | |||
| @queried_journals = journal.children_journals | |||
| @queried_journals = @queried_journals.ransack(notes_cont: keyword).result if keyword.present? | |||
| @queried_journals = @queried_journals.includes(:user, :attachments, :reply_journal) | |||
| @queried_journals = @queried_journals.reorder("journals.#{sort_by} #{sort_direction}").distinct | |||
| @queried_journals | |||
| end | |||
| end | |||
| @@ -1,32 +1,41 @@ | |||
| class Api::V1::Issues::Journals::CreateService < ApplicationService | |||
| include ActiveModel::Model | |||
| include Api::V1::Issues::Concerns::Checkable | |||
| include Api::V1::Issues::Concerns::Loadable | |||
| attr_reader :issue, :current_user, :notes, :parent_id, :attachment_ids | |||
| attr_reader :issue, :current_user, :notes, :parent_id, :reply_id, :attachment_ids | |||
| attr_accessor :created_journal | |||
| validates :issue, :current_user, presence: true | |||
| validates :notes, :issue, :current_user, presence: true | |||
| def initialize(issue, params, current_user=nil) | |||
| @issue = issue | |||
| @notes = params[:notes] | |||
| @parent_id = params[:parent_id] | |||
| @reply_id = params[:reply_id] | |||
| @attachment_ids = params[:attachment_ids] | |||
| @current_user = current_user | |||
| end | |||
| def call | |||
| raise Error, errors.full_messages.join(", ") unless valid? | |||
| raise Error, "请输入回复的评论ID" if parent_id.present? && !reply_id.present? | |||
| ActiveRecord::Base.transaction do | |||
| check_parent_journal(parent_id) if parent_id.present? | |||
| check_parent_journal(reply_id) if reply_id.present? | |||
| check_attachments(attachment_ids) unless attachment_ids.blank? | |||
| load_attachments(attachment_ids) unless attachment_ids.blank? | |||
| try_lock("Api::V1::Issues::Journals::CreateService:#{@issue.id}") | |||
| @created_journal = Journal.new(journal_attributes) | |||
| @created_journal = @issue.journals.new(journal_attributes) | |||
| build_comment_participants | |||
| @created_journal.attachments = @attachments | |||
| @created_journal.attachments = @attachments unless attachment_ids.blank? | |||
| @created_journal.save! | |||
| unlock("Api::V1::Issues::Journals::CreateService:#{@issue.id}") | |||
| @created_journal | |||
| end | |||
| end | |||
| @@ -34,10 +43,12 @@ class Api::V1::Issues::Journals::CreateService < ApplicationService | |||
| def journal_attributes | |||
| journal_attributes = { | |||
| notes: notes | |||
| notes: notes, | |||
| user_id: current_user.id | |||
| } | |||
| journal_attributes.merge!({parent_id: parent_id}) if parent_id.present? | |||
| journal_attributes.merge!({reply_id: reply_id}) if reply_id.present? | |||
| journal_attributes | |||
| end | |||
| @@ -0,0 +1,37 @@ | |||
| class Api::V1::Issues::Journals::UpdateService < ApplicationService | |||
| include ActiveModel::Model | |||
| include Api::V1::Issues::Concerns::Checkable | |||
| include Api::V1::Issues::Concerns::Loadable | |||
| attr_reader :issue, :journal, :current_user, :notes, :attachment_ids | |||
| attr_accessor :updated_journal | |||
| validates :notes, :issue, :journal, :current_user, presence: true | |||
| def initialize(issue, journal, params, current_user=nil) | |||
| @issue = issue | |||
| @journal = journal | |||
| @notes = params[:notes] | |||
| @attachment_ids = params[:attachment_ids] | |||
| @current_user = current_user | |||
| end | |||
| def call | |||
| raise Error, errors.full_messages.join(", ") unless valid? | |||
| ActiveRecord::Base.transaction do | |||
| check_attachments(attachment_ids) unless attachment_ids.blank? | |||
| load_attachments(attachment_ids) unless attachment_ids.blank? | |||
| try_lock("Api::V1::Issues::Journals::UpdateService:#{@issue.id}:#{@journal.id}") | |||
| @updated_journal = @journal | |||
| @updated_journal.notes = notes | |||
| @updated_journal.attachments = @attachments unless attachment_ids.blank? | |||
| @updated_journal.save! | |||
| unlock("Api::V1::Issues::Journals::UpdateService:#{@issue.id}:#{@journal.id}") | |||
| @updated_journal | |||
| end | |||
| end | |||
| end | |||
| @@ -0,0 +1,4 @@ | |||
| json.total_count @journals.total_count | |||
| json.journals @journals do |journal| | |||
| json.partial! "children_detail", journal: journal | |||
| end | |||
| @@ -0,0 +1 @@ | |||
| json.partial! "detail", journal: @object_result | |||