Fix READ index, READ show, POST create /tasks/:id/results endpoints

This commit is contained in:
Luka Murn 2018-10-07 12:36:58 +02:00
parent fc5cf33614
commit 7527dc7352
6 changed files with 51 additions and 22 deletions

View file

@ -14,7 +14,7 @@ module Api
.page(params.dig(:page, :number)) .page(params.dig(:page, :number))
.per(params.dig(:page, :size)) .per(params.dig(:page, :size))
render jsonapi: results, each_serializer: ResultSerializer, render jsonapi: results, each_serializer: ResultSerializer,
include: %i(asset table text) include: %i(text table file)
end end
def create def create
@ -22,13 +22,13 @@ module Api
throw ActionController::ParameterMissing unless @result throw ActionController::ParameterMissing unless @result
render jsonapi: @result, render jsonapi: @result,
serializer: ResultSerializer, serializer: ResultSerializer,
include: %i(asset table text), include: %i(text table file),
status: :created status: :created
end end
def show def show
render jsonapi: @result, serializer: ResultSerializer, render jsonapi: @result, serializer: ResultSerializer,
include: %i(asset table text) include: %i(text table file)
end end
private private

View file

@ -23,8 +23,9 @@ module Api
elsif object.asset&.file&.is_stored_on_s3? elsif object.asset&.file&.is_stored_on_s3?
object.asset.presigned_url(download: true) object.asset.presigned_url(download: true)
else else
# TODO
# separate api endpoint for local files download is needed # 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 end
end end

View file

@ -3,8 +3,32 @@
module Api module Api
module V1 module V1
class ResultAssetSerializer < ActiveModel::Serializer class ResultAssetSerializer < ActiveModel::Serializer
type :result_assets type :result_files
attributes :asset_id 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 end
end end

View file

@ -4,22 +4,20 @@ module Api
module V1 module V1
class ResultSerializer < ActiveModel::Serializer class ResultSerializer < ActiveModel::Serializer
type :results type :results
attributes :name, :user_id, :archived attributes :name, :archived
attribute :my_module_id, key: :task_id belongs_to :user, serializer: UserSerializer
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 }
has_one :result_text, key: :text, has_one :result_text, key: :text,
serializer: ResultTextSerializer, serializer: ResultTextSerializer,
class_name: 'ResultText', class_name: 'ResultText',
if: -> { object.is_text } 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 end
end end

View file

@ -4,7 +4,15 @@ module Api
module V1 module V1
class ResultTableSerializer < ActiveModel::Serializer class ResultTableSerializer < ActiveModel::Serializer
type :result_tables 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 end
end end

View file

@ -576,9 +576,7 @@ Rails.application.routes.draw do
path: 'tags', path: 'tags',
as: :tags as: :tags
resources :protocols, only: %i(index show) resources :protocols, only: %i(index show)
resources :results, only: %i(index create show), resources :results, only: %i(index create show)
path: 'results',
as: :results
get 'inputs', to: 'tasks#inputs' get 'inputs', to: 'tasks#inputs'
get 'outputs', to: 'tasks#outputs' get 'outputs', to: 'tasks#outputs'
get 'activities', to: 'tasks#activities' get 'activities', to: 'tasks#activities'