diff --git a/app/controllers/api/v1/activities_controller.rb b/app/controllers/api/v1/activities_controller.rb deleted file mode 100644 index 125c44c86..000000000 --- a/app/controllers/api/v1/activities_controller.rb +++ /dev/null @@ -1,59 +0,0 @@ -# frozen_string_literal: true - -module Api - module V1 - class ActivitiesController < BaseController - before_action :load_team - before_action :load_project - before_action :load_experiment - before_action :load_task - before_action :load_activity, only: :show - - def index - activities = @my_module.activities - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) - - render jsonapi: activities, - each_serializer: ActivitySerializer - end - - def show - render jsonapi: @activity, serializer: ActivitySerializer - 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(:task_id)) - render jsonapi: {}, status: :not_found if @my_module.nil? - end - - def load_activity - @activity = @my_module.activities.find( - params.require(:id) - ) - render jsonapi: {}, status: :not_found if @activity.nil? - end - end - end -end diff --git a/app/controllers/api/v1/tasks_controller.rb b/app/controllers/api/v1/tasks_controller.rb index 562af7919..9ab8ed1ea 100644 --- a/app/controllers/api/v1/tasks_controller.rb +++ b/app/controllers/api/v1/tasks_controller.rb @@ -7,7 +7,7 @@ module Api before_action :load_project before_action :load_experiment before_action :load_task, only: :show - before_action :load_task_relative, only: %i(inputs outputs) + before_action :load_task_relative, only: %i(inputs outputs activities) def index tasks = @experiment.my_modules @@ -35,6 +35,15 @@ module Api render jsonapi: outputs, each_serializer: TaskSerializer end + def activities + activities = @task.activities + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: activities, + each_serializer: ActivitySerializer + end + private def load_team diff --git a/app/serializers/api/v1/activity_serializer.rb b/app/serializers/api/v1/activity_serializer.rb index 28910b7f5..251bc3711 100644 --- a/app/serializers/api/v1/activity_serializer.rb +++ b/app/serializers/api/v1/activity_serializer.rb @@ -4,9 +4,15 @@ module Api module V1 class ActivitySerializer < ActiveModel::Serializer type :activities - attributes :id, :my_module_id, :user_id, :type_of, :message, - :project_id, :experiment_id - belongs_to :my_module, serializer: TaskSerializer + attributes :id, :type_of, :message + belongs_to :project, serializer: ProjectSerializer + belongs_to :experiment, serializer: TaskSerializer, + if: -> { object.experiment.present? } + belongs_to :my_module, key: :task, + serializer: TaskSerializer, + class_name: 'MyModule', + if: -> { object.my_module.present? } + belongs_to :user, serializer: UserSerializer end end end diff --git a/app/serializers/api/v1/project_serializer.rb b/app/serializers/api/v1/project_serializer.rb new file mode 100644 index 000000000..db413ece6 --- /dev/null +++ b/app/serializers/api/v1/project_serializer.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectSerializer < ActiveModel::Serializer + type :projects + attributes :id + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 067ac2663..f4440c769 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -581,9 +581,7 @@ Rails.application.routes.draw do as: :results get 'inputs', to: 'tasks#inputs' get 'outputs', to: 'tasks#outputs' - resources :activities, only: %i(index show), - path: 'activities', - as: :activities + get 'activities', to: 'tasks#activities' end end end