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

post /work_packages/:id/file_links first implementation

Christophe Bliardc46ee9a239bb
diff --git a/lib/api/v3/utilities/path_helper.rb b/lib/api/v3/utilities/path_helper.rb
index 5de46833150..3690acc86d7 100644
--- a/lib/api/v3/utilities/path_helper.rb
+++ b/lib/api/v3/utilities/path_helper.rb
@@ -198,6 +198,10 @@ module API
"#{root}/custom_options/#{id}"
end
+ def self.file_links_by_work_package(id)
+ "#{work_package(id)}/file_links"
+ end
+
index :help_text
show :help_text
@@ -504,7 +508,7 @@ module API
groupBy: group_by,
pageSize: page_size,
offset: offset
- }.reject { |_, v| v.blank? }
+ }.compact_blank
if query_params.any?
"#{send(path)}?#{query_params.to_query}"
diff --git a/modules/storages/app/contracts/storages/file_links/create_contract.rb b/modules/storages/app/contracts/storages/file_links/create_contract.rb
new file mode 100644
index 00000000000..07dd17d1969
--- /dev/null
+++ b/modules/storages/app/contracts/storages/file_links/create_contract.rb
@@ -0,0 +1,30 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+class Storages::FileLinks::CreateContract < BaseContract
+end
diff --git a/modules/storages/app/services/storages/file_links/create_service.rb b/modules/storages/app/services/storages/file_links/create_service.rb
new file mode 100644
index 00000000000..c03d0e91035
--- /dev/null
+++ b/modules/storages/app/services/storages/file_links/create_service.rb
@@ -0,0 +1,32 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+class Storages::FileLinks::CreateService < ::BaseServices::Create
+end
diff --git a/modules/storages/app/services/storages/file_links/set_attributes_service.rb b/modules/storages/app/services/storages/file_links/set_attributes_service.rb
new file mode 100644
index 00000000000..1031abdbeb0
--- /dev/null
+++ b/modules/storages/app/services/storages/file_links/set_attributes_service.rb
@@ -0,0 +1,32 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+class Storages::FileLinks::SetAttributesService < ::BaseServices::SetAttributes
+end
diff --git a/modules/storages/lib/api/v3/file_links/create_endpoint.rb b/modules/storages/lib/api/v3/file_links/create_endpoint.rb
new file mode 100644
index 00000000000..a62ed8b665c
--- /dev/null
+++ b/modules/storages/lib/api/v3/file_links/create_endpoint.rb
@@ -0,0 +1,38 @@
+module API::V3::FileLinks
+ class CreateEndpoint < API::Utilities::Endpoints::Create
+ include ::API::V3::Utilities::Endpoints::V3Deductions
+ include ::API::V3::Utilities::Endpoints::V3PresentSingle
+
+ def process(request, params_elements)
+ global_result = ServiceResult.new(
+ success: true,
+ result: []
+ )
+ params_elements.each do |params|
+ one_result = super(request, params)
+ global_result.add_dependent!(one_result)
+ global_result.result << one_result.result
+ end
+ global_result
+ end
+
+ def present_success(request, call)
+ render_representer.create(
+ call.result,
+ self_link: request.api_v3_paths.file_links_by_work_package(request.work_package.id),
+ current_user: request.current_user
+ )
+ end
+
+ private
+
+ def params_modifier
+ ->(params) do
+ params[:creator_id] = current_user.id
+ params[:container_id] = work_package.id
+ params[:container_type] = work_package.class.name
+ params
+ end
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb
index 2bd0918ced3..55cc7e5ee2e 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_collection_representer.rb
@@ -29,7 +29,7 @@
module API
module V3
module FileLinks
- class FileLinkCollectionRepresenter < ::API::Decorators::OffsetPaginatedCollection
+ class FileLinkCollectionRepresenter < ::API::Decorators::UnpaginatedCollection
end
end
end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_payload_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_payload_representer.rb
new file mode 100644
index 00000000000..198b8b5a175
--- /dev/null
+++ b/modules/storages/lib/api/v3/file_links/file_link_payload_representer.rb
@@ -0,0 +1,37 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module API
+ module V3
+ module FileLinks
+ class FileLinkPayloadRepresenter < FileLinkRepresenter
+ include ::API::Utilities::PayloadRepresenter
+ end
+ end
+ end
+end
diff --git a/modules/storages/lib/api/v3/file_links/file_link_representer.rb b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
index b4dbfaffb07..3f39f89b036 100644
--- a/modules/storages/lib/api/v3/file_links/file_link_representer.rb
+++ b/modules/storages/lib/api/v3/file_links/file_link_representer.rb
@@ -43,7 +43,9 @@ module API
exec_context: :decorator,
getter: ->(*) { make_origin_data(represented) },
setter: ->(fragment:, **) do
- represented.assign_attributes(parse_origin_data(fragment))
+ parse_origin_data(fragment).each do |attribute, value|
+ represented[attribute] = value
+ end
end
link :self do
diff --git a/modules/storages/lib/api/v3/file_links/file_links_api.rb b/modules/storages/lib/api/v3/file_links/file_links_api.rb
index 61ad8fb8098..13cb8a895da 100644
--- a/modules/storages/lib/api/v3/file_links/file_links_api.rb
+++ b/modules/storages/lib/api/v3/file_links/file_links_api.rb
@@ -43,9 +43,13 @@ module API
self_path: -> { api_v3_paths.file_links(params[:id]) })
.mount
- post do
- raise ::API::Errors::NotImplemented
- end
+ post &CreateEndpoint
+ .new(
+ model: ::Storages::FileLink,
+ parse_service: ParseCreateParamsService,
+ render_representer: FileLinkCollectionRepresenter
+ )
+ .mount
route_param :file_link_id, type: Integer, desc: 'File link id' do
after_validation do
diff --git a/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb b/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb
new file mode 100644
index 00000000000..4e04bff74b8
--- /dev/null
+++ b/modules/storages/lib/api/v3/file_links/parse_create_params_service.rb
@@ -0,0 +1,46 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+module API::V3::FileLinks
+ class ParseCreateParamsService < ::API::ParseResourceParamsService
+ def call(request_body)
+ ServiceResult.new(
+ success: true,
+ result: parse_elements(request_body)
+ )
+ end
+
+ def parse_elements(request_body)
+ request_body.dig("_embedded", "elements").map do |params|
+ API::V3::FileLinks::FileLinkRepresenter
+ .new(Hashie::Mash.new, current_user: current_user)
+ .from_hash(params)
+ end
+ end
+ end
+end
diff --git a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
index ce0ef4f09bd..99af1672f82 100644
--- a/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
+++ b/modules/storages/spec/requests/api/v3/file_links/file_links_spec.rb
@@ -79,14 +79,60 @@ describe 'API v3 file links resource', type: :request do
describe 'POST /api/v3/work_packages/:work_package_id/file_links' do
let(:path) { api_v3_paths.file_links(work_package.id) }
- let(:params) { {} }
+ let(:params) do
+ {
+ _type: "Collection",
+ _embedded: {
+ elements: [
+ {
+ originData: {
+ id: 5503,
+ name: "logo.png",
+ mimeType: "image/png",
+ createdAt: "2021-12-19T09:42:10.170Z",
+ lastModifiedAt: "2021-12-20T14:00:13.987Z",
+ createdByName: "Luke Skywalker",
+ lastModifiedByName: "Anakin Skywalker"
+ },
+ _links: {
+ storageUrl: {
+ href: "https://nextcloud.deathstar.rocks/"
+ }
+ }
+ },
+ {
+ originData: {
+ id: 5514,
+ name: "plans.md",
+ mimeType: "text/markdown",
+ createdAt: "2021-12-14T09:01:23.456Z",
+ lastModifiedAt: "2021-12-14T09:01:23.456Z",
+ createdByName: "Yoda",
+ lastModifiedByName: "Yoda"
+ },
+ _links: {
+ storageUrl: {
+ href: "https://nextcloud.deathstar.rocks/"
+ }
+ }
+ }
+ ]
+ }
+ }
+ end
before do
- post path, params.to_json, 'CONTENT_TYPE' => 'application/json'
+ header 'Content-Type', 'application/json'
+ post path, params.to_json
end
- it 'returns not implemented' do
- expect(subject.status).to be 501
+ it 'returns ok created' do
+ expect(response.status).to eq(201)
+ end
+
+ it_behaves_like 'API V3 collection response', 2, 2, 'FileLink' do
+ let(:elements) { Storages::FileLink.all.order(id: :asc) }
+ let(:expected_status_code) { 201 }
end
end
diff --git a/spec/requests/api/v3/support/api_v3_collection_response.rb b/spec/requests/api/v3/support/api_v3_collection_response.rb
index 3b28e6acd07..c874e954ae1 100644
--- a/spec/requests/api/v3/support/api_v3_collection_response.rb
+++ b/spec/requests/api/v3/support/api_v3_collection_response.rb
@@ -30,6 +30,7 @@ require 'spec_helper'
shared_examples_for 'API V3 collection response' do |total, count, element_type, collection_type = 'Collection'|
subject { last_response.body }
+
# If an array of elements is provided, those elements are expected
# to be embedded in the _embedded/elements section in the order provided.
# Only the id of the element is checked for.
@@ -53,9 +54,12 @@ shared_examples_for 'API V3 collection response' do |total, count, element_type,
end
end
+ # Allow overridding the expect HTTP status code
+ let(:expected_status_code) { 200 }
+
it 'returns a collection successfully' do
aggregate_failures do
- expect(last_response.status).to eql(200)
+ expect(last_response.status).to eq(expected_status_code)
expect(subject).to be_json_eql(collection_type.to_json).at_path('_type')
expect(subject).to be_json_eql(count_number.to_json).at_path('count')
expect(subject).to be_json_eql(total_number.to_json).at_path('total')