新增:统计项目分类下私有项目的数量

This commit is contained in:
yystopf 2022-11-15 14:25:35 +08:00
parent 49abadfa3f
commit 64c6f69f56
5 changed files with 37 additions and 9 deletions

View File

@ -154,6 +154,15 @@ class ProjectsController < ApplicationController
}
gitea_repo = Gitea::Repository::UpdateService.call(@owner, @project&.repository&.identifier, gitea_params)
@project.repository.update_attributes({hidden: gitea_repo["private"], identifier: gitea_repo["name"]})
# 更新对应所属分类下的项目数量(私有)
before_is_public = @project.previous_changes[:is_public].present? ? @project.previous_changes[:is_public][0] : @project.is_public
after_is_public = @project.previous_changes[:is_public].present? ? @project.previous_changes[:is_public][1] : @project.is_public
before_pc_id = @project.previous_changes[:project_category_id].present? ? @project.previous_changes[:project_category_id][0] : @project.project_category_id
after_pc_id = @project.previous_changes[:project_category_id].present? ? @project.previous_changes[:project_category_id][1] : @project.project_category_id
before_pc = ProjectCategory.find_by_id(before_pc_id)
after_pc = ProjectCategory.find_by_id(after_pc_id)
before_pc.decrement!(:private_projects_count, 1) if before_pc.present? && !before_is_public
after_pc.increment!(:private_projects_count, 1) if after_pc.present? && !after_is_public
end
SendTemplateMessageJob.perform_later('ProjectSettingChanged', current_user.id, @project&.id, @project.previous_changes.slice(:name, :description, :project_category_id, :project_language_id, :is_public, :identifier)) if Site.has_notice_menu?
end
@ -171,6 +180,8 @@ class ProjectsController < ApplicationController
Gitea::Repository::DeleteService.new(@project.owner, @project.identifier).call
@project.destroy!
@project.forked_projects.update_all(forked_from_project_id: nil)
# 如果该项目有所属的项目分类以及为私有项目,需要更新对应数量
@project.project_category.decrement!(:private_projects_count, 1) if @project.project_category.present? && !@project.is_public
render_ok
end
else

View File

@ -2,14 +2,15 @@
#
# Table name: project_categories
#
# id :integer not null, primary key
# name :string(255)
# position :integer
# projects_count :integer default("0")
# created_at :datetime not null
# updated_at :datetime not null
# ancestry :string(255)
# pinned_index :integer default("0")
# id :integer not null, primary key
# name :string(255)
# position :integer
# projects_count :integer default("0")
# created_at :datetime not null
# updated_at :datetime not null
# ancestry :string(255)
# pinned_index :integer default("0")
# private_projects_count :integer default("0")
#
# Indexes
#

View File

@ -16,6 +16,7 @@ class Projects::CreateService < ApplicationService
Project.update_common_projects_count!
ProjectUnit.init_types(@project.id)
Repositories::CreateService.new(user, @project, repository_params).call
upgrade_project_category_private_projects_count
else
Rails.logger.info("#############___________create_project_erros______###########{@project.errors.messages}")
end
@ -28,6 +29,14 @@ class Projects::CreateService < ApplicationService
private
def upgrade_project_category_private_projects_count
# 如果为空或者项目为公有项目直接返回
return unless params[:project_category_id].present?
return if repo_is_public
project_category = ProjectCategory.find_by_id(params[:project_category_id])
project_category.increment!(:private_projects_count, 1)
end
def authroize_user_id_success
(user.id == params[:user_id].to_i) || (user.organizations.find_by_id(params[:user_id]).present?)
end

View File

@ -4,7 +4,8 @@
<th width="5%">序号</th>
<th width="30%">名称</th>
<th width="20%"><%= sort_tag('精选', name: 'pinned_index', path: admins_project_categories_path) %></th>
<th width="20%"><%= sort_tag('项目数', name: 'projects_count', path: admins_project_categories_path) %></th>
<th width="10%"><%= sort_tag('项目数', name: 'projects_count', path: admins_project_categories_path) %></th>
<th width="10%"><%= sort_tag('私有项目数', name: 'private_projects_count', path: admins_project_categories_path) %></th>
<th width="20%">精选项目数</th>
<th width="20%"><%= sort_tag('创建时间', name: 'created_at', path: admins_project_categories_path) %></th>
<th width="25%">操作</th>
@ -20,6 +21,7 @@
</td>
<td><%= project_category.pinned_index == 0 ? "" : "√" %></td>
<td><%= project_category.projects_count %></td>
<td><%= project_category.private_projects_count %></td>
<td><%= project_category.projects.select(:id).where(is_pinned: true).size %></td>
<td><%= project_category.created_at&.strftime('%Y-%m-%d %H:%M') %></td>
<td class="action-container">

View File

@ -0,0 +1,5 @@
class AddPrivateProjectsCountToProjectCategory < ActiveRecord::Migration[5.2]
def change
add_column :project_categories, :private_projects_count, :integer, default: 0
end
end