forgeplus/app/controllers/projects_controller.rb

87 lines
2.6 KiB
Ruby
Raw Normal View History

2020-03-09 00:40:16 +08:00
class ProjectsController < ApplicationController
include ApplicationHelper
include OperateProjectAbilityAble
before_action :require_login, except: %i[index branches group_type_list]
before_action :find_project_with_id, only: %i[show branches update destroy]
before_action :authorizate_user_can_edit_project!, only: %i[update]
def index
scope = Projects::ListQuery.call(params)
@total_count = scope.size
@projects = paginate(scope)
end
def create
ActiveRecord::Base.transaction do
Projects::CreateForm.new(project_params).validate!
@project = Projects::CreateService.new(current_user, project_params).call
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def migrate
ActiveRecord::Base.transaction do
Projects::MigrateForm.new(mirror_params).validate!
@project = Projects::MigrateService.new(current_user, mirror_params).call
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def branches
@branches = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call
end
def group_type_list
@project_group_list = Project.visible.group(:project_type).select('project_type, count(*) AS projects_count')
end
def update
ActiveRecord::Base.transaction do
# Projects::CreateForm.new(project_params).validate!
private = params[:private]
if [true, false].include? private
new_project_params = project_params.merge(is_public: !private)
Gitea::Repository::UpdateService.new(@project.owner, @project.repository.identifier, {private: private}).call
@project.repository.update_column(:hidden, private)
end
@project.update_attributes!(new_project_params)
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
def show
end
def destroy
if current_user.admin? || @project.owner?(current_user)
ActiveRecord::Base.transaction do
Gitea::Repository::DeleteService.new(@project.owner, @project.identifier).call
@project.destroy!
render_ok
end
else
render_forbidden('你没有权限操作')
end
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
end
private
def project_params
params.permit(:user_id, :name, :description, :repository_name,
:project_category_id, :project_language_id, :license_id, :ignore_id)
end
def mirror_params
params.permit(:user_id, :name, :description, :repository_name,
:project_category_id, :project_language_id, :clone_addr, :private)
end
end