Completed the functionality for experiments and related endpoints

This commit is contained in:
Zanz2 2018-09-07 11:58:02 +02:00
parent 1589cfb1b2
commit e64aee5db0
9 changed files with 108 additions and 47 deletions

View file

@ -0,0 +1,57 @@
# frozen_string_literal: true
module Api
module V1
class ConnectionsController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_connections
before_action :load_connection, only: :show
def index
render jsonapi: @connections, each_serializer: ConnectionSerializer
end
def show
render jsonapi: @connection, serializer: ConnectionSerializer
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_connections
@connections = Connection.joins(
'LEFT JOIN my_modules AS inputs ON input_id = inputs.id'
).joins(
'LEFT JOIN my_modules AS outputs ON output_id = outputs.id'
).where(
'inputs.experiment_id = ? OR outputs.experiment_id = ?',
@experiment.id, @experiment.id
)
end
def load_connection
@connection = @connections.find(params.require(:id))
end
end
end
end

View file

@ -11,16 +11,11 @@ module Api
experiments = @project.experiments
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: experiments,
links: { self: api_v1_team_project_experiments_path },
each_serializer: ExperimentSerializer
render jsonapi: experiments, each_serializer: ExperimentSerializer
end
def show
render jsonapi: @experiment,
links: { self: api_v1_team_project_experiment_path },
serializer: ExperimentSerializer
render jsonapi: @experiment, serializer: ExperimentSerializer
end
private
@ -32,12 +27,16 @@ module Api
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(@project)
render jsonapi: {}, status: :forbidden unless can_read_project?(
@project
)
end
def load_experiment
@experiment = @project.experiments.find(params.require(:id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(@experiment)
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
end
end

View file

@ -10,10 +10,11 @@ module Api
def index
my_module_groups = @experiment.my_module_groups
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: my_module_groups, each_serializer: MyModuleGroupSerializer
render jsonapi: my_module_groups,
each_serializer: MyModuleGroupSerializer
end
def show
@ -29,16 +30,22 @@ module Api
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(@project)
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)
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task_groups
@my_module_group = @experiment.my_module_groups.find(params.require(:id))
@my_module_group = @experiment.my_module_groups.find(
params.require(:id)
)
render jsonapi: {}, status: :not_found if @my_module_group.nil?
end
end

View file

@ -10,8 +10,8 @@ module Api
def index
tasks = @experiment.my_modules
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: tasks, each_serializer: MyModuleSerializer
end
@ -29,12 +29,16 @@ module Api
def load_project
@project = @team.projects.find(params.require(:project_id))
render jsonapi: {}, status: :forbidden unless can_read_project?(@project)
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)
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
def load_task

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
module Api
module V1
class ConnectionSerializer < ActiveModel::Serializer
type :connection
attributes :id, :input_id, :output_id
end
end
end

View file

@ -6,13 +6,6 @@ module Api
type :experiment
attributes :id, :name, :description, :project_id, :created_by_id,
:archived, :created_at, :updated_at
has_many :my_modules, key: :my_module,
serializer: MyModuleSerializer,
class_name: 'MyModule'
has_many :my_module_groups, key: :my_module_group,
serializer: MyModuleGroupSerializer,
class_name: 'MyModuleGroup'
#link(:related) { api_v1_team_project_experiment_path(@experiment) }
end
end
end

View file

@ -3,9 +3,9 @@
module Api
module V1
class MyModuleGroupSerializer < ActiveModel::Serializer
type :task_group
type :MyModuleGroup
attributes :id, :created_at, :updated_at, :created_by_id, :experiment_id
belongs_to :experiment
belongs_to :experiment, serializer: ExperimentSerializer
end
end
end

View file

@ -3,23 +3,10 @@
module Api
module V1
class MyModuleSerializer < ActiveModel::Serializer
type :task
attributes :id, :name, :due_date, :description, :my_module_group_id, :nr_of_assigned_samples, :state
link(:self) {
api_v1_team_project_experiment_task_url(
return_path[0], return_path[1], return_path[2], object.id
)
}
belongs_to :experiment
def return_path
return_array = []
experiment = object.experiment
project = experiment.project
team = project.team
return_array.push(experiment, project, team)
return_array
end
type :tasks
attributes :id, :name, :due_date, :description,
:my_module_group_id, :nr_of_assigned_samples, :state
belongs_to :experiment, serializer: ExperimentSerializer
end
end
end

View file

@ -540,7 +540,7 @@ Rails.application.routes.draw do
get 'health', to: 'api#health'
get 'status', to: 'api#status'
post 'auth/token', to: 'api#authenticate'
#if Api.configuration.core_api_v1_preview
if Api.configuration.core_api_v1_preview
namespace :v1 do
resources :teams, only: %i(index show) do
resources :inventories,
@ -564,6 +564,10 @@ Rails.application.routes.draw do
only: %i(index show),
path: 'task_groups',
as: :task_groups
resources :connections,
only: %i(index show),
path: 'connections',
as: :connections
end
end
resources :users, only: %i(show) do
@ -572,7 +576,7 @@ Rails.application.routes.draw do
end
end
end
#end
end
end
end