|
|
@ -28,28 +28,58 @@ |
|
|
|
|
|
|
|
|
|
|
|
module API::V3::FileLinks |
|
|
|
module API::V3::FileLinks |
|
|
|
class ParseCreateParamsService < ::API::ParseResourceParamsService |
|
|
|
class ParseCreateParamsService < ::API::ParseResourceParamsService |
|
|
|
|
|
|
|
MAX_ELEMENTS = 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
attr_reader :request_body |
|
|
|
|
|
|
|
|
|
|
|
def call(request_body) |
|
|
|
def call(request_body) |
|
|
|
|
|
|
|
@request_body = request_body |
|
|
|
ServiceResult.new( |
|
|
|
ServiceResult.new( |
|
|
|
success: true, |
|
|
|
success: true, |
|
|
|
result: parse_elements(request_body) |
|
|
|
result: parse_elements |
|
|
|
) |
|
|
|
) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def parse_elements(request_body) |
|
|
|
private |
|
|
|
request_body.dig("_embedded", "elements") |
|
|
|
|
|
|
|
.tap { ensure_valid_elements(_1) } |
|
|
|
def parse_elements |
|
|
|
.map do |params| |
|
|
|
assert_valid_elements |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
elements.map do |element| |
|
|
|
API::V3::FileLinks::FileLinkRepresenter |
|
|
|
API::V3::FileLinks::FileLinkRepresenter |
|
|
|
.new(Hashie::Mash.new, current_user: current_user) |
|
|
|
.new(Hashie::Mash.new, current_user: current_user) |
|
|
|
.from_hash(params) |
|
|
|
.from_hash(element) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
def elements |
|
|
|
|
|
|
|
@elements ||= request_body.dig("_embedded", "elements") |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_valid_elements |
|
|
|
|
|
|
|
assert_elements_is_present |
|
|
|
|
|
|
|
assert_elements_is_an_array |
|
|
|
|
|
|
|
assert_elements_does_not_exceed_maximum |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_elements_is_present |
|
|
|
|
|
|
|
return if elements.present? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
raise API::Errors::PropertyMissingError.new('_embedded/elements') |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_elements_is_an_array |
|
|
|
|
|
|
|
return if elements.is_a?(Array) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
raise API::Errors::PropertyFormatError.new('_embedded/elements', 'Array', elements.class.name) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def assert_elements_does_not_exceed_maximum |
|
|
|
|
|
|
|
return if elements.size <= MAX_ELEMENTS |
|
|
|
|
|
|
|
|
|
|
|
def ensure_valid_elements(elements) |
|
|
|
raise API::Errors::Validation.new('_embedded/elements', |
|
|
|
raise API::Errors::PropertyMissingError.new('_embedded/elements') if elements.blank? |
|
|
|
I18n.t('api_v3.errors.too_many_elements_created_at_once', |
|
|
|
raise API::Errors::PropertyFormatError.new('_embedded/elements', 'Array', elements.class.name) unless elements.is_a?(Array) |
|
|
|
max: MAX_ELEMENTS, actual: elements.size)) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|