Merge pull request #1292 from Zanz2/zz_experiments_api_task_2666

Created API endpoints for experiments and related data [SCI-2666]
This commit is contained in:
Zanz2 2018-09-13 10:49:49 +02:00 committed by GitHub
commit 765ee25853
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 272 additions and 0 deletions

View file

@ -0,0 +1,59 @@
# 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
@connections = @connections.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
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

@ -0,0 +1,43 @@
# frozen_string_literal: true
module Api
module V1
class ExperimentsController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment, only: :show
def index
experiments = @project.experiments
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: experiments, each_serializer: ExperimentSerializer
end
def show
render jsonapi: @experiment, serializer: ExperimentSerializer
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(:id))
render jsonapi: {}, status: :forbidden unless can_read_experiment?(
@experiment
)
end
end
end
end

View file

@ -0,0 +1,52 @@
# frozen_string_literal: true
module Api
module V1
class MyModuleGroupsController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task_group, only: :show
def index
my_module_groups = @experiment.my_module_groups
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: my_module_groups,
each_serializer: MyModuleGroupSerializer
end
def show
render jsonapi: @my_module_group, serializer: MyModuleGroupSerializer
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_group
@my_module_group = @experiment.my_module_groups.find(
params.require(:id)
)
end
end
end
end

View file

@ -0,0 +1,50 @@
# frozen_string_literal: true
module Api
module V1
class MyModulesController < BaseController
before_action :load_team
before_action :load_project
before_action :load_experiment
before_action :load_task, only: :show
def index
tasks = @experiment.my_modules
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
render jsonapi: tasks, each_serializer: MyModuleSerializer
end
def show
render jsonapi: @my_module, serializer: MyModuleSerializer
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(:id))
render jsonapi: {}, status: :not_found if @my_module.nil?
end
end
end
end

View file

@ -0,0 +1,19 @@
# frozen_string_literal: true
module Api
module V1
class ConnectionSerializer < ActiveModel::Serializer
type :connections
attributes :id, :input_id, :output_id
has_one :input_task, serializer: MyModuleSerializer
has_one :output_task, serializer: MyModuleSerializer
def input_task
MyModule.find(object.input_id)
end
def output_task
MyModule.find(object.output_id)
end
end
end
end

View file

@ -0,0 +1,10 @@
# frozen_string_literal: true
module Api
module V1
class ExperimentSerializer < ActiveModel::Serializer
type :experiments
attributes :id, :name, :description, :archived
end
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Api
module V1
class MyModuleGroupSerializer < ActiveModel::Serializer
type :task_groups
attributes :id, :experiment_id
belongs_to :experiment, serializer: ExperimentSerializer
end
end
end

View file

@ -0,0 +1,12 @@
# frozen_string_literal: true
module Api
module V1
class MyModuleSerializer < ActiveModel::Serializer
type :tasks
attributes :id, :name, :due_date, :description, :state
attribute :my_module_group_id, key: :task_group_id
belongs_to :experiment, serializer: ExperimentSerializer
end
end
end

View file

@ -554,6 +554,22 @@ Rails.application.routes.draw do
path: 'items',
as: :items
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',
as: :task_groups
resources :connections,
only: %i(index show),
path: 'connections',
as: :connections
end
end
end
resources :users, only: %i(show) do
resources :user_identities,