From 5fafd8195c6eeda07a30e3e25841f3d06271986a Mon Sep 17 00:00:00 2001 From: yystopf Date: Thu, 4 Jan 2024 14:25:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E6=A0=87=E8=AE=B0=E5=85=B3=E8=81=94=E7=BB=84=E7=BB=87=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/pm/issue_tags_controller.rb | 67 +++++++++++++++++++ app/models/issue_tag.rb | 5 ++ config/routes/api.rb | 2 +- ...53819_add_organization_id_to_issue_tags.rb | 5 ++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/pm/issue_tags_controller.rb create mode 100644 db/migrate/20240104053819_add_organization_id_to_issue_tags.rb diff --git a/app/controllers/api/pm/issue_tags_controller.rb b/app/controllers/api/pm/issue_tags_controller.rb new file mode 100644 index 000000000..3962b76d4 --- /dev/null +++ b/app/controllers/api/pm/issue_tags_controller.rb @@ -0,0 +1,67 @@ +class Api::Pm::IssueTagsController < Api::Pm::BaseController + + def index + @issue_tags = IssueTag.pm_able + @issue_tags = @issue_tags.where(organization_id: params[:organization_id]) if params[:organization_id].present? + @issue_tags = @issue_tags.where(pm_project_id: params[:pm_project_id]) if params[:pm_project_id].present? + @issue_tags = @issue_tags.ransack(name_cont: params[:keyword]).result if params[:keyword].present? + @issue_tags = @issue_tags.reorder("#{tag_sort_by} #{tag_sort_direction}") + @issue_tags = kaminari_paginate(@issue_tags) + render "api/v1/issues/issue_tags/index" + end + + def create + return render_error("请输入正确的OrganizationID") unless Organization.exists?(id: issue_tag_create_params[:organization_id]) + @issue_tag = IssueTag.new(issue_tag_create_params.merge!(project_id: 0)) + if @issue_tag.save! + render_ok + else + render_error("创建标记失败!") + end + end + + before_action :load_issue_tag, only: [:update, :destroy] + + def update + @issue_tag.attributes = issue_tag_update_params + if @issue_tag.save! + render_ok + else + render_error("更新标记失败!") + end + end + + def destroy + if @issue_tag.destroy! + render_ok + else + render_error("删除标记失败!") + end + end + + + private + 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 + + def issue_tag_create_params + params.permit(:name, :description, :color, :pm_project_id, :organization_id) + end + + def issue_tag_update_params + params.permit(:name, :description, :color) + end + + def load_issue_tag + @issue_tag = IssueTag.pm_able.find_by_id(params[:id]) + end +end \ No newline at end of file diff --git a/app/models/issue_tag.rb b/app/models/issue_tag.rb index e8ffa4c0a..3c88c4ef1 100644 --- a/app/models/issue_tag.rb +++ b/app/models/issue_tag.rb @@ -15,9 +15,11 @@ # gitea_url :string(255) # pull_requests_count :integer default("0") # pm_project_id :integer +# organization_id :integer # # Indexes # +# index_issue_tags_on_organization_id (organization_id) # index_issue_tags_on_user_id_and_name_and_project_id (user_id,name,project_id) # @@ -29,6 +31,9 @@ class IssueTag < ApplicationRecord has_many :pull_request_issues, -> {where(issue_classify: "pull_request")}, source: :issue, through: :issue_tags_relates belongs_to :project, optional: true, counter_cache: true belongs_to :user, optional: true + belongs_to :organization, optional: true + + scope :pm_able, -> {where(project_id: 0)} validates :name, uniqueness: {scope: :project_id, message: "已存在" }, if: :pm_project? diff --git a/config/routes/api.rb b/config/routes/api.rb index 77abee455..50c6a9538 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -13,7 +13,6 @@ defaults format: :json do get :link_index get :parent_issues end - resources :issue_links resources :journals do @@ -22,6 +21,7 @@ defaults format: :json do end end end + resources :issue_tags resources :sprint_issues, only: [:index] do collection do get :statistics diff --git a/db/migrate/20240104053819_add_organization_id_to_issue_tags.rb b/db/migrate/20240104053819_add_organization_id_to_issue_tags.rb new file mode 100644 index 000000000..04cc02d0e --- /dev/null +++ b/db/migrate/20240104053819_add_organization_id_to_issue_tags.rb @@ -0,0 +1,5 @@ +class AddOrganizationIdToIssueTags < ActiveRecord::Migration[5.2] + def change + add_reference :issue_tags, :organization + end +end