新增: 用户项目列表支持多个参数

This commit is contained in:
yystopf 2022-06-23 18:16:57 +08:00
parent fe58175ee0
commit 7cad953413
5 changed files with 95 additions and 2 deletions

View File

@ -10,4 +10,9 @@ class Api::V1::BaseController < ApplicationController
def current_user
User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
end
def require_manager_above
@project = load_project
return render_forbidden unless current_user.admin? && @project.manager?(current_user)
end
end

View File

@ -1,5 +1,5 @@
class Api::V1::Projects::WebhooksController < Api::V1::BaseController
before_action :load_project
before_action :require_manager_above
before_action :find_webhook, only: [:show, :update, :destroy, :tests, :hooktasks]
def index

View File

@ -2,6 +2,12 @@ class Api::V1::Users::ProjectsController < Api::V1::BaseController
before_action :load_observe_user
def index
@projects = kaminari_paginate(@observe_user.projects)
@object_results = Api::V1::Users::Projects::ListService.call(@observe_user, query_params, current_user)
@projects = kaminari_paginate(@object_results)
end
private
def query_params
params.permit(:category, :is_public, :project_type, :sort_by, :sort_direction, :search)
end
end

View File

@ -0,0 +1,79 @@
class Api::V1::Users::Projects::ListService < ApplicationService
include ActiveModel::Model
attr_reader :observe_user, :category, :is_public, :project_type, :sort_by, :sort_direction, :search, :current_user
attr_accessor :queried_projects
validates :category, inclusion: {in: %w(all join created watched forked), message: "请输入正确的Category"}
validates :is_public, inclusion: {in: [true, false], message: '请输入正确的IsPublic'}
validates :project_type, inclusion: {in: %w(common mirror sync_mirror), message: '请输入正确的ProjectType'}, allow_nil: true
validates :sort_by, inclusion: {in: Project.column_names, message: '请输入正确的SortBy'}
validates :sort_direction, inclusion: {in: %w(asc desc), message: '请输入正确的SortDirection'}
def initialize(observe_user, params, current_user=nil)
@observe_user = observe_user
@category = params[:category] || 'all'
@is_public = params[:is_public] || true
@project_type = params[:project_type]
@sort_by = params[:sort_by] || 'updated_on'
@sort_direction = params[:sort_direction] || 'desc'
@search = params[:search]
@current_user = current_user
end
def call
raise Error, errors.full_messages.join(", ") unless valid?
# begin
project_query_data
queried_projects
# rescue
# raise Error, "服务器错误,请联系系统管理员!"
# end
end
private
def project_query_data
if current_user.admin?
projects = Project
else
projects = Project.visible
end
case category
when 'join'
normal_projects = projects.where.not(user_id: observe_user.id).members_projects(observe_user.id).to_sql
org_projects = projects.joins(team_projects: [team: :team_users]).where(team_users: {user_id: observe_user.id}).to_sql
projects = Project.from("( #{normal_projects} UNION #{org_projects} ) AS projects").distinct
when 'created'
projects = projects.where(user_id: observe_user.id)
when 'watched'
projects = projects.where.not(user_id: observe_user.id).joins(:watchers).where(watchers: {watchable_type: "Project", user_id: observe_user.id})
when 'forked'
fork_ids = observe_user.fork_users.select(:id, :fork_project_id).pluck(:fork_project_id)
projects = projects.where(id: fork_ids)
else
normal_projects = projects.members_projects(observe_user.id).to_sql
org_projects = projects.joins(team_projects: [team: :team_users]).where(team_users: {user_id: observe_user.id}).to_sql
projects = Project.from("( #{ normal_projects} UNION #{ org_projects } ) AS projects").distinct
end
if is_public
projects = projects.visible
else
projects = projects.is_private
end
projects = projects.with_project_type(project_type)
q = projects.ransack(name_or_identifier_cont: search)
scope = q.result.includes(:project_category, :project_language,:owner, :repository, :has_pinned_users)
scope = scope.order("projects.#{sort_by} #{sort_direction}")
@queried_projects = scope
end
end

View File

@ -1,5 +1,8 @@
json.total_count @projects.total_count
json.projects @projects do |project|
json.owner do
json.partial! "api/v1/users/simple_user", user: project.owner
end
json.partial! "api/v1/projects/simple_detail", project: project
end