Extract WP writing from form API

pull/2153/head
Hagen Schink 10 years ago
parent 1ef1f19780
commit 52bdc90222
  1. 22
      lib/api/v3/work_packages/form/form_api.rb
  2. 30
      lib/api/v3/work_packages/work_packages_api.rb

@ -33,23 +33,8 @@ module API
class FormAPI < Grape::API class FormAPI < Grape::API
helpers do helpers do
def process_form_request def process_form_request
if form_post_request_body write_work_package_attributes
form_post_request_body_without_links = form_post_request_body write_request_valid?
links = form_post_request_body_without_links.delete('_links')
# enforces availibility validation of lock_version
@representer.represented.lock_version = nil
@representer.from_json(form_post_request_body_without_links.to_json)
if links
link_to_object_extractor = ::API::V3::WorkPackages::LinkToObjectExtractor.new
linked_properties = link_to_object_extractor.parse_links(links)
@representer.represented.attributes = linked_properties
end
patch_request_valid?
end
error = ::API::Errors::ErrorBase.create(@representer.represented.errors) error = ::API::Errors::ErrorBase.create(@representer.represented.errors)
@ -61,9 +46,6 @@ module API
end end
end end
def form_post_request_body
env['api.request.body']
end
end end
post '/form' do post '/form' do

@ -44,11 +44,29 @@ module API
@representer = ::API::V3::WorkPackages::WorkPackageRepresenter.new(work_package, { current_user: current_user }, :activities, :users) @representer = ::API::V3::WorkPackages::WorkPackageRepresenter.new(work_package, { current_user: current_user }, :activities, :users)
end end
def patch_request_body def write_work_package_attributes
env['api.request.input'] if request_body
request_body_without_links = request_body
links = request_body_without_links.delete('_links')
# enforces availibility validation of lock_version
@representer.represented.lock_version = nil
@representer.from_json(request_body_without_links.to_json)
if links
link_to_object_extractor = ::API::V3::WorkPackages::LinkToObjectExtractor.new
linked_properties = link_to_object_extractor.parse_links(links)
@representer.represented.attributes = linked_properties
end
end
end
def request_body
env['api.request.body']
end end
def patch_request_valid? def write_request_valid?
contract = WorkPackageContract.new(@representer.represented, current_user) contract = WorkPackageContract.new(@representer.represented, current_user)
# Although the contract triggers the ActiveModel validations on # Although the contract triggers the ActiveModel validations on
@ -77,9 +95,7 @@ module API
end end
patch do patch do
@representer.represented.lock_version = nil # enforces availibility validation of lock_version write_work_package_attributes
@representer.from_json(patch_request_body)
send_notifications = !(params.has_key?(:notify) && params[:notify] == 'false') send_notifications = !(params.has_key?(:notify) && params[:notify] == 'false')
update_service = UpdateWorkPackageService.new(current_user, update_service = UpdateWorkPackageService.new(current_user,
@ -87,7 +103,7 @@ module API
nil, nil,
send_notifications) send_notifications)
if patch_request_valid? && update_service.save if write_request_valid? && update_service.save
decorate_work_package(@work_package.reload) decorate_work_package(@work_package.reload)
@representer @representer
else else

Loading…
Cancel
Save