From 30c4fac2aee63432bffc5fa9f1dd2755b312e190 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Mon, 10 Sep 2018 14:22:27 +0200 Subject: [PATCH] Added task inputs and outputs functionality --- .../v1/my_modules_antecessors_controller.rb | 0 .../api/v1/my_modules_controller.rb | 36 +++++++++++++++++++ .../v1/my_module_antecessors_serializer.rb | 0 config/routes.rb | 19 +++++----- 4 files changed, 44 insertions(+), 11 deletions(-) delete mode 100644 app/controllers/api/v1/my_modules_antecessors_controller.rb delete mode 100644 app/serializers/api/v1/my_module_antecessors_serializer.rb diff --git a/app/controllers/api/v1/my_modules_antecessors_controller.rb b/app/controllers/api/v1/my_modules_antecessors_controller.rb deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/controllers/api/v1/my_modules_controller.rb b/app/controllers/api/v1/my_modules_controller.rb index 5ec6ffa0c..4f1ebd7fd 100644 --- a/app/controllers/api/v1/my_modules_controller.rb +++ b/app/controllers/api/v1/my_modules_controller.rb @@ -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 diff --git a/app/serializers/api/v1/my_module_antecessors_serializer.rb b/app/serializers/api/v1/my_module_antecessors_serializer.rb deleted file mode 100644 index e69de29bb..000000000 diff --git a/config/routes.rb b/config/routes.rb index 1df86160d..6f43ca63d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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