Merge pull request #1297 from Zanz2/zz_tasks_api_task_2667

Created read endpoint for tasks, results and related data [SCI-2667] [SCI-2669]
This commit is contained in:
Zanz2 2018-09-24 15:31:47 +02:00 committed by GitHub
commit 8699761389
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 501 additions and 6 deletions

View file

@ -0,0 +1,59 @@
# frozen_string_literal: true
module Api
module V1
class ActivitiesController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task
before_action :load_activity, only: :show
def index
activities = @my_module.activities
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: activities,
each_serializer: ActivitySerializer
end
def show
render jsonapi: @activity, serializer: ActivitySerializer
end
private
def load_team
@team = Team.find(params.require(:team_id))
render jsonapi: {}, status: :forbidden unless can_read_team?(@team)
end
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
def load_experiment
@experiment = @project.experiments.find(params.require(:experiment_id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task
@my_module = @experiment.my_modules.find(params.require(:task_id))
render jsonapi: {}, status: :not_found if @my_module.nil?
end
def load_activity
@activity = @my_module.activities.find(
params.require(:id)
)
render jsonapi: {}, status: :not_found if @activity.nil?
end
end
end
end

View file

@ -7,7 +7,7 @@ module Api
before_action :load_inventory
before_action :load_inventory_column, only: %i(show update destroy)
before_action :check_manage_permissions, only: %i(update destroy)
before_action :check_create_permissions, only: %i(create)
def index
columns = @inventory.repository_columns
.includes(:repository_list_items)
@ -66,6 +66,12 @@ module Api
end
end
def check_create_permissions
unless can_manage_repository?(@inventory)
render body: nil, status: :forbidden
end
end
def inventory_column_params
unless params.require(:data).require(:type) == 'inventory_columns'
raise ActionController::BadRequest,

View file

@ -0,0 +1,59 @@
# frozen_string_literal: true
module Api
module V1
class MyModuleRepositoryRowsController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task
before_action :load_task_repository_row, only: :show
def index
repo_rows = @my_module.my_module_repository_rows
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: repo_rows,
each_serializer: MyModuleRepositoryRowSerializer
end
def show
render jsonapi: @repo_row, serializer: MyModuleRepositoryRowSerializer
end
private
def load_team
@team = Team.find(params.require(:team_id))
render jsonapi: {}, status: :forbidden unless can_read_team?(@team)
end
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
def load_experiment
@experiment = @project.experiments.find(params.require(:experiment_id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task
@my_module = @experiment.my_modules.find(params.require(:task_id))
render jsonapi: {}, status: :not_found if @my_module.nil?
end
def load_task_repository_row
@repo_row = @my_module.my_module_repository_rows.find(
params.require(:id)
)
render jsonapi: {}, status: :not_found if @repo_row.nil?
end
end
end
end

View file

@ -0,0 +1,59 @@
# frozen_string_literal: true
module Api
module V1
class MyModuleTagsController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task
before_action :load_task_tag, only: :show
def index
task_tags = @my_module.my_module_tags
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: task_tags,
each_serializer: MyModuleTagSerializer
end
def show
render jsonapi: @task_tag, serializer: MyModuleTagSerializer
end
private
def load_team
@team = Team.find(params.require(:team_id))
render jsonapi: {}, status: :forbidden unless can_read_team?(@team)
end
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
def load_experiment
@experiment = @project.experiments.find(params.require(:experiment_id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task
@my_module = @experiment.my_modules.find(params.require(:task_id))
render jsonapi: {}, status: :not_found if @my_module.nil?
end
def load_task_tag
@task_tag = @my_module.my_module_tags.find(
params.require(:id)
)
render jsonapi: {}, status: :not_found if @task_tag.nil?
end
end
end
end

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,30 @@ 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: 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: input, serializer: MyModuleSerializer
end
private
def load_team
@ -43,7 +68,14 @@ module Api
def load_task
@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))
end
end
end

View file

@ -0,0 +1,57 @@
# frozen_string_literal: true
module Api
module V1
class ProtocolsController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task
before_action :load_protocol, only: :show
def index
protocols = @my_module.protocols
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: protocols,
each_serializer: ProtocolSerializer
end
def show
render jsonapi: @protocol, serializer: ProtocolSerializer
end
private
def load_team
@team = Team.find(params.require(:team_id))
render jsonapi: {}, status: :forbidden unless can_read_team?(@team)
end
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
def load_experiment
@experiment = @project.experiments.find(params.require(:experiment_id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task
@my_module = @experiment.my_modules.find(params.require(:task_id))
end
def load_protocol
@protocol = @my_module.protocols.find(
params.require(:id)
)
end
end
end
end

View file

@ -0,0 +1,60 @@
# frozen_string_literal: true
module Api
module V1
class ResultsController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task
before_action :load_result, only: :show
def index
results = @my_module.results
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: results, include: %w(
result_table result_text result_asset
), each_serializer: ResultSerializer
end
def show
render jsonapi: @result, include: %w(
result_table result_text result_asset
), serializer: ResultSerializer
end
private
def load_team
@team = Team.find(params.require(:team_id))
render jsonapi: {}, status: :forbidden unless can_read_team?(@team)
end
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
def load_experiment
@experiment = @project.experiments.find(params.require(:experiment_id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task
@my_module = @experiment.my_modules.find(params.require(:task_id))
end
def load_result
@result = @my_module.results.find(
params.require(:id)
)
end
end
end
end

View file

@ -0,0 +1,57 @@
# frozen_string_literal: true
module Api
module V1
class UserMyModulesController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task
before_action :load_user_task, only: :show
def index
user_tasks = @my_module.user_my_modules
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: user_tasks,
each_serializer: UserMyModuleSerializer
end
def show
render jsonapi: @user_task, serializer: UserMyModuleSerializer
end
private
def load_team
@team = Team.find(params.require(:team_id))
render jsonapi: {}, status: :forbidden unless can_read_team?(@team)
end
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
def load_experiment
@experiment = @project.experiments.find(params.require(:experiment_id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task
@my_module = @experiment.my_modules.find(params.require(:task_id))
end
def load_user_task
@user_task = @my_module.user_my_modules.find(
params.require(:id)
)
end
end
end
end

View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Api
module V1
class ActivitySerializer < ActiveModel::Serializer
type :activities
attributes :id, :my_module_id, :user_id, :type_of, :message,
:project_id, :experiment_id
belongs_to :my_module, serializer: MyModuleSerializer
end
end
end

View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Api
module V1
class MyModuleRepositoryRowSerializer < ActiveModel::Serializer
type :task_inventory_rows
attribute :repository_row_id, key: :inventory_row_id
attribute :my_module_id, key: :task_id
belongs_to :my_module, serializer: MyModuleSerializer
end
end
end

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Api
module V1
class MyModuleTagSerializer < ActiveModel::Serializer
type :task_tags
attributes :id, :tag_id
attribute :my_module_id, key: :task_id
belongs_to :my_module, serializer: MyModuleSerializer
end
end
end

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
module Api
module V1
class ProtocolSerializer < ActiveModel::Serializer
type :protocols
attributes :id, :name, :authors, :description,
:team_id, :protocol_type,
:nr_of_linked_children
attribute :my_module_id, key: :task_id
belongs_to :my_module, serializer: MyModuleSerializer
end
end
end

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
module Api
module V1
class ResultSerializer < ActiveModel::Serializer
type :results
attributes :id, :name, :user_id, :archived, :result_text,
:result_table, :result_asset
attribute :my_module_id, key: :task_id
belongs_to :my_module, serializer: MyModuleSerializer
end
end
end

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
module Api
module V1
class UserMyModuleSerializer < ActiveModel::Serializer
type :user_tasks
attributes :id, :user_id
attribute :my_module_id, key: :task_id
belongs_to :my_module, serializer: MyModuleSerializer
end
end
end

View file

@ -558,10 +558,6 @@ Rails.application.routes.draw do
end
resources :projects, only: %i(index show) do
resources :experiments, only: %i(index show) do
resources :my_modules,
only: %i(index show),
path: 'tasks',
as: :tasks
resources :my_module_groups,
only: %i(index show),
path: 'task_groups',
@ -570,6 +566,37 @@ Rails.application.routes.draw do
only: %i(index show),
path: 'connections',
as: :connections
resources :my_modules,
only: %i(index show),
path: 'tasks',
as: :tasks do
resources :my_module_repository_rows, only: %i(index show),
path: 'task_inventory_rows',
as: :task_inventory_rows
resources :user_my_modules, only: %i(index show),
path: 'user_tasks',
as: :user_tasks
resources :my_module_tags, only: %i(index show),
path: 'task_tags',
as: :task_tags
resources :protocols, only: %i(index show),
path: 'protocols',
as: :protocols
resources :results, only: %i(index show),
path: 'results',
as: :results
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
end
end
end
end