Commit 1a49ef52 authored by Dmytro Zaporozhets's avatar Dmytro Zaporozhets

Merge branch 'req-rename-service' into 'master'

Move requirement services and finder into requirement_management

See merge request gitlab-org/gitlab!30167
parents b34f5113 6d15345d
# frozen_string_literal: true
class RequirementsFinder
include Gitlab::Utils::StrongMemoize
# Params:
# project_id: integer
# iids: integer[]
# state: string[]
# sort: string
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
items = init_collection
items = by_state(items)
items = by_iid(items)
sort(items)
end
private
attr_reader :current_user, :params
def init_collection
return RequirementsManagement::Requirement.none unless Ability.allowed?(current_user, :read_requirement, project)
project.requirements
end
def by_iid(items)
return items unless params[:iids].present?
items.for_iid(params[:iids])
end
def by_state(items)
return items unless params[:state].present?
items.for_state(params[:state])
end
def project
strong_memoize(:project) do
::Project.find_by_id(params[:project_id]) if params[:project_id].present?
end
end
def sort(items)
sorts = RequirementsManagement::Requirement.simple_sorts.keys
sort = sorts.include?(params[:sort]) ? params[:sort] : 'id_desc'
items.order_by(sort)
end
end
# frozen_string_literal: true
module RequirementsManagement
class RequirementsFinder
include Gitlab::Utils::StrongMemoize
# Params:
# project_id: integer
# iids: integer[]
# state: string[]
# sort: string
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute
items = init_collection
items = by_state(items)
items = by_iid(items)
sort(items)
end
private
attr_reader :current_user, :params
def init_collection
return RequirementsManagement::Requirement.none unless Ability.allowed?(current_user, :read_requirement, project)
project.requirements
end
def by_iid(items)
return items unless params[:iids].present?
items.for_iid(params[:iids])
end
def by_state(items)
return items unless params[:state].present?
items.for_state(params[:state])
end
def project
strong_memoize(:project) do
::Project.find_by_id(params[:project_id]) if params[:project_id].present?
end
end
def sort(items)
sorts = RequirementsManagement::Requirement.simple_sorts.keys
sort = sorts.include?(params[:sort]) ? params[:sort] : 'id_desc'
items.order_by(sort)
end
end
end
......@@ -26,7 +26,7 @@ module Mutations
project = authorized_find!(full_path: project_path)
validate_flag!(project)
requirement = ::Requirements::CreateService.new(
requirement = ::RequirementsManagement::CreateRequirementService.new(
project,
context[:current_user],
args
......
......@@ -43,7 +43,7 @@ module Mutations
requirement_iid = args.delete(:iid)
requirement = authorized_find!(project_path: project_path, iid: requirement_iid)
requirement = ::Requirements::UpdateService.new(
requirement = ::RequirementsManagement::UpdateRequirementService.new(
requirement.project,
context[:current_user],
args
......
......@@ -31,7 +31,7 @@ module Resolvers
args[:project_id] = project.id
args[:iids] ||= [args[:iid]].compact
RequirementsFinder.new(context[:current_user], args).execute
RequirementsManagement::RequirementsFinder.new(context[:current_user], args).execute
end
end
end
# frozen_string_literal: true
module Requirements
class CreateService < BaseService
module RequirementsManagement
class CreateRequirementService < BaseService
include Gitlab::Allowable
def execute
......
# frozen_string_literal: true
module Requirements
class UpdateService < BaseService
module RequirementsManagement
class UpdateRequirementService < BaseService
def execute(requirement)
raise Gitlab::Access::AccessDeniedError unless can?(current_user, :update_requirement, project)
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe RequirementsFinder do
describe RequirementsManagement::RequirementsFinder do
let_it_be(:project) { create(:project) }
let_it_be(:project_user) { create(:user).tap { |u| project.add_developer(u) } }
let_it_be(:requirement1) { create(:requirement, project: project, state: 'opened', updated_at: 3.days.ago) }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe Requirements::CreateService do
describe RequirementsManagement::CreateRequirementService do
let_it_be(:project) { create(:project)}
let_it_be(:user) { create(:user) }
let_it_be(:other_user) { create(:user) }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe Requirements::UpdateService do
describe RequirementsManagement::UpdateRequirementService do
let_it_be(:project) { create(:project)}
let_it_be(:user) { create(:user) }
let_it_be(:requirement) { create(:requirement, project: project) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment