2018-09-05 15:55:04 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class ExperimentsController < BaseController
|
|
|
|
before_action :load_team
|
|
|
|
before_action :load_project
|
2018-11-07 22:43:44 +08:00
|
|
|
before_action only: :show do
|
|
|
|
load_experiment(:id)
|
|
|
|
end
|
2020-09-17 05:15:24 +08:00
|
|
|
before_action :load_experiment_for_managing, only: %i(update)
|
2018-09-05 15:55:04 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
experiments = @project.experiments
|
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
2018-09-07 17:58:02 +08:00
|
|
|
render jsonapi: experiments, each_serializer: ExperimentSerializer
|
2018-09-05 15:55:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2018-09-07 17:58:02 +08:00
|
|
|
render jsonapi: @experiment, serializer: ExperimentSerializer
|
2018-09-05 15:55:04 +08:00
|
|
|
end
|
2020-09-17 05:15:24 +08:00
|
|
|
|
|
|
|
def create
|
|
|
|
raise PermissionError.new(Experiment, :create) unless can_create_experiments?(@project)
|
|
|
|
|
|
|
|
experiment = @project.experiments.create!(experiment_params.merge!(created_by: current_user,
|
|
|
|
last_modified_by: current_user))
|
|
|
|
|
|
|
|
render jsonapi: experiment, serializer: ExperimentSerializer, status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@experiment.assign_attributes(experiment_params)
|
|
|
|
|
|
|
|
return render body: nil, status: :no_content unless @experiment.changed?
|
|
|
|
|
|
|
|
if @experiment.archived_changed?
|
|
|
|
if @experiment.archived?
|
|
|
|
@experiment.archived_by = current_user
|
|
|
|
@experiment.archived_on = DateTime.now
|
|
|
|
else
|
|
|
|
@experiment.restored_by = current_user
|
|
|
|
@experiment.restored_on = DateTime.now
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@experiment.last_modified_by = current_user
|
|
|
|
@experiment.save!
|
|
|
|
render jsonapi: @experiment, serializer: ExperimentSerializer, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def experiment_params
|
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'experiments'
|
|
|
|
|
|
|
|
params.require(:data).require(:attributes).permit(:name, :description, :archived)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_experiment_for_managing
|
|
|
|
@experiment = @project.experiments.find(params.require(:id))
|
|
|
|
raise PermissionError.new(Experiment, :manage) unless can_manage_experiment?(@experiment)
|
|
|
|
end
|
2018-09-05 15:55:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|