180 lines
5.7 KiB
Ruby
180 lines
5.7 KiB
Ruby
class Api::Pm::IssuesController < Api::Pm::BaseController
|
||
before_action :require_login, except: [:index]
|
||
before_action :load_project
|
||
before_action :load_issue, only: %i[show update destroy link_index]
|
||
before_action :load_issues, only: %i[batch_update batch_destroy]
|
||
before_action :check_issue_operate_permission, only: %i[update destroy]
|
||
|
||
def index
|
||
@object_result = Api::V1::Issues::ListService.call(@project, query_params, current_user)
|
||
@total_issues_count = @object_result[:total_issues_count]
|
||
@opened_issues_count = @object_result[:opened_issues_count]
|
||
@closed_issues_count = @object_result[:closed_issues_count]
|
||
if params[:only_name].present?
|
||
@issues = kaminary_select_paginate(
|
||
@object_result[:data].select(:id, :subject, :project_issues_index, :updated_on, :created_on))
|
||
else
|
||
@issues = kaminari_paginate(@object_result[:data])
|
||
end
|
||
render 'api/v1/issues/index'
|
||
end
|
||
|
||
def link_index
|
||
pm_issue_type = params[:pm_issue_type] || [1, 2, 3]
|
||
not_join_id = @issue.pm_links.pluck(:be_linkable_id)
|
||
not_join_id << @issue.id
|
||
object_issues = Issue.includes(:pm_links).where(
|
||
pm_project_id: params[:pm_project_id],
|
||
root_id: nil,
|
||
pm_issue_type: pm_issue_type
|
||
).where.not(id: not_join_id)
|
||
@issues = kaminari_paginate(object_issues)
|
||
render 'api/v1/issues/index'
|
||
end
|
||
|
||
|
||
def show
|
||
@issue.associate_attachment_container
|
||
render 'api/v1/issues/show'
|
||
end
|
||
|
||
def create
|
||
@object_result = Api::V1::Issues::CreateService.call(@project, issue_params, current_user)
|
||
render 'api/v1/issues/create'
|
||
end
|
||
|
||
def update
|
||
@object_result = Api::V1::Issues::UpdateService.call(@project, @issue, issue_params, current_user)
|
||
render 'api/v1/issues/update'
|
||
end
|
||
|
||
def batch_update
|
||
@object_result = Api::V1::Issues::BatchUpdateService.call(@project, @issues, batch_issue_params, current_user)
|
||
if @object_result
|
||
render_ok
|
||
else
|
||
render_error('批量更新疑修失败!')
|
||
end
|
||
end
|
||
|
||
def batch_destroy
|
||
@object_result = Api::V1::Issues::BatchDeleteService.call(@project, @issues, current_user)
|
||
if @object_result
|
||
render_ok
|
||
else
|
||
render_error('批量删除疑修失败!')
|
||
end
|
||
end
|
||
|
||
def priorities
|
||
@priorities = IssuePriority.order(position: :asc)
|
||
@priorities = @priorities.ransack(name_cont: params[:keyword]).result if params[:keyword]
|
||
@priorities = kaminary_select_paginate(@priorities)
|
||
render "api/v1/issues/issue_priorities/index"
|
||
end
|
||
|
||
def tags
|
||
IssueTag.pm_init_data(params[:pm_project_id]) unless $redis_cache.hget("pm_project_init_issue_tags", params[:pm_project_id])
|
||
@issue_tags = IssueTag.where(pm_project_id: params[:pm_project_id]).reorder("#{tag_sort_by} #{tag_sort_direction}")
|
||
@issue_tags = @issue_tags.ransack(name_cont: params[:keyword]).result if params[:keyword].present?
|
||
params[:only_name] = true #强制渲染 不走project
|
||
@issue_tags = kaminary_select_paginate(@issue_tags.select(:id, :name, :color))
|
||
render "api/v1/issues/issue_tags/index"
|
||
end
|
||
|
||
def statues
|
||
@statues = IssueStatus.order("position asc")
|
||
@statues = @statues.ransack(name_cont: params[:keyword]).result if params[:keyword].present?
|
||
@statues = kaminary_select_paginate(@statues)
|
||
render "api/v1/issues/statues/index"
|
||
end
|
||
|
||
|
||
|
||
def destroy
|
||
@object_result = Api::V1::Issues::DeleteService.call(@project, @issue, current_user)
|
||
if @object_result
|
||
render_ok
|
||
else
|
||
render_error('删除疑修失败!')
|
||
end
|
||
end
|
||
|
||
private
|
||
def check_issue_operate_permission
|
||
return if params[:project_id].to_i.zero?
|
||
render_forbidden('您没有操作权限!') unless @project.member?(current_user) || current_user.admin? || @issue.user == current_user
|
||
end
|
||
|
||
def load_issue
|
||
return render_parameter_missing if params[:pm_project_id].blank?
|
||
@issue = Issue.issue_issue.where(pm_project_id: params[:pm_project_id]).find_by_id(params[:id])
|
||
render_not_found('疑修不存在!') if @issue.blank?
|
||
end
|
||
|
||
def load_issues
|
||
return render_error('请输入正确的ID数组!') unless params[:ids].is_a?(Array)
|
||
params[:ids].each do |id|
|
||
@issue = Issue.find_by(id: id, pm_project_id: params[:pm_project_id])
|
||
if @issue.blank?
|
||
return render_not_found("ID为#{id}的疑修不存在!")
|
||
end
|
||
end
|
||
if params[:ids].blank?
|
||
@issues = Issue.where(pm_project_id: params[:pm_project_id])
|
||
else
|
||
@issues = Issue.where(id: params[:ids], pm_project_id: params[:pm_project_id])
|
||
end
|
||
end
|
||
|
||
|
||
def query_params
|
||
params.permit(
|
||
:only_name,
|
||
:category,
|
||
:participant_category,
|
||
:keyword, :author_id,
|
||
:milestone_id, :assigner_id,
|
||
:status_id,
|
||
:begin_date, :end_date,
|
||
:sort_by, :sort_direction, :root_id,
|
||
:issue_tag_ids, :pm_project_id, :pm_sprint_id, :pm_issue_type
|
||
)
|
||
end
|
||
|
||
|
||
|
||
def issue_params
|
||
params.permit(
|
||
:status_id, :priority_id, :milestone_id,
|
||
:branch_name, :start_date, :due_date, :time_scale,
|
||
:subject, :description, :blockchain_token_num,
|
||
:pm_project_id, :pm_sprint_id, :pm_issue_type, :root_id,
|
||
issue_tag_ids: [],
|
||
assigner_ids: [],
|
||
attachment_ids: [],
|
||
receivers_login: []
|
||
)
|
||
end
|
||
|
||
def batch_issue_params
|
||
params.permit(
|
||
:status_id, :priority_id, :milestone_id, :pm_sprint_id, :pm_issue_type, :root_id, :target_pm_project_id,
|
||
:issue_tag_ids => [],
|
||
:assigner_ids => [] )
|
||
end
|
||
|
||
def tag_sort_by
|
||
sort_by = params.fetch(:sort_by, "created_at")
|
||
sort_by = IssueTag.column_names.include?(sort_by) ? sort_by : "created_at"
|
||
sort_by
|
||
end
|
||
|
||
def tag_sort_direction
|
||
sort_direction = params.fetch(:sort_direction, "desc").downcase
|
||
sort_direction = %w(desc asc).include?(sort_direction) ? sort_direction : "desc"
|
||
sort_direction
|
||
end
|
||
|
||
end
|