From 7527dc7352faf0e7ca89da6e77dabd2a7f0079fc Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 12:36:58 +0200 Subject: [PATCH] Fix READ index, READ show, POST create /tasks/:id/results endpoints --- app/controllers/api/v1/results_controller.rb | 6 ++-- .../v1/repository_asset_value_serializer.rb | 3 +- .../api/v1/result_asset_serializer.rb | 28 +++++++++++++++++-- app/serializers/api/v1/result_serializer.rb | 22 +++++++-------- .../api/v1/result_table_serializer.rb | 10 ++++++- config/routes.rb | 4 +-- 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/app/controllers/api/v1/results_controller.rb b/app/controllers/api/v1/results_controller.rb index 068d927f3..938c4e20a 100644 --- a/app/controllers/api/v1/results_controller.rb +++ b/app/controllers/api/v1/results_controller.rb @@ -14,7 +14,7 @@ module Api .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) render jsonapi: results, each_serializer: ResultSerializer, - include: %i(asset table text) + include: %i(text table file) end def create @@ -22,13 +22,13 @@ module Api throw ActionController::ParameterMissing unless @result render jsonapi: @result, serializer: ResultSerializer, - include: %i(asset table text), + include: %i(text table file), status: :created end def show render jsonapi: @result, serializer: ResultSerializer, - include: %i(asset table text) + include: %i(text table file) end private diff --git a/app/serializers/api/v1/repository_asset_value_serializer.rb b/app/serializers/api/v1/repository_asset_value_serializer.rb index be1373efb..50fac8350 100644 --- a/app/serializers/api/v1/repository_asset_value_serializer.rb +++ b/app/serializers/api/v1/repository_asset_value_serializer.rb @@ -23,8 +23,9 @@ module Api elsif object.asset&.file&.is_stored_on_s3? object.asset.presigned_url(download: true) else + # TODO # separate api endpoint for local files download is needed - download_asset_path(object.asset.id) + 'url'#download_asset_path(object.asset.id) end end end diff --git a/app/serializers/api/v1/result_asset_serializer.rb b/app/serializers/api/v1/result_asset_serializer.rb index f6638c2b7..f69dbbae8 100644 --- a/app/serializers/api/v1/result_asset_serializer.rb +++ b/app/serializers/api/v1/result_asset_serializer.rb @@ -3,8 +3,32 @@ module Api module V1 class ResultAssetSerializer < ActiveModel::Serializer - type :result_assets - attributes :asset_id + type :result_files + attributes :file_id, :file_name, :file_size, :url + + def file_id + object.asset&.id + end + + def file_name + object.asset&.file_file_name + end + + def file_size + object.asset&.file_file_size + end + + def url + if !object.asset&.file_present + nil + elsif object.asset&.file&.is_stored_on_s3? + object.asset.presigned_url(download: true) + else + # TODO + # separate api endpoint for local files download is needed + 'url'#download_asset_path(object.asset.id) + end + end end end end diff --git a/app/serializers/api/v1/result_serializer.rb b/app/serializers/api/v1/result_serializer.rb index 5e990f1cb..ea0f4b334 100644 --- a/app/serializers/api/v1/result_serializer.rb +++ b/app/serializers/api/v1/result_serializer.rb @@ -4,22 +4,20 @@ module Api module V1 class ResultSerializer < ActiveModel::Serializer type :results - attributes :name, :user_id, :archived - attribute :my_module_id, key: :task_id - - belongs_to :my_module, serializer: TaskSerializer - has_one :result_asset, key: :asset, - serializer: ResultAssetSerializer, - class_name: 'ResultAsset', - if: -> { object.is_asset } - has_one :result_table, key: :table, - serializer: ResultTableSerializer, - class_name: 'ResultTable', - if: -> { object.is_table } + attributes :name, :archived + belongs_to :user, serializer: UserSerializer has_one :result_text, key: :text, serializer: ResultTextSerializer, class_name: 'ResultText', if: -> { object.is_text } + has_one :result_table, key: :table, + serializer: ResultTableSerializer, + class_name: 'ResultTable', + if: -> { object.is_table } + has_one :result_asset, key: :file, + serializer: ResultAssetSerializer, + class_name: 'ResultAsset', + if: -> { object.is_asset } end end end diff --git a/app/serializers/api/v1/result_table_serializer.rb b/app/serializers/api/v1/result_table_serializer.rb index b4bdddc1c..211950441 100644 --- a/app/serializers/api/v1/result_table_serializer.rb +++ b/app/serializers/api/v1/result_table_serializer.rb @@ -4,7 +4,15 @@ module Api module V1 class ResultTableSerializer < ActiveModel::Serializer type :result_tables - attributes :table_id + attributes :table_id, :table_contents + + def table_id + object.table&.id + end + + def table_contents + object.table&.contents + end end end end diff --git a/config/routes.rb b/config/routes.rb index f4440c769..4b5ebcef1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -576,9 +576,7 @@ Rails.application.routes.draw do path: 'tags', as: :tags resources :protocols, only: %i(index show) - resources :results, only: %i(index create show), - path: 'results', - as: :results + resources :results, only: %i(index create show) get 'inputs', to: 'tasks#inputs' get 'outputs', to: 'tasks#outputs' get 'activities', to: 'tasks#activities'