scinote-web/app/controllers/api/v2/step_elements/assets_controller.rb
wandji 465c8d1f80
Unify API step and result elements controllers implementation and types [SCI-9907] (#6854)
* Unify api step and result elements controllers implementation and types [SCI-9907]

* Update wrong serializer types [SCI-9907]

* Rename result and step text types [SCI-9907]

* Combine result and step assets serializer [SCI-9907]
2024-01-08 10:31:14 +01:00

43 lines
1.3 KiB
Ruby

# frozen_string_literal: true
module Api
module V2
module StepElements
class AssetsController < ::Api::V1::AssetsController
def index
attachments =
timestamps_filter(@step.assets).page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: attachments, each_serializer: Api::V2::AssetSerializer
end
def show
render jsonapi: @asset, serializer: Api::V2::AssetSerializer
end
def create
raise PermissionError.new(Asset, :create) unless can_manage_protocol_in_module?(@protocol)
if @form_multipart_upload
asset = @step.assets.new(asset_params.merge({ team_id: @team.id }))
else
blob = ActiveStorage::Blob.create_and_upload!(
io: StringIO.new(Base64.decode64(asset_params[:file_data])),
filename: asset_params[:file_name],
content_type: asset_params[:file_type]
)
asset = @step.assets.new(file: blob, team: @team)
end
asset.save!(context: :on_api_upload)
asset.post_process_file
render jsonapi: asset,
serializer: Api::V2::AssetSerializer,
status: :created
end
end
end
end
end