Added task inputs and outputs functionality

This commit is contained in:
Zanz2 2018-09-10 14:22:27 +02:00
parent e82af2186e
commit 30c4fac2ae
4 changed files with 44 additions and 11 deletions

View file

@ -7,6 +7,7 @@ module Api
before_action :load_project
before_action :load_experiment
before_action :load_task, only: :show
before_action :load_task_relative, only: %i(outputs output inputs input)
def index
tasks = @experiment.my_modules
@ -20,6 +21,32 @@ module Api
render jsonapi: @my_module, serializer: MyModuleSerializer
end
def outputs
outputs = @my_module.my_modules
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: outputs, each_serializer: MyModuleSerializer
end
def output
output = @my_module.my_modules.find(params.require(:id))
render jsonapi: {}, status: :not_found if output.nil?
render jsonapi: output, serializer: MyModuleSerializer
end
def inputs
inputs = @my_module.my_module_antecessors
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: inputs, each_serializer: MyModuleSerializer
end
def input
input = @my_module.my_module_antecessors.find(params.require(:id))
render jsonapi: {}, status: :not_found if input.nil?
render jsonapi: input, serializer: MyModuleSerializer
end
private
def load_team
@ -45,6 +72,15 @@ module Api
@my_module = @experiment.my_modules.find(params.require(:id))
render jsonapi: {}, status: :not_found if @my_module.nil?
end
# Made the method below because its more elegant than changing parameters
# in routes file, and here. It exists because when we call input or output
# for a task, the "id" that used to be task id is now an id for the output
# or input.
def load_task_relative
@my_module = @experiment.my_modules.find(params.require(:task_id))
render jsonapi: {}, status: :not_found if @my_module.nil?
end
end
end
end

View file

@ -572,17 +572,14 @@ Rails.application.routes.draw do
resources :protocols, only: %i(index show),
path: 'protocols',
as: :protocols
resources :results, only: %i(index show),
path: 'results',
as: :results
resources :my_module_antecessors, only: %i(index show),
path: 'ancestors',
as: :ancestors
resources :my_modules, only: %i(index show),
path: 'descendants',
as: :descendants
# The above two are just my modules (tasks), so im not sure
# if i should duplicate controllers and serializers
get 'inputs',
to: 'my_modules#inputs'
get 'inputs/:id',
to: 'my_modules#input'
get 'outputs',
to: 'my_modules#outputs'
get 'outputs/:id',
to: 'my_modules#output'
resources :activities, only: %i(index show),
path: 'activities',
as: :activities