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
|
2018-09-06 21:38:48 +08:00
|
|
|
tasks = @experiment.my_modules
|
2020-09-02 17:43:00 +08:00
|
|
|
.includes(:my_module_status, :my_modules, :my_module_antecessors)
|
2018-09-07 17:58:02 +08:00
|
|
|
.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
|
|
|
|
raise PermissionError.new(MyModule, :create) unless can_manage_experiment?(@experiment)
|
|
|
|
|
2020-09-03 15:49:00 +08:00
|
|
|
my_module = @experiment.my_modules.create!(task_params_create)
|
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))
|
|
|
|
raise PermissionError.new(MyModule, :manage) unless can_manage_module?(@task)
|
2018-09-10 20:22:27 +08:00
|
|
|
end
|
2018-09-05 18:54:08 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|