2018-09-05 18:54:08 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
2018-10-07 15:45:24 +08:00
|
|
|
class TasksController < BaseController
|
2020-07-02 23:03:29 +08:00
|
|
|
include Api::V1::ExtraParams
|
|
|
|
|
2018-09-05 18:54:08 +08:00
|
|
|
before_action :load_team
|
|
|
|
before_action :load_project
|
|
|
|
before_action :load_experiment
|
2018-11-07 22:43:44 +08:00
|
|
|
before_action only: :show do
|
|
|
|
load_task(:id)
|
|
|
|
end
|
2020-07-02 23:03:29 +08:00
|
|
|
before_action :load_task_for_managing, only: %i(update)
|
|
|
|
before_action :load_task, only: :activities
|
2018-09-05 18:54:08 +08:00
|
|
|
|
|
|
|
def index
|
2023-07-27 08:31:57 +08:00
|
|
|
tasks =
|
2023-07-31 18:21:40 +08:00
|
|
|
timestamps_filter(
|
2023-07-27 08:31:57 +08:00
|
|
|
@experiment.my_modules
|
|
|
|
).includes(:my_module_status, :my_modules, :my_module_antecessors)
|
2023-05-19 17:34:31 +08:00
|
|
|
tasks = archived_filter(tasks).page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
2018-09-05 18:54:08 +08:00
|
|
|
|
2021-08-18 16:00:01 +08:00
|
|
|
render jsonapi: tasks, each_serializer: TaskSerializer,
|
|
|
|
include: include_params,
|
|
|
|
rte_rendering: render_rte?,
|
|
|
|
team: @team
|
2018-09-05 18:54:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2021-08-18 16:00:01 +08:00
|
|
|
render jsonapi: @task, serializer: TaskSerializer,
|
|
|
|
include: include_params,
|
|
|
|
rte_rendering: render_rte?,
|
|
|
|
team: @team
|
2018-09-05 18:54:08 +08:00
|
|
|
end
|
|
|
|
|
2019-09-16 15:31:30 +08:00
|
|
|
def create
|
2023-07-11 15:29:17 +08:00
|
|
|
raise PermissionError.new(MyModule, :create) unless can_create_experiment_tasks?(@experiment)
|
2019-09-16 15:31:30 +08:00
|
|
|
|
2021-09-30 17:32:11 +08:00
|
|
|
my_module = @experiment.my_modules.create!(task_params_create.merge(created_by: current_user))
|
2020-07-02 23:03:29 +08:00
|
|
|
|
|
|
|
render jsonapi: my_module, serializer: TaskSerializer,
|
|
|
|
rte_rendering: render_rte?,
|
|
|
|
status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-09-03 15:49:00 +08:00
|
|
|
@task.assign_attributes(task_params_update)
|
2019-09-16 15:31:30 +08:00
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
if @task.changed? && @task.save!
|
|
|
|
render jsonapi: @task, serializer: TaskSerializer, status: :ok
|
|
|
|
else
|
|
|
|
render body: nil, status: :no_content
|
|
|
|
end
|
2019-09-16 15:31:30 +08:00
|
|
|
end
|
|
|
|
|
2018-10-07 17:49:11 +08:00
|
|
|
def activities
|
2019-04-08 21:07:45 +08:00
|
|
|
activities = ActivitiesService.my_module_activities(@task)
|
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
2018-10-07 17:49:11 +08:00
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
render jsonapi: activities, each_serializer: ActivitySerializer
|
2018-10-07 17:49:11 +08:00
|
|
|
end
|
|
|
|
|
2018-09-05 18:54:08 +08:00
|
|
|
private
|
|
|
|
|
2020-09-03 15:49:00 +08:00
|
|
|
def task_params_create
|
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'tasks'
|
|
|
|
|
2020-09-15 17:19:25 +08:00
|
|
|
params.require(:data).require(:attributes).permit(%i(name x y description))
|
2020-09-03 15:49:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def task_params_update
|
2019-09-16 15:31:30 +08:00
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'tasks'
|
|
|
|
|
2020-09-15 17:19:25 +08:00
|
|
|
params.require(:data).require(:attributes).permit(%i(name x y description my_module_status_id))
|
2019-09-16 15:31:30 +08:00
|
|
|
end
|
|
|
|
|
2021-08-18 16:00:01 +08:00
|
|
|
def permitted_includes
|
|
|
|
%w(comments)
|
|
|
|
end
|
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
def load_task_for_managing
|
|
|
|
@task = @experiment.my_modules.find(params.require(:id))
|
2021-09-14 17:08:35 +08:00
|
|
|
raise PermissionError.new(MyModule, :manage) unless can_manage_my_module?(@task)
|
2018-09-10 20:22:27 +08:00
|
|
|
end
|
2018-09-05 18:54:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|