pull/4953/head
Markus Kahl 8 years ago
parent 304806b322
commit 6023755e06
  1. 2
      lib/api/v3/relations/relations_api.rb
  2. 2
      lib/api/v3/relations/relations_helper.rb
  3. 35
      lib/api/v3/work_packages/available_relation_candidates_api.rb
  4. 67
      lib/api/v3/work_packages/available_relation_candidates_helper.rb
  5. 7
      lib/api/v3/work_packages/work_package_relations_api.rb

@ -34,7 +34,7 @@ module API
module V3 module V3
module Relations module Relations
class RelationsAPI < ::API::OpenProjectAPI class RelationsAPI < ::API::OpenProjectAPI
helpers RelationHelpers helpers ::API::V3::Relations::RelationsHelper
resources :relations do resources :relations do
get do get do

@ -29,7 +29,7 @@
module API module API
module V3 module V3
module Relations module Relations
module RelationHelpers module RelationsHelper
def filter_attributes(relation) def filter_attributes(relation)
relation relation
.attributes .attributes

@ -30,7 +30,7 @@ module API
module V3 module V3
module WorkPackages module WorkPackages
class AvailableRelationCandidatesAPI < ::API::OpenProjectAPI class AvailableRelationCandidatesAPI < ::API::OpenProjectAPI
include API::V3::Utilities::PathHelper helpers ::API::V3::WorkPackages::AvailableRelationCandidatesHelper
resources :available_relation_candidates do resources :available_relation_candidates do
params do params do
@ -40,7 +40,7 @@ module API
end end
get do get do
from = @work_package from = @work_package
query = work_package_query from, params[:type], params[:pageSize] query = work_package_query params[:query], from, params[:type], params[:pageSize]
work_packages = filter_work_packages query, from, params[:type] work_packages = filter_work_packages query, from, params[:type]
::API::V3::WorkPackages::WorkPackageListRepresenter.new( ::API::V3::WorkPackages::WorkPackageListRepresenter.new(
@ -50,37 +50,6 @@ module API
) )
end end
end end
private
##
# Queries the compatible work package's to the given one as much as possible through the
# database.
#
# @param from [WorkPackage] The work package in the `from` position of a relation.
# @param type [String] The type of relation (e.g. follows, blocks, etc.) to be checked.
# @param limit [Integer] Maximum number of results to retrieve.
def work_package_query(from, type, limit)
WorkPackage.where("id = ? OR subject LIKE ?", params[:query].to_i, "%#{params[:query]}%")
.where.not(id: from.id) # can't relate to itself
.limit(limit)
if Setting.cross_project_work_package_relations?
query
else
query.where(project_id: from.project_id) # has to be same project
end
end
def filter_work_packages(work_packages, from, type)
work_packages.reject { |to| illegal_relation? type, from, to }
end
def illegal_relation?(type, from, to)
rel = Relation.new(relation_type: params[:type], from: from, to: to)
rel.shared_hierarchy? || rel.circular_dependency?
end
end end
end end
end end

@ -0,0 +1,67 @@
#-- copyright
# OpenProject is a project management system.
# Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
module API
module V3
module WorkPackages
module AvailableRelationCandidatesHelper
include API::V3::Utilities::PathHelper
##
# Queries the compatible work package's to the given one as much as possible through the
# database.
#
# @param query [String] The ID or part of a subject to filter by
# @param from [WorkPackage] The work package in the `from` position of a relation.
# @param type [String] The type of relation (e.g. follows, blocks, etc.) to be checked.
# @param limit [Integer] Maximum number of results to retrieve.
def work_package_query(query, from, type, limit)
wps = WorkPackage.where("id = ? OR subject LIKE ?", query.to_i, "%#{query}%")
.where.not(id: from.id) # can't relate to itself
.limit(limit)
if Setting.cross_project_work_package_relations?
wps
else
wps.where(project_id: from.project_id) # has to be same project
end
end
def filter_work_packages(work_packages, from, type)
work_packages.reject { |to| illegal_relation? type, from, to }
end
def illegal_relation?(type, from, to)
rel = Relation.new(relation_type: type, from: from, to: to)
rel.shared_hierarchy? || rel.circular_dependency?
end
end
end
end
end

@ -30,16 +30,15 @@ module API
module V3 module V3
module WorkPackages module WorkPackages
class WorkPackageRelationsAPI < ::API::OpenProjectAPI class WorkPackageRelationsAPI < ::API::OpenProjectAPI
helpers ::API::V3::Relations::RelationHelpers helpers ::API::V3::Relations::RelationsHelper
resources :relations do resources :relations do
## ##
# @todo Redirect to relations endpoint as soon as `list relations` API endpoint # @todo Redirect to relations endpoint as soon as `list relations` API endpoint
# including filters is complete. # including filters is complete.
get do get do
relations = @work_package.relations.select do |relation| visible = ->(relation) { relation.other_work_package(@work_package).visible? }
relation.other_work_package(@work_package).visible? relations = @work_package.relations.select(&visible)
end
::API::V3::Relations::RelationCollectionRepresenter.new( ::API::V3::Relations::RelationCollectionRepresenter.new(
relations, relations,

Loading…
Cancel
Save