mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-09-07 21:55:20 +08:00
Completed the functionality for experiments and related endpoints
This commit is contained in:
parent
1589cfb1b2
commit
e64aee5db0
9 changed files with 108 additions and 47 deletions
57
app/controllers/api/v1/connections_controller.rb
Normal file
57
app/controllers/api/v1/connections_controller.rb
Normal 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
|
|
@ -11,16 +11,11 @@ module Api
|
||||||
experiments = @project.experiments
|
experiments = @project.experiments
|
||||||
.page(params.dig(:page, :number))
|
.page(params.dig(:page, :number))
|
||||||
.per(params.dig(:page, :size))
|
.per(params.dig(:page, :size))
|
||||||
|
render jsonapi: experiments, each_serializer: ExperimentSerializer
|
||||||
render jsonapi: experiments,
|
|
||||||
links: { self: api_v1_team_project_experiments_path },
|
|
||||||
each_serializer: ExperimentSerializer
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
render jsonapi: @experiment,
|
render jsonapi: @experiment, serializer: ExperimentSerializer
|
||||||
links: { self: api_v1_team_project_experiment_path },
|
|
||||||
serializer: ExperimentSerializer
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -32,12 +27,16 @@ module Api
|
||||||
|
|
||||||
def load_project
|
def load_project
|
||||||
@project = @team.projects.find(params.require(:project_id))
|
@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
|
end
|
||||||
|
|
||||||
def load_experiment
|
def load_experiment
|
||||||
@experiment = @project.experiments.find(params.require(:id))
|
@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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,10 +10,11 @@ module Api
|
||||||
|
|
||||||
def index
|
def index
|
||||||
my_module_groups = @experiment.my_module_groups
|
my_module_groups = @experiment.my_module_groups
|
||||||
.page(params.dig(:page, :number))
|
.page(params.dig(:page, :number))
|
||||||
.per(params.dig(:page, :size))
|
.per(params.dig(:page, :size))
|
||||||
|
|
||||||
render jsonapi: my_module_groups, each_serializer: MyModuleGroupSerializer
|
render jsonapi: my_module_groups,
|
||||||
|
each_serializer: MyModuleGroupSerializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
@ -29,16 +30,22 @@ module Api
|
||||||
|
|
||||||
def load_project
|
def load_project
|
||||||
@project = @team.projects.find(params.require(:project_id))
|
@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
|
end
|
||||||
|
|
||||||
def load_experiment
|
def load_experiment
|
||||||
@experiment = @project.experiments.find(params.require(:experiment_id))
|
@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
|
end
|
||||||
|
|
||||||
def load_task_groups
|
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?
|
render jsonapi: {}, status: :not_found if @my_module_group.nil?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,8 +10,8 @@ module Api
|
||||||
|
|
||||||
def index
|
def index
|
||||||
tasks = @experiment.my_modules
|
tasks = @experiment.my_modules
|
||||||
.page(params.dig(:page, :number))
|
.page(params.dig(:page, :number))
|
||||||
.per(params.dig(:page, :size))
|
.per(params.dig(:page, :size))
|
||||||
|
|
||||||
render jsonapi: tasks, each_serializer: MyModuleSerializer
|
render jsonapi: tasks, each_serializer: MyModuleSerializer
|
||||||
end
|
end
|
||||||
|
@ -29,12 +29,16 @@ module Api
|
||||||
|
|
||||||
def load_project
|
def load_project
|
||||||
@project = @team.projects.find(params.require(:project_id))
|
@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
|
end
|
||||||
|
|
||||||
def load_experiment
|
def load_experiment
|
||||||
@experiment = @project.experiments.find(params.require(:experiment_id))
|
@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
|
end
|
||||||
|
|
||||||
def load_task
|
def load_task
|
||||||
|
|
10
app/serializers/api/v1/connection_serializer.rb
Normal file
10
app/serializers/api/v1/connection_serializer.rb
Normal 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
|
|
@ -6,13 +6,6 @@ module Api
|
||||||
type :experiment
|
type :experiment
|
||||||
attributes :id, :name, :description, :project_id, :created_by_id,
|
attributes :id, :name, :description, :project_id, :created_by_id,
|
||||||
:archived, :created_at, :updated_at
|
: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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,9 +3,9 @@
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class MyModuleGroupSerializer < ActiveModel::Serializer
|
class MyModuleGroupSerializer < ActiveModel::Serializer
|
||||||
type :task_group
|
type :MyModuleGroup
|
||||||
attributes :id, :created_at, :updated_at, :created_by_id, :experiment_id
|
attributes :id, :created_at, :updated_at, :created_by_id, :experiment_id
|
||||||
belongs_to :experiment
|
belongs_to :experiment, serializer: ExperimentSerializer
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,23 +3,10 @@
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
class MyModuleSerializer < ActiveModel::Serializer
|
class MyModuleSerializer < ActiveModel::Serializer
|
||||||
type :task
|
type :tasks
|
||||||
attributes :id, :name, :due_date, :description, :my_module_group_id, :nr_of_assigned_samples, :state
|
attributes :id, :name, :due_date, :description,
|
||||||
link(:self) {
|
:my_module_group_id, :nr_of_assigned_samples, :state
|
||||||
api_v1_team_project_experiment_task_url(
|
belongs_to :experiment, serializer: ExperimentSerializer
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -540,7 +540,7 @@ Rails.application.routes.draw do
|
||||||
get 'health', to: 'api#health'
|
get 'health', to: 'api#health'
|
||||||
get 'status', to: 'api#status'
|
get 'status', to: 'api#status'
|
||||||
post 'auth/token', to: 'api#authenticate'
|
post 'auth/token', to: 'api#authenticate'
|
||||||
#if Api.configuration.core_api_v1_preview
|
if Api.configuration.core_api_v1_preview
|
||||||
namespace :v1 do
|
namespace :v1 do
|
||||||
resources :teams, only: %i(index show) do
|
resources :teams, only: %i(index show) do
|
||||||
resources :inventories,
|
resources :inventories,
|
||||||
|
@ -564,6 +564,10 @@ Rails.application.routes.draw do
|
||||||
only: %i(index show),
|
only: %i(index show),
|
||||||
path: 'task_groups',
|
path: 'task_groups',
|
||||||
as: :task_groups
|
as: :task_groups
|
||||||
|
resources :connections,
|
||||||
|
only: %i(index show),
|
||||||
|
path: 'connections',
|
||||||
|
as: :connections
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
resources :users, only: %i(show) do
|
resources :users, only: %i(show) do
|
||||||
|
@ -572,7 +576,7 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
#end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue