forgeplus/app/controllers/api/v1/issues_controller.rb

133 lines
4.3 KiB
Ruby
Raw Permalink Normal View History

2023-02-08 16:56:01 +08:00
class Api::V1::IssuesController < Api::V1::BaseController
before_action :require_login, except: [:index, :show, :test_cases]
before_action :require_public_and_member_above, only: [:index, :show, :create, :update, :destroy, :test_cases]
2023-02-20 16:47:19 +08:00
before_action :require_operate_above, only: [:batch_update, :batch_destroy]
2023-02-08 16:56:01 +08:00
def index
2023-02-22 14:42:52 +08:00
IssueTag.init_data(@project.id) unless $redis_cache.hget("project_init_issue_tags", @project.id)
@object_result = Api::V1::Issues::ListService.call(@project, query_params, current_user)
2023-02-22 14:42:52 +08:00
@total_issues_count = @object_result[:total_issues_count]
@opened_issues_count = @object_result[:opened_issues_count]
@closed_issues_count = @object_result[:closed_issues_count]
2023-03-03 18:43:18 +08:00
if params[:only_name].present?
2023-04-12 16:12:52 +08:00
@issues = kaminary_select_paginate(@object_result[:data].select(:id, :subject, :project_issues_index, :updated_on, :created_on))
2023-03-03 18:43:18 +08:00
else
@issues = kaminari_paginate(@object_result[:data])
end
2023-02-08 16:56:01 +08:00
end
def test_cases
test_cases = @project.issues.where("test_case is not null")
test_cases = test_cases.where('test_case LIKE ?', "%#{params[:keyword]}%") if params[:keyword].present?
@test_cases = kaminary_select_paginate(test_cases.select(:test_case).distinct)
render_ok(count: @test_cases.total_count, data: @test_cases.as_json(only: %i[id test_case]))
end
def create
@object_result = Api::V1::Issues::CreateService.call(@project, issue_params, current_user)
end
before_action :load_issue, only: [:show, :update, :destroy]
2023-02-24 10:24:57 +08:00
before_action :check_issue_operate_permission, only: [:update, :destroy]
def show
2023-02-20 16:47:19 +08:00
@user_permission = current_user.present? && current_user.logged? && (@project.member?(current_user) || current_user.admin? || @issue.user == current_user)
2024-08-15 16:04:35 +08:00
# @ratings =
# if @project.manager?(current_user)
# @issue.ratings
# else
# @issue.ratings.where(rater_id: current_user)
# end
end
def update
2023-02-14 23:19:17 +08:00
@object_result = Api::V1::Issues::UpdateService.call(@project, @issue, issue_params, current_user)
end
def destroy
@object_result = Api::V1::Issues::DeleteService.call(@project, @issue, current_user)
if @object_result
render_ok
else
render_error("删除疑修失败!")
end
2023-02-14 23:19:17 +08:00
end
before_action :load_issues, only: [:batch_update, :batch_destroy]
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
2023-02-14 23:19:17 +08:00
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
private
def load_issue
@issue = @project.issues.issue_issue.where(project_issues_index: params[:index]).where.not(id: params[:index]).take || Issue.find_by_id(params[:index])
if @issue.blank?
render_not_found("疑修不存在!")
end
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)
if @issue.blank?
return render_not_found("ID为#{id}的疑修不存在!")
end
end
@issues = Issue.where(id: params[:ids])
end
2023-02-20 16:47:19 +08:00
def check_issue_operate_permission
2023-02-22 09:55:05 +08:00
return render_forbidden("您没有操作权限!") unless @project.member?(current_user) || current_user.admin? || @issue.user == current_user
end
2023-02-08 16:56:01 +08:00
def query_params
params.permit(
2023-08-29 11:35:46 +08:00
:only_name,
2023-02-08 16:56:01 +08:00
:category,
:participant_category,
:keyword, :author_id,
:milestone_id, :assigner_id,
:status_id,
:test_case,
:begin_date, :end_date,
2023-02-08 16:56:01 +08:00
:sort_by, :sort_direction,
2023-02-22 15:56:44 +08:00
:issue_tag_ids)
2023-02-08 16:56:01 +08:00
end
def issue_params
params.permit(
:status_id, :priority_id, :milestone_id,
:branch_name, :start_date, :due_date,
:subject, :description,
:test_case,
:issue_tag_ids => [],
:assigner_ids => [],
:attachment_ids => [],
:receivers_login => [])
end
2023-02-14 23:19:17 +08:00
def batch_issue_params
params.permit(
:status_id, :priority_id, :milestone_id,
:issue_tag_ids => [],
:assigner_ids => [])
end
2023-02-08 16:56:01 +08:00
end