Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

extend default endpoints to bcf api

ulfertsdc9c8faf3c58
diff --git a/app/services/api/v3/parse_resource_params_service.rb b/app/services/api/v3/parse_resource_params_service.rb
index f9ea957d17d..16fee24c91a 100644
--- a/app/services/api/v3/parse_resource_params_service.rb
+++ b/app/services/api/v3/parse_resource_params_service.rb
@@ -32,7 +32,7 @@ module API
private
def deduce_representer(model)
- "API::V3::#{model.to_s.pluralize}::#{model}Representer".constantize
+ "API::V3::#{model.to_s.pluralize}::#{model}PayloadRepresenter".constantize
end
def parsing_representer
diff --git a/lib/api/utilities/endpoints/bodied.rb b/lib/api/utilities/endpoints/bodied.rb
new file mode 100644
index 00000000000..839077ea61b
--- /dev/null
+++ b/lib/api/utilities/endpoints/bodied.rb
@@ -0,0 +1,173 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Utilities
+ module Endpoints
+ class Bodied
+ def default_instance_generator(_model)
+ raise NotImplementedError
+ end
+
+ def default_params_modifier
+ ->(params) do
+ params
+ end
+ end
+
+ def initialize(model:,
+ api_name: model.name.demodulize,
+ instance_generator: default_instance_generator(model),
+ params_modifier: default_params_modifier,
+ process_service: nil,
+ parse_service: nil)
+ self.model = model
+ self.api_name = api_name
+ self.instance_generator = instance_generator
+ self.params_modifier = params_modifier
+ self.parse_representer = deduce_parse_representer
+ self.render_representer = deduce_render_representer
+ self.process_contract = deduce_process_contract
+ self.process_service = process_service || deduce_process_service
+ self.parse_service = parse_service || deduce_parse_service
+ end
+
+ def mount
+ update = self
+
+ -> do
+ params = update.parse(current_user, request_body)
+
+ params = instance_exec(params, &update.params_modifier)
+
+ call = update.process(current_user,
+ instance_exec(params, &update.instance_generator),
+ params)
+
+ update.render(current_user, call) do
+ status update.success_status
+ end
+ end
+ end
+
+ def parse(current_user, request_body)
+ parse_service
+ .new(current_user,
+ model: model,
+ representer: parse_representer)
+ .call(request_body)
+ .result
+ end
+
+ def process(current_user, instance, params)
+ args = { user: current_user,
+ model: instance,
+ contract_class: process_contract }
+
+ process_service
+ .new(args.compact)
+ .call(params)
+ end
+
+ def render(current_user, call)
+ if success?(call)
+ yield
+ present_success(current_user, call)
+ else
+ present_error(call)
+ end
+ end
+
+ def success_status
+ :ok
+ end
+
+ attr_accessor :model,
+ :api_name,
+ :instance_generator,
+ :parse_representer,
+ :render_representer,
+ :params_modifier,
+ :process_contract,
+ :process_service,
+ :parse_service
+
+ private
+
+ def present_success(_current_user, _call)
+ raise NotImplementedError
+ end
+
+ def present_error(_call)
+ raise NotImplementedError
+ end
+
+ def success?(call)
+ call.success?
+ end
+
+ def deduce_process_service
+ "::#{deduce_backend_namespace}::SetAttributesService".constantize
+ end
+
+ def deduce_process_contract
+ "::#{deduce_backend_namespace}::#{update_or_create}Contract".constantize
+ end
+
+ def deduce_parse_representer
+ raise NotImplementedError
+ #"::API::V3::#{deduce_api_namespace}::#{api_name}PayloadRepresenter".constantize
+ end
+
+ def deduce_parse_service
+ raise NotImplementedError
+ end
+
+ def deduce_render_representer
+ raise NotImplementedError
+ end
+
+ def deduce_api_namespace
+ api_name.pluralize
+ end
+
+ def backend_name
+ model.name.demodulize
+ end
+
+ def deduce_backend_namespace
+ backend_name.pluralize
+ end
+
+ def update_or_create
+ raise NotImplementedError
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/utilities/endpoints/create.rb b/lib/api/utilities/endpoints/create.rb
new file mode 100644
index 00000000000..6c3e9673013
--- /dev/null
+++ b/lib/api/utilities/endpoints/create.rb
@@ -0,0 +1,50 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Utilities
+ module Endpoints
+ class Create < Modify
+ def default_instance_generator(_model)
+ ->(_params) do
+ end
+ end
+
+ def success_status
+ :created
+ end
+
+ private
+
+ def update_or_create
+ "Create"
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/utilities/endpoints/delete.rb b/lib/api/utilities/endpoints/delete.rb
new file mode 100644
index 00000000000..4fc3243b422
--- /dev/null
+++ b/lib/api/utilities/endpoints/delete.rb
@@ -0,0 +1,107 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Utilities
+ module Endpoints
+ class Delete
+ def default_instance_generator(model)
+ ->(_params) do
+ instance_variable_get("@#{model.name.demodulize.underscore}")
+ end
+ end
+
+ def initialize(model:,
+ instance_generator: default_instance_generator(model),
+ process_service: nil)
+ self.model = model
+ self.instance_generator = instance_generator
+ self.process_service = process_service || deduce_process_service
+ end
+
+ def mount
+ delete = self
+
+ -> do
+ call = delete.process(current_user,
+ instance_exec(params, &delete.instance_generator))
+
+ delete.render(call) do
+ status delete.success_status
+ end
+ end
+ end
+
+ def process(current_user, instance)
+ process_service
+ .new(user: current_user,
+ model: instance)
+ .call
+ end
+
+ def render(call)
+ if success?(call)
+ yield
+ else
+ present_error(call)
+ end
+ end
+
+ def success_status
+ 204
+ end
+
+ attr_accessor :model,
+ :instance_generator,
+ :process_service
+
+ private
+
+ def present_error(call)
+ fail ::API::Errors::ErrorBase.create_and_merge_errors(call.errors)
+ end
+
+ def success?(call)
+ call.success?
+ end
+
+ def deduce_process_service
+ "::#{deduce_namespace}::DeleteService".constantize
+ end
+
+ def deduce_namespace
+ demodulized_name.pluralize
+ end
+
+ def demodulized_name
+ model.name.demodulize
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/utilities/endpoints/index.rb b/lib/api/utilities/endpoints/index.rb
new file mode 100644
index 00000000000..1acbb3f1544
--- /dev/null
+++ b/lib/api/utilities/endpoints/index.rb
@@ -0,0 +1,151 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Utilities
+ module Endpoints
+ class Index
+ include ::API::Utilities::PageSizeHelper
+
+ def initialize(model:,
+ api_name: model.name.demodulize,
+ scope: nil,
+ render_representer: nil)
+ self.model = model_class(model)
+ self.scope = scope
+ self.api_name = api_name
+ self.render_representer = render_representer || deduce_render_representer
+ end
+
+ def mount
+ index = self
+
+ -> do
+ query = index.parse(params)
+
+ self_path = api_v3_paths.send(index.self_path)
+ base_scope = index.scope ? instance_exec(&index.scope) : index.model
+
+ index.render(query, params, self_path, base_scope)
+ end
+ end
+
+ def parse(params)
+ ParamsToQueryService
+ .new(model, User.current)
+ .call(params)
+ end
+
+ def render(query, params, self_path, base_scope)
+ if query.valid?
+ render_success(query, params, self_path, base_scope)
+ else
+ render_error(query)
+ end
+ end
+
+ def self_path
+ api_name.underscore.pluralize
+ end
+
+ attr_accessor :model,
+ :api_name,
+ :scope,
+ :render_representer
+
+ private
+
+ def render_success(query, params, self_path, base_scope)
+ results = merge_scopes(base_scope, query.results)
+
+ if paginated_representer?
+ render_paginated_success(results, params, self_path)
+ else
+ render_unpaginated_success(results, self_path)
+ end
+ end
+
+ def render_paginated_success(results, params, self_path)
+ render_representer
+ .new(results,
+ self_path,
+ page: to_i_or_nil(params[:offset]),
+ per_page: resolve_page_size(params[:pageSize]),
+ current_user: User.current)
+ end
+
+ def render_unpaginated_success(results, self_path)
+ render_representer
+ .new(results,
+ self_path,
+ current_user: User.current)
+ end
+
+ def paginated_representer?
+ render_representer.ancestors.include?(::API::Decorators::OffsetPaginatedCollection)
+ end
+
+ def render_error(query)
+ raise ::API::Errors::InvalidQuery.new(query.errors.full_messages)
+ end
+
+ def deduce_render_representer
+ "::API::V3::#{deduce_namespace}::#{api_name}CollectionRepresenter".constantize
+ end
+
+ def deduce_namespace
+ api_name.pluralize
+ end
+
+ def model_class(scope)
+ if scope.is_a? Class
+ scope
+ else
+ scope.model
+ end
+ end
+
+ def with_based_scope(other_scope)
+ if scope
+ merge_scopes(scope, other_scope)
+ else
+ other_scope
+ end
+ end
+
+ def merge_scopes(scope_a, scope_b)
+ if scope_a.is_a? Class
+ scope_b
+ else
+ scope_a.merge(scope_b)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/utilities/endpoints/modify.rb b/lib/api/utilities/endpoints/modify.rb
new file mode 100644
index 00000000000..4eea40f9d2a
--- /dev/null
+++ b/lib/api/utilities/endpoints/modify.rb
@@ -0,0 +1,92 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Utilities
+ module Endpoints
+ class Modify < Bodied
+ def default_instance_generator(model)
+ ->(_params, _current_user) do
+ instance_variable_get("@#{model.name.demodulize.underscore}")
+ end
+ end
+
+ private
+
+ def present_success(current_user, call)
+ raise NotImplementedError
+ end
+
+ def present_error(call)
+ errors = call.errors
+ errors = merge_dependent_errors call if errors.empty?
+
+ api_errors = [::API::Errors::ErrorBase.create_and_merge_errors(errors)]
+
+ fail ::API::Errors::MultipleErrors.create_if_many(api_errors)
+ end
+
+ def merge_dependent_errors(call)
+ errors = ActiveModel::Errors.new call.result
+
+ call.dependent_results.each do |dr|
+ dr.errors.keys.each do |field|
+ dr.errors.symbols_and_messages_for(field).each do |symbol, full_message, _|
+ errors.add :base, symbol, message: dependent_error_message(dr.result, full_message)
+ end
+ end
+ end
+
+ errors
+ end
+
+ def dependent_error_message(result, full_message)
+ I18n.t(
+ :error_in_dependent,
+ dependent_class: result.model_name.human,
+ related_id: result.id,
+ related_subject: result.name,
+ error: full_message
+ )
+ end
+
+ def deduce_process_service
+ "::#{deduce_backend_namespace}::#{update_or_create}Service".constantize
+ end
+
+ #def deduce_render_representer
+ # "::API::V3::#{deduce_api_namespace}::#{api_name}Representer".constantize
+ #end
+
+ def deduce_process_contract
+ nil
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/utilities/endpoints/show.rb b/lib/api/utilities/endpoints/show.rb
new file mode 100644
index 00000000000..ecb26491532
--- /dev/null
+++ b/lib/api/utilities/endpoints/show.rb
@@ -0,0 +1,86 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Utilities
+ module Endpoints
+ class Show
+ def default_instance_generator(model)
+ ->(_params) do
+ instance_variable_get("@#{model.name.demodulize.underscore}")
+ end
+ end
+
+ def initialize(model:,
+ api_name: model.name.demodulize,
+ render_representer: nil,
+ instance_generator: default_instance_generator(model))
+
+ self.model = model
+ self.api_name = api_name
+ self.instance_generator = instance_generator
+ self.render_representer = render_representer || deduce_render_representer
+ end
+
+ def mount
+ show = self
+
+ -> do
+ show.render(instance_exec(params, &show.instance_generator))
+ end
+ end
+
+ def render(instance)
+ render_representer
+ .create(instance,
+ current_user: User.current,
+ embed_links: true)
+ end
+
+ def self_path
+ api_name.underscore.pluralize
+ end
+
+ attr_accessor :model,
+ :api_name,
+ :instance_generator,
+ :render_representer
+
+ private
+
+ def deduce_render_representer
+ raise NotImplementedError
+ end
+
+ def deduce_api_namespace
+ api_name.pluralize
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/utilities/endpoints/update.rb b/lib/api/utilities/endpoints/update.rb
new file mode 100644
index 00000000000..9ff2a72dcc6
--- /dev/null
+++ b/lib/api/utilities/endpoints/update.rb
@@ -0,0 +1,47 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module Utilities
+ module Endpoints
+ class Update < Modify
+ def default_instance_generator(model)
+ ->(_params) do
+ instance_variable_get("@#{model.name.demodulize.underscore}")
+ end
+ end
+
+ private
+
+ def update_or_create
+ "Update"
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/utilities/endpoints/bodied.rb b/lib/api/v3/utilities/endpoints/bodied.rb
deleted file mode 100644
index d3036d3306f..00000000000
--- a/lib/api/v3/utilities/endpoints/bodied.rb
+++ /dev/null
@@ -1,170 +0,0 @@
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
-#++
-
-module API
- module V3
- module Utilities
- module Endpoints
- class Bodied
- def default_instance_generator(_model)
- raise NotImplementedError
- end
-
- def default_params_modifier
- ->(params) do
- params
- end
- end
-
- def initialize(model:,
- api_name: model.name.demodulize,
- instance_generator: default_instance_generator(model),
- params_modifier: default_params_modifier,
- process_service: nil,
- parse_service: API::V3::ParseResourceParamsService)
- self.model = model
- self.api_name = api_name
- self.instance_generator = instance_generator
- self.params_modifier = params_modifier
- self.parse_representer = deduce_parse_representer
- self.render_representer = deduce_render_representer
- self.process_contract = deduce_process_contract
- self.process_service = process_service || deduce_process_service
- self.parse_service = parse_service
- end
-
- def mount
- update = self
-
- -> do
- params = update.parse(current_user, request_body)
-
- params = instance_exec(params, &update.params_modifier)
-
- call = update.process(current_user,
- instance_exec(params, &update.instance_generator),
- params)
-
- update.render(current_user, call) do
- status update.success_status
- end
- end
- end
-
- def parse(current_user, request_body)
- parse_service
- .new(current_user,
- model: model,
- representer: parse_representer)
- .call(request_body)
- .result
- end
-
- def process(current_user, instance, params)
- args = { user: current_user,
- model: instance,
- contract_class: process_contract }
-
- process_service
- .new(args.compact)
- .call(params)
- end
-
- def render(current_user, call)
- if success?(call)
- yield
- present_success(current_user, call)
- else
- present_error(call)
- end
- end
-
- def success_status
- :ok
- end
-
- attr_accessor :model,
- :api_name,
- :instance_generator,
- :parse_representer,
- :render_representer,
- :params_modifier,
- :process_contract,
- :process_service,
- :parse_service
-
- private
-
- def present_success(_current_user, _call)
- raise NotImplementedError
- end
-
- def present_error(_call)
- raise NotImplementedError
- end
-
- def success?(call)
- call.success?
- end
-
- def deduce_process_service
- "::#{decude_backend_namespace}::SetAttributesService".constantize
- end
-
- def deduce_process_contract
- "::#{decude_backend_namespace}::#{update_or_create}Contract".constantize
- end
-
- def deduce_parse_representer
- "::API::V3::#{deduce_api_namespace}::#{api_name}PayloadRepresenter".constantize
- end
-
- def deduce_render_representer
- raise NotImplementedError
- end
-
- def deduce_api_namespace
- api_name.pluralize
- end
-
- def backend_name
- model.name.demodulize
- end
-
- def decude_backend_namespace
- backend_name.pluralize
- end
-
- def update_or_create
- raise NotImplementedError
- end
- end
- end
- end
- end
-end
diff --git a/lib/api/v3/utilities/endpoints/create.rb b/lib/api/v3/utilities/endpoints/create.rb
index e046f2ed43b..e7f26720ce3 100644
--- a/lib/api/v3/utilities/endpoints/create.rb
+++ b/lib/api/v3/utilities/endpoints/create.rb
@@ -30,21 +30,9 @@ module API
module V3
module Utilities
module Endpoints
- class Create < Modify
- def default_instance_generator(_model)
- ->(_params) do
- end
- end
-
- def success_status
- :created
- end
-
- private
-
- def update_or_create
- "Create"
- end
+ class Create < API::Utilities::Endpoints::Create
+ include V3Deductions
+ include V3PresentSingle
end
end
end
diff --git a/lib/api/v3/utilities/endpoints/delete.rb b/lib/api/v3/utilities/endpoints/delete.rb
index 5edba946175..e547b90794d 100644
--- a/lib/api/v3/utilities/endpoints/delete.rb
+++ b/lib/api/v3/utilities/endpoints/delete.rb
@@ -30,78 +30,7 @@ module API
module V3
module Utilities
module Endpoints
- class Delete
- def default_instance_generator(model)
- ->(_params) do
- instance_variable_get("@#{model.name.demodulize.underscore}")
- end
- end
-
- def initialize(model:,
- instance_generator: default_instance_generator(model),
- process_service: nil)
- self.model = model
- self.instance_generator = instance_generator
- self.process_service = process_service || deduce_process_service
- end
-
- def mount
- delete = self
-
- -> do
- call = delete.process(current_user,
- instance_exec(params, &delete.instance_generator))
-
- delete.render(call) do
- status delete.success_status
- end
- end
- end
-
- def process(current_user, instance)
- process_service
- .new(user: current_user,
- model: instance)
- .call
- end
-
- def render(call)
- if success?(call)
- yield
- else
- present_error(call)
- end
- end
-
- def success_status
- 204
- end
-
- attr_accessor :model,
- :instance_generator,
- :process_service
-
- private
-
- def present_error(call)
- fail ::API::Errors::ErrorBase.create_and_merge_errors(call.errors)
- end
-
- def success?(call)
- call.success?
- end
-
- def deduce_process_service
- "::#{deduce_namespace}::DeleteService".constantize
- end
-
- def deduce_namespace
- demodulized_name.pluralize
- end
-
- def demodulized_name
- model.name.demodulize
- end
+ class Delete < API::Utilities::Endpoints::Delete
end
end
end
diff --git a/lib/api/v3/utilities/endpoints/form.rb b/lib/api/v3/utilities/endpoints/form.rb
index 4ee4c69cab2..6267883cff9 100644
--- a/lib/api/v3/utilities/endpoints/form.rb
+++ b/lib/api/v3/utilities/endpoints/form.rb
@@ -30,7 +30,9 @@ module API
module V3
module Utilities
module Endpoints
- class Form < Bodied
+ class Form < API::Utilities::Endpoints::Bodied
+ include V3Deductions
+
def success?(call)
only_validation_errors?(api_errors(call))
end
diff --git a/lib/api/v3/utilities/endpoints/index.rb b/lib/api/v3/utilities/endpoints/index.rb
index 5d80e36703f..c9810d29ef0 100644
--- a/lib/api/v3/utilities/endpoints/index.rb
+++ b/lib/api/v3/utilities/endpoints/index.rb
@@ -30,122 +30,7 @@ module API
module V3
module Utilities
module Endpoints
- class Index
- include ::API::Utilities::PageSizeHelper
-
- def initialize(model:,
- api_name: model.name.demodulize,
- scope: nil,
- render_representer: nil)
- self.model = model_class(model)
- self.scope = scope
- self.api_name = api_name
- self.render_representer = render_representer || deduce_render_representer
- end
-
- def mount
- index = self
-
- -> do
- query = index.parse(params)
-
- self_path = api_v3_paths.send(index.self_path)
- base_scope = index.scope ? instance_exec(&index.scope) : index.model
-
- index.render(query, params, self_path, base_scope)
- end
- end
-
- def parse(params)
- ParamsToQueryService
- .new(model, User.current)
- .call(params)
- end
-
- def render(query, params, self_path, base_scope)
- if query.valid?
- render_success(query, params, self_path, base_scope)
- else
- render_error(query)
- end
- end
-
- def self_path
- api_name.underscore.pluralize
- end
-
- attr_accessor :model,
- :api_name,
- :scope,
- :render_representer
-
- private
-
- def render_success(query, params, self_path, base_scope)
- results = merge_scopes(base_scope, query.results)
-
- if paginated_representer?
- render_paginated_success(results, params, self_path)
- else
- render_unpaginated_success(results, self_path)
- end
- end
-
- def render_paginated_success(results, params, self_path)
- render_representer
- .new(results,
- self_path,
- page: to_i_or_nil(params[:offset]),
- per_page: resolve_page_size(params[:pageSize]),
- current_user: User.current)
- end
-
- def render_unpaginated_success(results, self_path)
- render_representer
- .new(results,
- self_path,
- current_user: User.current)
- end
-
- def paginated_representer?
- render_representer.ancestors.include?(::API::Decorators::OffsetPaginatedCollection)
- end
-
- def render_error(query)
- raise ::API::Errors::InvalidQuery.new(query.errors.full_messages)
- end
-
- def deduce_render_representer
- "::API::V3::#{deduce_namespace}::#{api_name}CollectionRepresenter".constantize
- end
-
- def deduce_namespace
- api_name.pluralize
- end
-
- def model_class(scope)
- if scope.is_a? Class
- scope
- else
- scope.model
- end
- end
-
- def with_based_scope(other_scope)
- if scope
- merge_scopes(scope, other_scope)
- else
- other_scope
- end
- end
-
- def merge_scopes(scope_a, scope_b)
- if scope_a.is_a? Class
- scope_b
- else
- scope_a.merge(scope_b)
- end
- end
+ class Index < API::Utilities::Endpoints::Index
end
end
end
diff --git a/lib/api/v3/utilities/endpoints/modify.rb b/lib/api/v3/utilities/endpoints/modify.rb
deleted file mode 100644
index c9eb2760288..00000000000
--- a/lib/api/v3/utilities/endpoints/modify.rb
+++ /dev/null
@@ -1,97 +0,0 @@
-#-- copyright
-# OpenProject is a project management system.
-# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
-#++
-
-module API
- module V3
- module Utilities
- module Endpoints
- class Modify < Bodied
- def default_instance_generator(model)
- ->(_params, _current_user) do
- instance_variable_get("@#{model.name.demodulize.underscore}")
- end
- end
-
- private
-
- def present_success(current_user, call)
- render_representer
- .create(call.result,
- current_user: current_user,
- embed_links: true)
- end
-
- def present_error(call)
- errors = call.errors
- errors = merge_dependent_errors call if errors.empty?
-
- api_errors = [::API::Errors::ErrorBase.create_and_merge_errors(errors)]
-
- fail ::API::Errors::MultipleErrors.create_if_many(api_errors)
- end
-
- def merge_dependent_errors(call)
- errors = ActiveModel::Errors.new call.result
-
- call.dependent_results.each do |dr|
- dr.errors.keys.each do |field|
- dr.errors.symbols_and_messages_for(field).each do |symbol, full_message, _|
- errors.add :base, symbol, message: dependent_error_message(dr.result, full_message)
- end
- end
- end
-
- errors
- end
-
- def dependent_error_message(result, full_message)
- I18n.t(
- :error_in_dependent,
- dependent_class: result.model_name.human,
- related_id: result.id,
- related_subject: result.name,
- error: full_message
- )
- end
-
- def deduce_process_service
- "::#{decude_backend_namespace}::#{update_or_create}Service".constantize
- end
-
- def deduce_render_representer
- "::API::V3::#{deduce_api_namespace}::#{api_name}Representer".constantize
- end
-
- def deduce_process_contract
- nil
- end
- end
- end
- end
- end
-end
diff --git a/lib/api/v3/utilities/endpoints/show.rb b/lib/api/v3/utilities/endpoints/show.rb
index 0ac771893b7..d3bfd65d397 100644
--- a/lib/api/v3/utilities/endpoints/show.rb
+++ b/lib/api/v3/utilities/endpoints/show.rb
@@ -30,57 +30,8 @@ module API
module V3
module Utilities
module Endpoints
- class Show
- def default_instance_generator(model)
- ->(_params) do
- instance_variable_get("@#{model.name.demodulize.underscore}")
- end
- end
-
- def initialize(model:,
- api_name: model.name.demodulize,
- render_representer: nil,
- instance_generator: default_instance_generator(model))
-
- self.model = model
- self.api_name = api_name
- self.instance_generator = instance_generator
- self.render_representer = render_representer || deduce_render_representer
- end
-
- def mount
- show = self
-
- -> do
- show.render(instance_exec(params, &show.instance_generator))
- end
- end
-
- def render(instance)
- render_representer
- .create(instance,
- current_user: User.current,
- embed_links: true)
- end
-
- def self_path
- api_name.underscore.pluralize
- end
-
- attr_accessor :model,
- :api_name,
- :instance_generator,
- :render_representer
-
- private
-
- def deduce_render_representer
- "::API::V3::#{deduce_namespace}::#{api_name}Representer".constantize
- end
-
- def deduce_namespace
- api_name.pluralize
- end
+ class Show < API::Utilities::Endpoints::Show
+ include V3Deductions
end
end
end
diff --git a/lib/api/v3/utilities/endpoints/update.rb b/lib/api/v3/utilities/endpoints/update.rb
index b2bbf588f6d..1446e4b3edb 100644
--- a/lib/api/v3/utilities/endpoints/update.rb
+++ b/lib/api/v3/utilities/endpoints/update.rb
@@ -30,18 +30,9 @@ module API
module V3
module Utilities
module Endpoints
- class Update < Modify
- def default_instance_generator(model)
- ->(_params) do
- instance_variable_get("@#{model.name.demodulize.underscore}")
- end
- end
-
- private
-
- def update_or_create
- "Update"
- end
+ class Update < API::Utilities::Endpoints::Update
+ include V3Deductions
+ include V3PresentSingle
end
end
end
diff --git a/lib/api/v3/utilities/endpoints/v3_deductions.rb b/lib/api/v3/utilities/endpoints/v3_deductions.rb
new file mode 100644
index 00000000000..46b43e0734e
--- /dev/null
+++ b/lib/api/v3/utilities/endpoints/v3_deductions.rb
@@ -0,0 +1,51 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Utilities
+ module Endpoints
+ module V3Deductions
+ private
+
+ def deduce_parse_service
+ API::V3::ParseResourceParamsService
+ end
+
+ def deduce_render_representer
+ "::API::V3::#{deduce_api_namespace}::#{api_name}Representer".constantize
+ end
+
+ def deduce_parse_representer
+ "::API::V3::#{deduce_api_namespace}::#{api_name}PayloadRepresenter".constantize
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/utilities/endpoints/v3_present_single.rb b/lib/api/v3/utilities/endpoints/v3_present_single.rb
new file mode 100644
index 00000000000..0654cc7e7dd
--- /dev/null
+++ b/lib/api/v3/utilities/endpoints/v3_present_single.rb
@@ -0,0 +1,44 @@
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module API
+ module V3
+ module Utilities
+ module Endpoints
+ module V3PresentSingle
+ def present_success(current_user, call)
+ render_representer
+ .create(call.result,
+ current_user: current_user,
+ embed_links: true)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/update.rb b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/update.rb
new file mode 100644
index 00000000000..81e9e886743
--- /dev/null
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/endpoints/update.rb
@@ -0,0 +1,51 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2018 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-2017 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 docs/COPYRIGHT.rdoc for more details.
+#++
+
+module Bcf::API::V2_1::Endpoints
+ class Update < API::Utilities::Endpoints::Update
+ def present_success(current_user, call)
+ render_representer
+ .new(call.result)
+ end
+
+ private
+
+ def deduce_parse_service
+ Bcf::API::V2_1::ParseResourceParamsService
+ end
+
+ def deduce_in_and_out_representer
+ "::Bcf::API::V2_1::#{deduce_api_namespace}::SingleRepresenter".constantize
+ end
+
+ alias_method :deduce_parse_representer, :deduce_in_and_out_representer
+ alias_method :deduce_render_representer, :deduce_in_and_out_representer
+ end
+end
diff --git a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
index 0a489a0d785..f3d47021cb3 100644
--- a/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
+++ b/modules/bcf/app/controllers/bcf/api/v2_1/projects_api.rb
@@ -58,22 +58,7 @@ module Bcf::API::V2_1
.new(@project)
end
- put do
- parse_call = Bcf::API::V2_1::ParseResourceParamsService
- .new(current_user, model: Project)
- .call(request_body)
-
- update_call = ::Projects::UpdateService
- .new(user: current_user, model: @project)
- .call(parse_call.result)
-
- if update_call.success?
- Bcf::API::V2_1::Projects::SingleRepresenter
- .new(update_call.result)
- else
- fail ::API::Errors::ErrorBase.create_and_merge_errors(update_call.errors)
- end
- end
+ put &::Bcf::API::V2_1::Endpoints::Update.new(model: Project).mount
end
end
end