mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-11-08 07:21:03 +08:00
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:
commit
765ee25853
9 changed files with 272 additions and 0 deletions
59
app/controllers/api/v1/connections_controller.rb
Normal file
59
app/controllers/api/v1/connections_controller.rb
Normal 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
|
||||
43
app/controllers/api/v1/experiments_controller.rb
Normal file
43
app/controllers/api/v1/experiments_controller.rb
Normal 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
|
||||
52
app/controllers/api/v1/my_module_groups_controller.rb
Normal file
52
app/controllers/api/v1/my_module_groups_controller.rb
Normal 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
|
||||
50
app/controllers/api/v1/my_modules_controller.rb
Normal file
50
app/controllers/api/v1/my_modules_controller.rb
Normal 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
|
||||
19
app/serializers/api/v1/connection_serializer.rb
Normal file
19
app/serializers/api/v1/connection_serializer.rb
Normal 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
|
||||
10
app/serializers/api/v1/experiment_serializer.rb
Normal file
10
app/serializers/api/v1/experiment_serializer.rb
Normal 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
|
||||
11
app/serializers/api/v1/my_module_group_serializer.rb
Normal file
11
app/serializers/api/v1/my_module_group_serializer.rb
Normal 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
|
||||
12
app/serializers/api/v1/my_module_serializer.rb
Normal file
12
app/serializers/api/v1/my_module_serializer.rb
Normal 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
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue