Commit 6d15345d authored by Jan Provaznik's avatar Jan Provaznik Committed by Dmytro Zaporozhets

Renamed requirement service

* moved under requirement_management
parent 78b68298
# 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 ...@@ -26,7 +26,7 @@ module Mutations
project = authorized_find!(full_path: project_path) project = authorized_find!(full_path: project_path)
validate_flag!(project) validate_flag!(project)
requirement = ::Requirements::CreateService.new( requirement = ::RequirementsManagement::CreateRequirementService.new(
project, project,
context[:current_user], context[:current_user],
args args
......
...@@ -43,7 +43,7 @@ module Mutations ...@@ -43,7 +43,7 @@ module Mutations
requirement_iid = args.delete(:iid) requirement_iid = args.delete(:iid)
requirement = authorized_find!(project_path: project_path, iid: requirement_iid) requirement = authorized_find!(project_path: project_path, iid: requirement_iid)
requirement = ::Requirements::UpdateService.new( requirement = ::RequirementsManagement::UpdateRequirementService.new(
requirement.project, requirement.project,
context[:current_user], context[:current_user],
args args
......
...@@ -31,7 +31,7 @@ module Resolvers ...@@ -31,7 +31,7 @@ module Resolvers
args[:project_id] = project.id args[:project_id] = project.id
args[:iids] ||= [args[:iid]].compact args[:iids] ||= [args[:iid]].compact
RequirementsFinder.new(context[:current_user], args).execute RequirementsManagement::RequirementsFinder.new(context[:current_user], args).execute
end end
end end
end end
# frozen_string_literal: true # frozen_string_literal: true
module Requirements module RequirementsManagement
class CreateService < BaseService class CreateRequirementService < BaseService
include Gitlab::Allowable include Gitlab::Allowable
def execute def execute
......
# frozen_string_literal: true # frozen_string_literal: true
module Requirements module RequirementsManagement
class UpdateService < BaseService class UpdateRequirementService < BaseService
def execute(requirement) def execute(requirement)
raise Gitlab::Access::AccessDeniedError unless can?(current_user, :update_requirement, project) raise Gitlab::Access::AccessDeniedError unless can?(current_user, :update_requirement, project)
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe RequirementsFinder do describe RequirementsManagement::RequirementsFinder do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:project_user) { create(:user).tap { |u| project.add_developer(u) } } 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) } let_it_be(:requirement1) { create(:requirement, project: project, state: 'opened', updated_at: 3.days.ago) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe Requirements::CreateService do describe RequirementsManagement::CreateRequirementService do
let_it_be(:project) { create(:project)} let_it_be(:project) { create(:project)}
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:other_user) { create(:user) } let_it_be(:other_user) { create(:user) }
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
require 'spec_helper' require 'spec_helper'
describe Requirements::UpdateService do describe RequirementsManagement::UpdateRequirementService do
let_it_be(:project) { create(:project)} let_it_be(:project) { create(:project)}
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:requirement) { create(:requirement, project: project) } 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