From 83717e8655ce5222ed8c7642bd8b057692f1e3e7 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Mon, 10 Sep 2018 16:20:02 +0200 Subject: [PATCH 01/40] created base projects endpoint --- app/controllers/api/v1/projects_controller.rb | 37 +++++++++++++++++++ app/serializers/api/v1/project_serializer.rb | 15 ++++++++ config/routes.rb | 3 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/projects_controller.rb create mode 100644 app/serializers/api/v1/project_serializer.rb diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb new file mode 100644 index 000000000..ce0e73068 --- /dev/null +++ b/app/controllers/api/v1/projects_controller.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectsController < BaseController + before_action :load_team + before_action :load_project, only: :show + + + def index + projects = @team.projects + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: projects, each_serializer: ProjectSerializer + end + + def show + render jsonapi: @project, serializer: ProjectSerializer + 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(:id)) + render jsonapi: {}, status: :forbidden unless can_read_project?( + @project + ) + end + 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..b1d9dede8 --- /dev/null +++ b/app/serializers/api/v1/project_serializer.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectSerializer < ActiveModel::Serializer + type :projects + attributes :id, :name, :visibility, :due_date, :team_id, :created_at, + :updated_at, :archived, :archived_on, :created_by_id, + :last_modified_by_id, :archived_by_id, :restored_by_id, + :restored_on, :experiments_order + + belongs_to :team, serializer: TeamSerializer + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 5712f8a9a..21567b83b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -554,7 +554,8 @@ Rails.application.routes.draw do path: 'items', as: :items end - resources :projects, only: %i(index show) do + resources :projects, only: %i(index show), path: 'projects', + as: :projects do resources :experiments, only: %i(index show) do resources :my_modules, only: %i(index show), From 44c3784977eed2a6f5871a36582a0f835951c956 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 09:13:17 +0200 Subject: [PATCH 02/40] Created base endpoints for projects and related models --- .../api/v1/activities_controller.rb | 82 +++++++++++++++++++ .../api/v1/project_comments_controller.rb | 43 ++++++++++ app/controllers/api/v1/projects_controller.rb | 1 - app/controllers/api/v1/reports_controller.rb | 42 ++++++++++ .../api/v1/user_projects_controller.rb | 42 ++++++++++ app/serializers/api/v1/activity_serializer.rb | 14 ++++ .../api/v1/project_comment_serializer.rb | 13 +++ app/serializers/api/v1/report_serializer.rb | 13 +++ .../api/v1/user_project_serializer.rb | 13 +++ config/routes.rb | 8 ++ 10 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/activities_controller.rb create mode 100644 app/controllers/api/v1/project_comments_controller.rb create mode 100644 app/controllers/api/v1/reports_controller.rb create mode 100644 app/controllers/api/v1/user_projects_controller.rb create mode 100644 app/serializers/api/v1/activity_serializer.rb create mode 100644 app/serializers/api/v1/project_comment_serializer.rb create mode 100644 app/serializers/api/v1/report_serializer.rb create mode 100644 app/serializers/api/v1/user_project_serializer.rb diff --git a/app/controllers/api/v1/activities_controller.rb b/app/controllers/api/v1/activities_controller.rb new file mode 100644 index 000000000..474e9726d --- /dev/null +++ b/app/controllers/api/v1/activities_controller.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +module Api + module V1 + class ActivitiesController < BaseController + before_action :load_team + before_action :load_project + before_action :load_project_activity, only: :project_activity + before_action :load_experiment, except: %i( + project_activities project_activity + ) + before_action :load_task, except: %i(project_activities project_activity) + 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 + + def project_activities + activities = @project.activities + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: activities, + each_serializer: ActivitySerializer + end + + def project_activity + render jsonapi: @project_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 + + def load_project_activity + @project_activity = @project.activities.find( + params.require(:id) + ) + render jsonapi: {}, status: :not_found if @project_activity.nil? + end + end + end +end diff --git a/app/controllers/api/v1/project_comments_controller.rb b/app/controllers/api/v1/project_comments_controller.rb new file mode 100644 index 000000000..bca71a462 --- /dev/null +++ b/app/controllers/api/v1/project_comments_controller.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectCommentsController < BaseController + before_action :load_team + before_action :load_project + before_action :load_project_comment, only: :show + + def index + project_comments = @project.project_comments + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: project_comments, + each_serializer: ProjectCommentSerializer + end + + def show + render jsonapi: @project_comment, serializer: ProjectCommentSerializer + 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_project_comment + @project_comment = @project.project_comments.find(params.require(:id)) + render jsonapi: {}, status: :not_found if @project_comment.nil? + end + end + end +end diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index ce0e73068..0cc5c05d0 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -6,7 +6,6 @@ module Api before_action :load_team before_action :load_project, only: :show - def index projects = @team.projects .page(params.dig(:page, :number)) diff --git a/app/controllers/api/v1/reports_controller.rb b/app/controllers/api/v1/reports_controller.rb new file mode 100644 index 000000000..01f527f53 --- /dev/null +++ b/app/controllers/api/v1/reports_controller.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Api + module V1 + class ReportsController < BaseController + before_action :load_team + before_action :load_project + before_action :load_report, only: :show + + def index + reports = @project.reports + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: reports, each_serializer: ReportSerializer + end + + def show + render jsonapi: @report, serializer: ReportSerializer + 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_report + @report = @project.reports.find(params.require(:id)) + render jsonapi: {}, status: :not_found if @report.nil? + end + end + end +end diff --git a/app/controllers/api/v1/user_projects_controller.rb b/app/controllers/api/v1/user_projects_controller.rb new file mode 100644 index 000000000..36e3eca2b --- /dev/null +++ b/app/controllers/api/v1/user_projects_controller.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Api + module V1 + class UserProjectsController < BaseController + before_action :load_team + before_action :load_project + before_action :load_user_project, only: :show + + def index + user_projects = @project.user_projects + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: user_projects, each_serializer: UserProjectSerializer + end + + def show + render jsonapi: @user_project, serializer: UserProjectSerializer + 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_user_project + @user_project = @project.user_projects.find(params.require(:id)) + render jsonapi: {}, status: :not_found if @user_project.nil? + end + end + end +end diff --git a/app/serializers/api/v1/activity_serializer.rb b/app/serializers/api/v1/activity_serializer.rb new file mode 100644 index 000000000..50b614923 --- /dev/null +++ b/app/serializers/api/v1/activity_serializer.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Api + module V1 + class ActivitySerializer < ActiveModel::Serializer + type :activities + attributes :id, :my_module_id, :user_id, :type_of, :message, :created_at, + :updated_at, :project_id, :experiment_id + belongs_to :my_module, serializer: MyModuleSerializer, optional: true + belongs_to :experiment, serializer: ExperimentSerializer, optional: true + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/app/serializers/api/v1/project_comment_serializer.rb b/app/serializers/api/v1/project_comment_serializer.rb new file mode 100644 index 000000000..b62ebebef --- /dev/null +++ b/app/serializers/api/v1/project_comment_serializer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectCommentSerializer < ActiveModel::Serializer + type :project_comments + attributes :id, :message, :user_id, :created_at, :updated_at, + :last_modified_by_id, :type, :associated_id + + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/app/serializers/api/v1/report_serializer.rb b/app/serializers/api/v1/report_serializer.rb new file mode 100644 index 000000000..0abff7e7e --- /dev/null +++ b/app/serializers/api/v1/report_serializer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Api + module V1 + class ReportSerializer < ActiveModel::Serializer + type :reports + attributes :id, :name, :description, :project_id, :user_id, :created_at, + :updated_at, :last_modified_by_id, :team_id + + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/app/serializers/api/v1/user_project_serializer.rb b/app/serializers/api/v1/user_project_serializer.rb new file mode 100644 index 000000000..ba2b1899e --- /dev/null +++ b/app/serializers/api/v1/user_project_serializer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Api + module V1 + class UserProjectSerializer < ActiveModel::Serializer + type :user_projects + attributes :id, :role, :user_id, :project_id, :created_at, :updated_at, + :assigned_by_id + + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 21567b83b..45a35c544 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -556,6 +556,14 @@ Rails.application.routes.draw do end resources :projects, only: %i(index show), path: 'projects', as: :projects do + resources :user_projects, only: %i(index show), + path: 'user_projects', as: :user_projects + resources :project_comments, only: %i(index show), + path: 'project_comments', as: :project_comments + resources :reports, only: %i(index show), + path: 'reports', as: :reports + get 'activities', to: 'activities#project_activities' + get 'activities/:id', to: 'activities#project_activity' resources :experiments, only: %i(index show) do resources :my_modules, only: %i(index show), From 94a7868d6e16133f71ab41da5faf8eb04456f222 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 09:16:59 +0200 Subject: [PATCH 03/40] Fixed hound --- app/controllers/api/v1/user_projects_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/user_projects_controller.rb b/app/controllers/api/v1/user_projects_controller.rb index 36e3eca2b..dfa376a07 100644 --- a/app/controllers/api/v1/user_projects_controller.rb +++ b/app/controllers/api/v1/user_projects_controller.rb @@ -9,8 +9,8 @@ module Api def index user_projects = @project.user_projects - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) render jsonapi: user_projects, each_serializer: UserProjectSerializer end From 43e6524392697a3ff8bb55896359686c072d7f16 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 10:31:16 +0200 Subject: [PATCH 04/40] standardized serializers by removing specified attributes --- app/serializers/api/v1/activity_serializer.rb | 4 ++-- app/serializers/api/v1/experiment_serializer.rb | 3 +-- app/serializers/api/v1/my_module_group_serializer.rb | 2 +- app/serializers/api/v1/project_comment_serializer.rb | 3 +-- app/serializers/api/v1/project_serializer.rb | 6 ++---- app/serializers/api/v1/report_serializer.rb | 3 +-- app/serializers/api/v1/user_project_serializer.rb | 3 +-- 7 files changed, 9 insertions(+), 15 deletions(-) diff --git a/app/serializers/api/v1/activity_serializer.rb b/app/serializers/api/v1/activity_serializer.rb index 50b614923..e69138827 100644 --- a/app/serializers/api/v1/activity_serializer.rb +++ b/app/serializers/api/v1/activity_serializer.rb @@ -4,8 +4,8 @@ module Api module V1 class ActivitySerializer < ActiveModel::Serializer type :activities - attributes :id, :my_module_id, :user_id, :type_of, :message, :created_at, - :updated_at, :project_id, :experiment_id + attributes :id, :my_module_id, :user_id, :type_of, :message, + :project_id, :experiment_id belongs_to :my_module, serializer: MyModuleSerializer, optional: true belongs_to :experiment, serializer: ExperimentSerializer, optional: true belongs_to :project, serializer: ProjectSerializer diff --git a/app/serializers/api/v1/experiment_serializer.rb b/app/serializers/api/v1/experiment_serializer.rb index db9c0dd7e..b1ef9023e 100644 --- a/app/serializers/api/v1/experiment_serializer.rb +++ b/app/serializers/api/v1/experiment_serializer.rb @@ -4,8 +4,7 @@ module Api module V1 class ExperimentSerializer < ActiveModel::Serializer type :experiments - attributes :id, :name, :description, :project_id, :created_by_id, - :archived, :created_at, :updated_at + attributes :id, :name, :description, :created_by_id, :archived end end end diff --git a/app/serializers/api/v1/my_module_group_serializer.rb b/app/serializers/api/v1/my_module_group_serializer.rb index 0ec8e2dca..4a1d5c46b 100644 --- a/app/serializers/api/v1/my_module_group_serializer.rb +++ b/app/serializers/api/v1/my_module_group_serializer.rb @@ -4,7 +4,7 @@ module Api module V1 class MyModuleGroupSerializer < ActiveModel::Serializer type :task_groups - attributes :id, :created_at, :updated_at, :created_by_id, :experiment_id + attributes :id, :created_by_id, :experiment_id belongs_to :experiment, serializer: ExperimentSerializer end end diff --git a/app/serializers/api/v1/project_comment_serializer.rb b/app/serializers/api/v1/project_comment_serializer.rb index b62ebebef..a9786f316 100644 --- a/app/serializers/api/v1/project_comment_serializer.rb +++ b/app/serializers/api/v1/project_comment_serializer.rb @@ -4,8 +4,7 @@ module Api module V1 class ProjectCommentSerializer < ActiveModel::Serializer type :project_comments - attributes :id, :message, :user_id, :created_at, :updated_at, - :last_modified_by_id, :type, :associated_id + attributes :id, :message, :user_id, :type, :associated_id belongs_to :project, serializer: ProjectSerializer end diff --git a/app/serializers/api/v1/project_serializer.rb b/app/serializers/api/v1/project_serializer.rb index b1d9dede8..0d31b26aa 100644 --- a/app/serializers/api/v1/project_serializer.rb +++ b/app/serializers/api/v1/project_serializer.rb @@ -4,10 +4,8 @@ module Api module V1 class ProjectSerializer < ActiveModel::Serializer type :projects - attributes :id, :name, :visibility, :due_date, :team_id, :created_at, - :updated_at, :archived, :archived_on, :created_by_id, - :last_modified_by_id, :archived_by_id, :restored_by_id, - :restored_on, :experiments_order + attributes :id, :name, :visibility, :due_date, + :archived, :experiments_order belongs_to :team, serializer: TeamSerializer end diff --git a/app/serializers/api/v1/report_serializer.rb b/app/serializers/api/v1/report_serializer.rb index 0abff7e7e..1d4d2509f 100644 --- a/app/serializers/api/v1/report_serializer.rb +++ b/app/serializers/api/v1/report_serializer.rb @@ -4,8 +4,7 @@ module Api module V1 class ReportSerializer < ActiveModel::Serializer type :reports - attributes :id, :name, :description, :project_id, :user_id, :created_at, - :updated_at, :last_modified_by_id, :team_id + attributes :id, :name, :description, :project_id belongs_to :project, serializer: ProjectSerializer end diff --git a/app/serializers/api/v1/user_project_serializer.rb b/app/serializers/api/v1/user_project_serializer.rb index ba2b1899e..e6b2dfbfe 100644 --- a/app/serializers/api/v1/user_project_serializer.rb +++ b/app/serializers/api/v1/user_project_serializer.rb @@ -4,8 +4,7 @@ module Api module V1 class UserProjectSerializer < ActiveModel::Serializer type :user_projects - attributes :id, :role, :user_id, :project_id, :created_at, :updated_at, - :assigned_by_id + attributes :id, :role, :user_id belongs_to :project, serializer: ProjectSerializer end From 6c51a14292b28bd637c3c60dace001be5c640257 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 13:04:40 +0200 Subject: [PATCH 05/40] Fixed oversight, added paging to connections --- app/controllers/api/v1/connections_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/v1/connections_controller.rb b/app/controllers/api/v1/connections_controller.rb index 740593a32..1ce2111f4 100644 --- a/app/controllers/api/v1/connections_controller.rb +++ b/app/controllers/api/v1/connections_controller.rb @@ -10,6 +10,8 @@ module Api before_action :load_connection, only: :show def index + @connections = @connections.page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) render jsonapi: @connections, each_serializer: ConnectionSerializer end From a086d181f58b707f0da2d76a1a46d08c663b7ec5 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 15:37:42 +0200 Subject: [PATCH 06/40] removing attributes --- app/serializers/api/v1/experiment_serializer.rb | 2 +- app/serializers/api/v1/my_module_group_serializer.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/serializers/api/v1/experiment_serializer.rb b/app/serializers/api/v1/experiment_serializer.rb index b1ef9023e..bff94ffc1 100644 --- a/app/serializers/api/v1/experiment_serializer.rb +++ b/app/serializers/api/v1/experiment_serializer.rb @@ -4,7 +4,7 @@ module Api module V1 class ExperimentSerializer < ActiveModel::Serializer type :experiments - attributes :id, :name, :description, :created_by_id, :archived + attributes :id, :name, :description, :archived end end end diff --git a/app/serializers/api/v1/my_module_group_serializer.rb b/app/serializers/api/v1/my_module_group_serializer.rb index 4a1d5c46b..06b52e31e 100644 --- a/app/serializers/api/v1/my_module_group_serializer.rb +++ b/app/serializers/api/v1/my_module_group_serializer.rb @@ -4,7 +4,7 @@ module Api module V1 class MyModuleGroupSerializer < ActiveModel::Serializer type :task_groups - attributes :id, :created_by_id, :experiment_id + attributes :id, :experiment_id belongs_to :experiment, serializer: ExperimentSerializer end end From dace81f72426ab43bbbc61d6b167ec1c30b3f74b Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Mon, 10 Sep 2018 16:20:02 +0200 Subject: [PATCH 07/40] created base projects endpoint --- app/controllers/api/v1/projects_controller.rb | 37 +++++++++++++++++++ app/serializers/api/v1/project_serializer.rb | 15 ++++++++ config/routes.rb | 3 +- 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/projects_controller.rb create mode 100644 app/serializers/api/v1/project_serializer.rb diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb new file mode 100644 index 000000000..ce0e73068 --- /dev/null +++ b/app/controllers/api/v1/projects_controller.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectsController < BaseController + before_action :load_team + before_action :load_project, only: :show + + + def index + projects = @team.projects + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: projects, each_serializer: ProjectSerializer + end + + def show + render jsonapi: @project, serializer: ProjectSerializer + 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(:id)) + render jsonapi: {}, status: :forbidden unless can_read_project?( + @project + ) + end + 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..b1d9dede8 --- /dev/null +++ b/app/serializers/api/v1/project_serializer.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectSerializer < ActiveModel::Serializer + type :projects + attributes :id, :name, :visibility, :due_date, :team_id, :created_at, + :updated_at, :archived, :archived_on, :created_by_id, + :last_modified_by_id, :archived_by_id, :restored_by_id, + :restored_on, :experiments_order + + belongs_to :team, serializer: TeamSerializer + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 5712f8a9a..21567b83b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -554,7 +554,8 @@ Rails.application.routes.draw do path: 'items', as: :items end - resources :projects, only: %i(index show) do + resources :projects, only: %i(index show), path: 'projects', + as: :projects do resources :experiments, only: %i(index show) do resources :my_modules, only: %i(index show), From 7f704c07e3e0de9f4045526bc9259f01c7671083 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 09:13:17 +0200 Subject: [PATCH 08/40] Created base endpoints for projects and related models --- .../api/v1/activities_controller.rb | 82 +++++++++++++++++++ .../api/v1/project_comments_controller.rb | 43 ++++++++++ app/controllers/api/v1/projects_controller.rb | 1 - app/controllers/api/v1/reports_controller.rb | 42 ++++++++++ .../api/v1/user_projects_controller.rb | 42 ++++++++++ app/serializers/api/v1/activity_serializer.rb | 14 ++++ .../api/v1/project_comment_serializer.rb | 13 +++ app/serializers/api/v1/report_serializer.rb | 13 +++ .../api/v1/user_project_serializer.rb | 13 +++ config/routes.rb | 8 ++ 10 files changed, 270 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v1/activities_controller.rb create mode 100644 app/controllers/api/v1/project_comments_controller.rb create mode 100644 app/controllers/api/v1/reports_controller.rb create mode 100644 app/controllers/api/v1/user_projects_controller.rb create mode 100644 app/serializers/api/v1/activity_serializer.rb create mode 100644 app/serializers/api/v1/project_comment_serializer.rb create mode 100644 app/serializers/api/v1/report_serializer.rb create mode 100644 app/serializers/api/v1/user_project_serializer.rb diff --git a/app/controllers/api/v1/activities_controller.rb b/app/controllers/api/v1/activities_controller.rb new file mode 100644 index 000000000..474e9726d --- /dev/null +++ b/app/controllers/api/v1/activities_controller.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +module Api + module V1 + class ActivitiesController < BaseController + before_action :load_team + before_action :load_project + before_action :load_project_activity, only: :project_activity + before_action :load_experiment, except: %i( + project_activities project_activity + ) + before_action :load_task, except: %i(project_activities project_activity) + 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 + + def project_activities + activities = @project.activities + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: activities, + each_serializer: ActivitySerializer + end + + def project_activity + render jsonapi: @project_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 + + def load_project_activity + @project_activity = @project.activities.find( + params.require(:id) + ) + render jsonapi: {}, status: :not_found if @project_activity.nil? + end + end + end +end diff --git a/app/controllers/api/v1/project_comments_controller.rb b/app/controllers/api/v1/project_comments_controller.rb new file mode 100644 index 000000000..bca71a462 --- /dev/null +++ b/app/controllers/api/v1/project_comments_controller.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectCommentsController < BaseController + before_action :load_team + before_action :load_project + before_action :load_project_comment, only: :show + + def index + project_comments = @project.project_comments + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: project_comments, + each_serializer: ProjectCommentSerializer + end + + def show + render jsonapi: @project_comment, serializer: ProjectCommentSerializer + 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_project_comment + @project_comment = @project.project_comments.find(params.require(:id)) + render jsonapi: {}, status: :not_found if @project_comment.nil? + end + end + end +end diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index ce0e73068..0cc5c05d0 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -6,7 +6,6 @@ module Api before_action :load_team before_action :load_project, only: :show - def index projects = @team.projects .page(params.dig(:page, :number)) diff --git a/app/controllers/api/v1/reports_controller.rb b/app/controllers/api/v1/reports_controller.rb new file mode 100644 index 000000000..01f527f53 --- /dev/null +++ b/app/controllers/api/v1/reports_controller.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Api + module V1 + class ReportsController < BaseController + before_action :load_team + before_action :load_project + before_action :load_report, only: :show + + def index + reports = @project.reports + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: reports, each_serializer: ReportSerializer + end + + def show + render jsonapi: @report, serializer: ReportSerializer + 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_report + @report = @project.reports.find(params.require(:id)) + render jsonapi: {}, status: :not_found if @report.nil? + end + end + end +end diff --git a/app/controllers/api/v1/user_projects_controller.rb b/app/controllers/api/v1/user_projects_controller.rb new file mode 100644 index 000000000..36e3eca2b --- /dev/null +++ b/app/controllers/api/v1/user_projects_controller.rb @@ -0,0 +1,42 @@ +# frozen_string_literal: true + +module Api + module V1 + class UserProjectsController < BaseController + before_action :load_team + before_action :load_project + before_action :load_user_project, only: :show + + def index + user_projects = @project.user_projects + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + + render jsonapi: user_projects, each_serializer: UserProjectSerializer + end + + def show + render jsonapi: @user_project, serializer: UserProjectSerializer + 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_user_project + @user_project = @project.user_projects.find(params.require(:id)) + render jsonapi: {}, status: :not_found if @user_project.nil? + end + end + end +end diff --git a/app/serializers/api/v1/activity_serializer.rb b/app/serializers/api/v1/activity_serializer.rb new file mode 100644 index 000000000..50b614923 --- /dev/null +++ b/app/serializers/api/v1/activity_serializer.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Api + module V1 + class ActivitySerializer < ActiveModel::Serializer + type :activities + attributes :id, :my_module_id, :user_id, :type_of, :message, :created_at, + :updated_at, :project_id, :experiment_id + belongs_to :my_module, serializer: MyModuleSerializer, optional: true + belongs_to :experiment, serializer: ExperimentSerializer, optional: true + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/app/serializers/api/v1/project_comment_serializer.rb b/app/serializers/api/v1/project_comment_serializer.rb new file mode 100644 index 000000000..b62ebebef --- /dev/null +++ b/app/serializers/api/v1/project_comment_serializer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProjectCommentSerializer < ActiveModel::Serializer + type :project_comments + attributes :id, :message, :user_id, :created_at, :updated_at, + :last_modified_by_id, :type, :associated_id + + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/app/serializers/api/v1/report_serializer.rb b/app/serializers/api/v1/report_serializer.rb new file mode 100644 index 000000000..0abff7e7e --- /dev/null +++ b/app/serializers/api/v1/report_serializer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Api + module V1 + class ReportSerializer < ActiveModel::Serializer + type :reports + attributes :id, :name, :description, :project_id, :user_id, :created_at, + :updated_at, :last_modified_by_id, :team_id + + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/app/serializers/api/v1/user_project_serializer.rb b/app/serializers/api/v1/user_project_serializer.rb new file mode 100644 index 000000000..ba2b1899e --- /dev/null +++ b/app/serializers/api/v1/user_project_serializer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Api + module V1 + class UserProjectSerializer < ActiveModel::Serializer + type :user_projects + attributes :id, :role, :user_id, :project_id, :created_at, :updated_at, + :assigned_by_id + + belongs_to :project, serializer: ProjectSerializer + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 21567b83b..45a35c544 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -556,6 +556,14 @@ Rails.application.routes.draw do end resources :projects, only: %i(index show), path: 'projects', as: :projects do + resources :user_projects, only: %i(index show), + path: 'user_projects', as: :user_projects + resources :project_comments, only: %i(index show), + path: 'project_comments', as: :project_comments + resources :reports, only: %i(index show), + path: 'reports', as: :reports + get 'activities', to: 'activities#project_activities' + get 'activities/:id', to: 'activities#project_activity' resources :experiments, only: %i(index show) do resources :my_modules, only: %i(index show), From 8ac1445acec95e8c43aea5122834733f2dfddc87 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 09:16:59 +0200 Subject: [PATCH 09/40] Fixed hound --- app/controllers/api/v1/user_projects_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/user_projects_controller.rb b/app/controllers/api/v1/user_projects_controller.rb index 36e3eca2b..dfa376a07 100644 --- a/app/controllers/api/v1/user_projects_controller.rb +++ b/app/controllers/api/v1/user_projects_controller.rb @@ -9,8 +9,8 @@ module Api def index user_projects = @project.user_projects - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) render jsonapi: user_projects, each_serializer: UserProjectSerializer end From 46d33d28a37817e79fdb02eda88f9ee9135a3413 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Tue, 11 Sep 2018 10:31:16 +0200 Subject: [PATCH 10/40] standardized serializers by removing specified attributes --- app/serializers/api/v1/activity_serializer.rb | 4 ++-- app/serializers/api/v1/project_comment_serializer.rb | 3 +-- app/serializers/api/v1/project_serializer.rb | 6 ++---- app/serializers/api/v1/report_serializer.rb | 3 +-- app/serializers/api/v1/user_project_serializer.rb | 3 +-- 5 files changed, 7 insertions(+), 12 deletions(-) diff --git a/app/serializers/api/v1/activity_serializer.rb b/app/serializers/api/v1/activity_serializer.rb index 50b614923..e69138827 100644 --- a/app/serializers/api/v1/activity_serializer.rb +++ b/app/serializers/api/v1/activity_serializer.rb @@ -4,8 +4,8 @@ module Api module V1 class ActivitySerializer < ActiveModel::Serializer type :activities - attributes :id, :my_module_id, :user_id, :type_of, :message, :created_at, - :updated_at, :project_id, :experiment_id + attributes :id, :my_module_id, :user_id, :type_of, :message, + :project_id, :experiment_id belongs_to :my_module, serializer: MyModuleSerializer, optional: true belongs_to :experiment, serializer: ExperimentSerializer, optional: true belongs_to :project, serializer: ProjectSerializer diff --git a/app/serializers/api/v1/project_comment_serializer.rb b/app/serializers/api/v1/project_comment_serializer.rb index b62ebebef..a9786f316 100644 --- a/app/serializers/api/v1/project_comment_serializer.rb +++ b/app/serializers/api/v1/project_comment_serializer.rb @@ -4,8 +4,7 @@ module Api module V1 class ProjectCommentSerializer < ActiveModel::Serializer type :project_comments - attributes :id, :message, :user_id, :created_at, :updated_at, - :last_modified_by_id, :type, :associated_id + attributes :id, :message, :user_id, :type, :associated_id belongs_to :project, serializer: ProjectSerializer end diff --git a/app/serializers/api/v1/project_serializer.rb b/app/serializers/api/v1/project_serializer.rb index b1d9dede8..0d31b26aa 100644 --- a/app/serializers/api/v1/project_serializer.rb +++ b/app/serializers/api/v1/project_serializer.rb @@ -4,10 +4,8 @@ module Api module V1 class ProjectSerializer < ActiveModel::Serializer type :projects - attributes :id, :name, :visibility, :due_date, :team_id, :created_at, - :updated_at, :archived, :archived_on, :created_by_id, - :last_modified_by_id, :archived_by_id, :restored_by_id, - :restored_on, :experiments_order + attributes :id, :name, :visibility, :due_date, + :archived, :experiments_order belongs_to :team, serializer: TeamSerializer end diff --git a/app/serializers/api/v1/report_serializer.rb b/app/serializers/api/v1/report_serializer.rb index 0abff7e7e..1d4d2509f 100644 --- a/app/serializers/api/v1/report_serializer.rb +++ b/app/serializers/api/v1/report_serializer.rb @@ -4,8 +4,7 @@ module Api module V1 class ReportSerializer < ActiveModel::Serializer type :reports - attributes :id, :name, :description, :project_id, :user_id, :created_at, - :updated_at, :last_modified_by_id, :team_id + attributes :id, :name, :description, :project_id belongs_to :project, serializer: ProjectSerializer end diff --git a/app/serializers/api/v1/user_project_serializer.rb b/app/serializers/api/v1/user_project_serializer.rb index ba2b1899e..e6b2dfbfe 100644 --- a/app/serializers/api/v1/user_project_serializer.rb +++ b/app/serializers/api/v1/user_project_serializer.rb @@ -4,8 +4,7 @@ module Api module V1 class UserProjectSerializer < ActiveModel::Serializer type :user_projects - attributes :id, :role, :user_id, :project_id, :created_at, :updated_at, - :assigned_by_id + attributes :id, :role, :user_id belongs_to :project, serializer: ProjectSerializer end From 81115f411aba45f8d2f3feb1df0bf234e861e954 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Thu, 13 Sep 2018 11:46:25 +0200 Subject: [PATCH 11/40] Renaming my module attribute to tasks --- app/serializers/api/v1/activity_serializer.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/serializers/api/v1/activity_serializer.rb b/app/serializers/api/v1/activity_serializer.rb index e69138827..216372095 100644 --- a/app/serializers/api/v1/activity_serializer.rb +++ b/app/serializers/api/v1/activity_serializer.rb @@ -4,8 +4,9 @@ module Api module V1 class ActivitySerializer < ActiveModel::Serializer type :activities - attributes :id, :my_module_id, :user_id, :type_of, :message, + attributes :id, :user_id, :type_of, :message, :project_id, :experiment_id + attribute :my_module_id, key: :task_id belongs_to :my_module, serializer: MyModuleSerializer, optional: true belongs_to :experiment, serializer: ExperimentSerializer, optional: true belongs_to :project, serializer: ProjectSerializer From 2e0c40ed459499467a9d770770d8fcc756b7d9db Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Wed, 26 Sep 2018 12:20:13 +0200 Subject: [PATCH 12/40] fixed merge --- app/controllers/api/v1/activities_controller.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/controllers/api/v1/activities_controller.rb b/app/controllers/api/v1/activities_controller.rb index e3b9b34a1..793794c8d 100644 --- a/app/controllers/api/v1/activities_controller.rb +++ b/app/controllers/api/v1/activities_controller.rb @@ -26,7 +26,6 @@ module Api render jsonapi: @activity, serializer: ActivitySerializer end -<<<<<<< HEAD def project_activities activities = @project.activities .page(params.dig(:page, :number)) @@ -40,8 +39,6 @@ module Api render jsonapi: @project_activity, serializer: ActivitySerializer end -======= ->>>>>>> 2a1c3037b8d19dd4ba3a7ae599fe0d7e6c5a2e24 private def load_team @@ -74,7 +71,6 @@ module Api ) render jsonapi: {}, status: :not_found if @activity.nil? end -<<<<<<< HEAD def load_project_activity @project_activity = @project.activities.find( @@ -82,8 +78,6 @@ module Api ) render jsonapi: {}, status: :not_found if @project_activity.nil? end -======= ->>>>>>> 2a1c3037b8d19dd4ba3a7ae599fe0d7e6c5a2e24 end end end From 2f99904000826f8e9af4d7009e412489f6258bc3 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 27 Sep 2018 10:43:52 -0700 Subject: [PATCH 13/40] merge core-api and handle merge issues --- .../v1/inventory_columns_controller_spec.rb | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/spec/requests/api/v1/inventory_columns_controller_spec.rb b/spec/requests/api/v1/inventory_columns_controller_spec.rb index 9af73ad5c..6b5b48116 100644 --- a/spec/requests/api/v1/inventory_columns_controller_spec.rb +++ b/spec/requests/api/v1/inventory_columns_controller_spec.rb @@ -259,4 +259,78 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do expect(RepositoryColumn.where(id: deleted_id)).to exist end end + + describe 'PATCH inventory_column, #update' do + before :all do + @valid_headers['Content-Type'] = 'application/json' + @inventory_column = ActiveModelSerializers::SerializableResource.new( + RepositoryColumn.last, + serializer: Api::V1::InventoryColumnSerializer + ) + end + + it 'Response with correctly updated inventory column' do + hash_body = nil + updated_inventory_column = @inventory_column.as_json + updated_inventory_column[:data][:attributes][:name] = + Faker::Name.unique.name + patch api_v1_team_inventory_column_path( + id: RepositoryColumn.last.id, + team_id: @teams.first.id, + inventory_id: @teams.first.repositories.first.id + ), params: updated_inventory_column.to_json, + headers: @valid_headers + expect(response).to have_http_status 200 + expect { hash_body = json }.not_to raise_exception + expect(hash_body.to_json).to match(updated_inventory_column.to_json) + end + + it 'Invalid request, wrong team' do + hash_body = nil + updated_inventory_column = @inventory_column.as_json + updated_inventory_column[:data][:attributes][:name] = + Faker::Name.unique.name + patch api_v1_team_inventory_column_path( + id: RepositoryColumn.last.id, + team_id: @teams.second.id, + inventory_id: @teams.first.repositories.first.id + ), params: updated_inventory_column.to_json, + headers: @valid_headers + expect(response).to have_http_status 403 + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + + it 'Invalid request, wrong inventory' do + hash_body = nil + updated_inventory_column = @inventory_column.as_json + updated_inventory_column[:data][:attributes][:name] = + Faker::Name.unique.name + patch api_v1_team_inventory_column_path( + id: RepositoryColumn.last.id, + team_id: @teams.second.id, + inventory_id: @teams.second.repositories.first.id + ), params: updated_inventory_column.to_json, + headers: @valid_headers + expect(response).to have_http_status 403 + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + + it 'Invalid request, non-existent inventory' do + hash_body = nil + updated_inventory_column = @inventory_column.as_json + updated_inventory_column[:data][:attributes][:name] = + Faker::Name.unique.name + patch api_v1_team_inventory_column_path( + id: RepositoryColumn.last.id, + team_id: @teams.first.id, + inventory_id: 123 + ), params: updated_inventory_column.to_json, + headers: @valid_headers + expect(response).to have_http_status 404 + expect { hash_body = json }.not_to raise_exception + expect(hash_body).to match({}) + end + end end From 2dc09f080d21c04a3097fe63f2d8d062da648957 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Mon, 1 Oct 2018 14:59:06 +0200 Subject: [PATCH 14/40] Revert "Revert "Merge branch 'mc-SC-2697' of https://github.com/czbiohub/scinote-web-1 into core-api"" This reverts commit 7ef85a9b60ff2453d4610387a117306ad17beecc. --- .../api/v1/inventory_items_controller_spec.rb | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index ce1d969c7..97353307a 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -245,4 +245,74 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do expect(hash_body).to match({}) end end + + describe 'PATCH inventory_items, #update' do + before :all do + @valid_headers['Content-Type'] = 'application/json' + @inventory_item = ActiveModelSerializers::SerializableResource.new( + RepositoryRow.last, + serializer: Api::V1::InventoryItemSerializer, + include: :inventory_cells + ) + end + + it 'Response with correctly updated inventory item for name field' do + hash_body = nil + updated_inventory_item = @inventory_item.as_json[:data] + updated_inventory_item[:attributes][:name] = Faker::Name.unique.name + patch api_v1_team_inventory_item_path( + id: RepositoryRow.last.id, + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), params: { data: updated_inventory_item }.to_json, + headers: @valid_headers + expect(response).to have_http_status 200 + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data].to_json).to match(updated_inventory_item.to_json) + end + + it 'Response with correctly updated inventory item for list item column' do + hash_body = nil + updated_inventory_item = @inventory_item.as_json[:included] + updated_inventory_item.each do |cell| + attributes = cell[:attributes] + if attributes[:value_type] == 'list' + cell[:attributes] = { + value_type: 'list', + value: Faker::Name.unique.name, + column_id: 2 + } + end + end + patch api_v1_team_inventory_item_path( + id: RepositoryRow.last.id, + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), params: @inventory_item.to_json, + headers: @valid_headers + expect(response).to have_http_status 200 + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:included].to_json).to match(updated_inventory_item.to_json) + end + + it 'Response with correctly updated inventory item for text item column' do + hash_body = nil + updated_inventory_item = @inventory_item.as_json[:included] + updated_inventory_item.each do |cell| + attributes = cell[:attributes] + if attributes[:value_type] == 'text' + attributes[:value] = Faker::Name.unique.name + end + end + patch api_v1_team_inventory_item_path( + id: RepositoryRow.last.id, + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), params: @inventory_item.to_json, + headers: @valid_headers + expect(response).to have_http_status 200 + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:included].to_json).to match(updated_inventory_item.to_json) + end + end end From 9f4cb02fba8d8de4545b045d5c7a325107a6e7eb Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Fri, 5 Oct 2018 11:50:17 +0200 Subject: [PATCH 15/40] Update doorkeeper gem to >= 4.6 --- Gemfile | 2 +- Gemfile.lock | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index b69de8ed6..0f1ee73b3 100644 --- a/Gemfile +++ b/Gemfile @@ -17,7 +17,7 @@ gem 'recaptcha', require: 'recaptcha/rails' gem 'sanitize', '~> 4.4' # Gems for OAuth2 subsystem -gem 'doorkeeper', '~> 4.4' +gem 'doorkeeper', '>= 4.6' gem 'omniauth' gem 'omniauth-linkedin-oauth2' diff --git a/Gemfile.lock b/Gemfile.lock index 12e8c7223..ffbc8c0e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -216,7 +216,7 @@ GEM discard (1.0.0) activerecord (>= 4.2, < 6) docile (1.1.5) - doorkeeper (4.4.1) + doorkeeper (5.0.0) railties (>= 4.2) erubi (1.7.1) execjs (2.7.0) @@ -308,7 +308,7 @@ GEM rails (>= 3.2.0) newrelic_rpm (4.7.1.340) nio4r (2.3.1) - nokogiri (1.8.4) + nokogiri (1.8.5) mini_portile2 (~> 2.3.0) nokogumbo (1.5.0) nokogiri @@ -357,7 +357,7 @@ GEM public_suffix (3.0.2) puma (3.11.2) rack (2.0.5) - rack-test (1.0.0) + rack-test (1.1.0) rack (>= 1.0, < 3) rails (5.1.6) actioncable (= 5.1.6) @@ -565,7 +565,7 @@ DEPENDENCIES devise_invitable devise_security_extension! discard (~> 1.0) - doorkeeper (~> 4.4) + doorkeeper (>= 4.6) factory_bot_rails faker figaro From 7b5a65416a597d076360e4cc4613193ee4c93039 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sat, 6 Oct 2018 11:47:23 +0200 Subject: [PATCH 16/40] Update status endpoint --- config/initializers/extends.rb | 2 +- spec/controllers/api/api_controller_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index d186191da..15eea86f5 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -61,7 +61,7 @@ class Extends repository_asset_value: :asset] # List of implemented core API versions - API_VERSIONS = ['20170715'] + API_VERSIONS = ['v1'] # Array used for injecting names of additional authentication methods for API API_PLUGABLE_AUTH_METHODS = [:azure_jwt_auth] diff --git a/spec/controllers/api/api_controller_spec.rb b/spec/controllers/api/api_controller_spec.rb index 90aa792ba..68a7ec432 100644 --- a/spec/controllers/api/api_controller_spec.rb +++ b/spec/controllers/api/api_controller_spec.rb @@ -16,8 +16,8 @@ describe Api::ApiController, type: :controller do expect { hash_body = json }.not_to raise_exception expect(hash_body).to match( 'message' => I18n.t('api.core.status_ok'), - 'versions' => [{ 'version' => '20170715', - 'baseUrl' => '/api/20170715/' }] + 'versions' => [{ 'version' => 'v1', + 'baseUrl' => '/api/v1/' }] ) end end From 6faaff550b8dc3341a755e763bed4cdf4031008e Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sat, 6 Oct 2018 12:18:57 +0200 Subject: [PATCH 17/40] Update GET /teams, GET teams/:team_id endpoints slightly --- app/controllers/api/v1/teams_controller.rb | 4 +++- app/serializers/api/v1/team_serializer.rb | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/teams_controller.rb b/app/controllers/api/v1/teams_controller.rb index 53e6e0c0c..40c4eefb0 100644 --- a/app/controllers/api/v1/teams_controller.rb +++ b/app/controllers/api/v1/teams_controller.rb @@ -13,7 +13,9 @@ module Api end def show - render jsonapi: @team, serializer: TeamSerializer + render jsonapi: @team, + serializer: TeamSerializer, + include: :created_by end private diff --git a/app/serializers/api/v1/team_serializer.rb b/app/serializers/api/v1/team_serializer.rb index ffe94acc0..62d13afae 100644 --- a/app/serializers/api/v1/team_serializer.rb +++ b/app/serializers/api/v1/team_serializer.rb @@ -4,6 +4,7 @@ module Api module V1 class TeamSerializer < ActiveModel::Serializer attributes :id, :name, :description, :space_taken + belongs_to :created_by, serializer: UserSerializer end end end From eaa8a627a843ff4d6766b8481e4dfc22645494db Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sat, 6 Oct 2018 12:39:44 +0200 Subject: [PATCH 18/40] Update GET /inventories, GET /inventories/:inventory_id endpoints --- app/controllers/api/v1/inventories_controller.rb | 4 +++- app/serializers/api/v1/inventory_serializer.rb | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/inventories_controller.rb b/app/controllers/api/v1/inventories_controller.rb index 70fef6fea..9b901980a 100644 --- a/app/controllers/api/v1/inventories_controller.rb +++ b/app/controllers/api/v1/inventories_controller.rb @@ -27,7 +27,9 @@ module Api end def show - render jsonapi: @inventory, serializer: InventorySerializer + render jsonapi: @inventory, + serializer: InventorySerializer, + include: :created_by end def update diff --git a/app/serializers/api/v1/inventory_serializer.rb b/app/serializers/api/v1/inventory_serializer.rb index dfe55f534..88752ecf3 100644 --- a/app/serializers/api/v1/inventory_serializer.rb +++ b/app/serializers/api/v1/inventory_serializer.rb @@ -5,6 +5,7 @@ module Api class InventorySerializer < ActiveModel::Serializer type :inventories attributes :id, :name + belongs_to :created_by, serializer: UserSerializer end end end From 6625b4019e4a4fd1121de58ecb1163b3db731d37 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sat, 6 Oct 2018 13:47:41 +0200 Subject: [PATCH 19/40] Update GET /inventory_columns, GET /inventory_columns/:id endpts --- .../api/v1/inventory_columns_controller.rb | 9 +- .../api/v1/inventory_column_serializer.rb | 4 +- .../v1/inventory_columns_controller_spec.rb | 103 ++++++++++++++++-- 3 files changed, 104 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/inventory_columns_controller.rb b/app/controllers/api/v1/inventory_columns_controller.rb index 2c9ae674c..1c50924ec 100644 --- a/app/controllers/api/v1/inventory_columns_controller.rb +++ b/app/controllers/api/v1/inventory_columns_controller.rb @@ -14,8 +14,9 @@ module Api .includes(:repository_list_items) .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: columns, each_serializer: InventoryColumnSerializer, - include: :inventory_list_items + render jsonapi: columns, + each_serializer: InventoryColumnSerializer, + hide_list_items: true end def create @@ -27,7 +28,9 @@ module Api end def show - render jsonapi: @inventory_column, serializer: InventoryColumnSerializer + render jsonapi: @inventory_column, + serializer: InventoryColumnSerializer, + include: :inventory_list_items end def update diff --git a/app/serializers/api/v1/inventory_column_serializer.rb b/app/serializers/api/v1/inventory_column_serializer.rb index 1d0f65b7a..347457446 100644 --- a/app/serializers/api/v1/inventory_column_serializer.rb +++ b/app/serializers/api/v1/inventory_column_serializer.rb @@ -7,7 +7,9 @@ module Api attributes :name, :data_type has_many :repository_list_items, key: :inventory_list_items, serializer: InventoryListItemSerializer, - class_name: 'RepositoryListItem' + class_name: 'RepositoryListItem', + if: -> { object.data_type == 'RepositoryListValue' && + !instance_options[:hide_list_items] } def data_type type_id = RepositoryColumn diff --git a/spec/requests/api/v1/inventory_columns_controller_spec.rb b/spec/requests/api/v1/inventory_columns_controller_spec.rb index 9af73ad5c..9a8992882 100644 --- a/spec/requests/api/v1/inventory_columns_controller_spec.rb +++ b/spec/requests/api/v1/inventory_columns_controller_spec.rb @@ -41,16 +41,9 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do ActiveModelSerializers::SerializableResource .new(@valid_inventory.repository_columns.limit(10), each_serializer: Api::V1::InventoryColumnSerializer, - include: :inventory_columns) + hide_list_items: true) .as_json[:data] ) - expect(hash_body[:included]).to match( - ActiveModelSerializers::SerializableResource - .new(@valid_inventory.repository_columns.limit(10), - each_serializer: Api::V1::InventoryColumnSerializer, - include: :inventory_list_items) - .as_json[:included] - ) end it 'When invalid request, user in not member of the team' do @@ -87,6 +80,100 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do end end + describe 'GET inventory_column, #show' do + it 'Valid text column response' do + text_column = @valid_inventory.repository_columns.first + hash_body = nil + get api_v1_team_inventory_column_path( + id: text_column.id, + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(text_column, + serializer: Api::V1::InventoryColumnSerializer) + .as_json[:data] + ) + expect(hash_body[:data]).not_to include('relationships') + end + + it 'Valid list column response' do + list_column = @valid_inventory.repository_columns.second + hash_body = nil + get api_v1_team_inventory_column_path( + id: list_column.id, + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(list_column, + serializer: Api::V1::InventoryColumnSerializer) + .as_json[:data] + ) + expect(hash_body[:data]).to include('relationships') + expect(hash_body[:included]).to match( + ActiveModelSerializers::SerializableResource + .new(@valid_inventory.repository_columns.limit(10), + each_serializer: Api::V1::InventoryColumnSerializer, + include: :inventory_list_items) + .as_json[:included] + ) + end + + it 'Valid file column response' do + file_column = @valid_inventory.repository_columns.third + hash_body = nil + get api_v1_team_inventory_column_path( + id: file_column.id, + team_id: @teams.first.id, + inventory_id: @valid_inventory.id + ), headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(file_column, + serializer: Api::V1::InventoryColumnSerializer) + .as_json[:data] + ) + expect(hash_body[:data]).not_to include('relationships') + end + + it 'Invalid request, non existing inventory column' do + get api_v1_team_inventory_column_path( + id: 1001, + team_id: @teams.first.id, + inventory_id: @teams.first.repositories.first.id + ), headers: @valid_headers + expect(response).to have_http_status(404) + end + + it 'When invalid request, incorrect repository' do + id = @teams.first.repositories.first.repository_columns.last.id + get api_v1_team_inventory_column_path( + id: id, + team_id: @teams.first.id, + inventory_id: @teams.second.repositories.first.id + ), headers: @valid_headers + expect(response).to have_http_status(404) + expect(RepositoryColumn.where(id: id)).to exist + end + + it 'When invalid request, repository from another team' do + id = @teams.first.repositories.first.repository_columns.last.id + get api_v1_team_inventory_column_path( + id: id, + team_id: @teams.second.id, + inventory_id: @teams.first.repositories.first.id + ), headers: @valid_headers + expect(response).to have_http_status(403) + expect(RepositoryColumn.where(id: id)).to exist + end + end + describe 'POST inventory_column, #create' do before :all do @valid_headers['Content-Type'] = 'application/json' From 9e78de6ef24e31b385913516fa101e57ea94fac5 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sat, 6 Oct 2018 17:11:59 +0200 Subject: [PATCH 20/40] Update CREATE, PATCH, DELETE inventory_column endpoints --- app/controllers/api/v1/inventory_columns_controller.rb | 8 +++++++- app/serializers/api/v1/inventory_column_serializer.rb | 6 ------ config/locales/en.yml | 6 ------ 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/app/controllers/api/v1/inventory_columns_controller.rb b/app/controllers/api/v1/inventory_columns_controller.rb index 1c50924ec..d11d8cd08 100644 --- a/app/controllers/api/v1/inventory_columns_controller.rb +++ b/app/controllers/api/v1/inventory_columns_controller.rb @@ -24,6 +24,7 @@ module Api @inventory.repository_columns.create!(inventory_column_params) render jsonapi: inventory_column, serializer: InventoryColumnSerializer, + hide_list_items: true, status: :created end @@ -37,7 +38,8 @@ module Api @inventory_column.attributes = update_inventory_column_params if @inventory_column.changed? && @inventory_column.save! render jsonapi: @inventory_column, - serializer: InventoryColumnSerializer + serializer: InventoryColumnSerializer, + hide_list_items: true else render body: nil end @@ -92,6 +94,10 @@ module Api raise ActionController::BadRequest, 'Object ID mismatch in URL and request body' end + if params.require(:data).require(:attributes).include?(:data_type) + raise ActionController::BadRequest, + 'Update of data_type attribute is not allowed' + end inventory_column_params[:attributes] end end diff --git a/app/serializers/api/v1/inventory_column_serializer.rb b/app/serializers/api/v1/inventory_column_serializer.rb index 347457446..64fe83d1d 100644 --- a/app/serializers/api/v1/inventory_column_serializer.rb +++ b/app/serializers/api/v1/inventory_column_serializer.rb @@ -10,12 +10,6 @@ module Api class_name: 'RepositoryListItem', if: -> { object.data_type == 'RepositoryListValue' && !instance_options[:hide_list_items] } - - def data_type - type_id = RepositoryColumn - .data_types[object.data_type] - I18n.t("api.v1.inventory_data_types.t#{type_id}") - end end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c7440c1de..8dee84d14 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1943,12 +1943,6 @@ en: status_ok: "Ok" expired_token: "Token is expired" invalid_token: "Token is invalid" - v1: - inventory_data_types: - t0: "text" - t1: "date" - t2: "list" - t3: "file" Add: "Add" Asset: "File" From d235dd780717073ff3f4fb048e10ab9a8bb3d6ce Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sat, 6 Oct 2018 18:37:40 +0200 Subject: [PATCH 21/40] Update GET /inventory_items, GET /inventory_items/:id endpoints --- .../api/v1/inventory_items_controller.rb | 3 ++- .../api/v1/inventory_cell_serializer.rb | 6 ----- .../api/v1/inventory_item_serializer.rb | 3 ++- .../api/v1/inventory_items_controller_spec.rb | 26 ++++++++++++++----- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/v1/inventory_items_controller.rb b/app/controllers/api/v1/inventory_items_controller.rb index 4ed13d37e..4f6815fcf 100644 --- a/app/controllers/api/v1/inventory_items_controller.rb +++ b/app/controllers/api/v1/inventory_items_controller.rb @@ -16,9 +16,10 @@ module Api repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES ).page(params.dig(:page, :number)) .per(params.dig(:page, :size)) + incl = params[:include] == 'inventory_cells' ? :inventory_cells : nil render jsonapi: items, each_serializer: InventoryItemSerializer, - include: :inventory_cells + include: incl end def create diff --git a/app/serializers/api/v1/inventory_cell_serializer.rb b/app/serializers/api/v1/inventory_cell_serializer.rb index b615f456c..dc89e1454 100644 --- a/app/serializers/api/v1/inventory_cell_serializer.rb +++ b/app/serializers/api/v1/inventory_cell_serializer.rb @@ -7,12 +7,6 @@ module Api attributes :id, :value_type, :value attribute :repository_column_id, key: :column_id - def value_type - type_id = RepositoryColumn - .data_types[object.repository_column.data_type] - I18n.t("api.v1.inventory_data_types.t#{type_id}") - end - def value value = case object.value_type diff --git a/app/serializers/api/v1/inventory_item_serializer.rb b/app/serializers/api/v1/inventory_item_serializer.rb index 3df5dc3f8..e980096fb 100644 --- a/app/serializers/api/v1/inventory_item_serializer.rb +++ b/app/serializers/api/v1/inventory_item_serializer.rb @@ -7,7 +7,8 @@ module Api attributes :name has_many :repository_cells, key: :inventory_cells, serializer: InventoryCellSerializer, - class_name: 'RepositoryCell' + class_name: 'RepositoryCell', + unless: -> { object.repository_cells.empty? } end end end diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index ce1d969c7..5d569fbcc 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -75,6 +75,24 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do include: :inventory_cells) .as_json[:data] ) + expect(hash_body).not_to include('included') + end + + it 'Response with correct inventory items, included cells' do + hash_body = nil + get api_v1_team_inventory_items_path( + team_id: @teams.first.id, + inventory_id: @teams.first.repositories.first.id, + include: 'inventory_cells' + ), headers: @valid_headers + expect { hash_body = json }.not_to raise_exception + expect(hash_body[:data]).to match( + ActiveModelSerializers::SerializableResource + .new(@valid_inventory.repository_rows.limit(10), + each_serializer: Api::V1::InventoryItemSerializer, + include: :inventory_cells) + .as_json[:data] + ) expect(hash_body[:included]).to match( ActiveModelSerializers::SerializableResource .new(@valid_inventory.repository_rows.limit(10), @@ -98,13 +116,7 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do include: :inventory_cells) .as_json[:data] ) - expect(hash_body[:included]).to match( - ActiveModelSerializers::SerializableResource - .new(@valid_inventory.repository_rows.limit(100), - each_serializer: Api::V1::InventoryItemSerializer, - include: :inventory_cells) - .as_json[:included] - ) + expect(hash_body).not_to include('included') end it 'When invalid request, user in not member of the team' do From c9a15db85f6fc28262e2466d05233865f261c9ae Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sat, 6 Oct 2018 19:53:31 +0200 Subject: [PATCH 22/40] Fix GET /task_groups endpoints --- app/controllers/api/v1/my_modules_controller.rb | 12 ++++++------ ...ups_controller.rb => task_groups_controller.rb} | 11 +++++++---- app/serializers/api/v1/activity_serializer.rb | 2 +- app/serializers/api/v1/connection_serializer.rb | 4 ++-- .../api/v1/my_module_group_serializer.rb | 11 ----------- .../api/v1/my_module_repository_row_serializer.rb | 2 +- app/serializers/api/v1/my_module_tag_serializer.rb | 2 +- app/serializers/api/v1/protocol_serializer.rb | 2 +- app/serializers/api/v1/result_serializer.rb | 2 +- app/serializers/api/v1/task_group_serializer.rb | 14 ++++++++++++++ ...{my_module_serializer.rb => task_serializer.rb} | 2 +- .../api/v1/user_my_module_serializer.rb | 2 +- 12 files changed, 36 insertions(+), 30 deletions(-) rename app/controllers/api/v1/{my_module_groups_controller.rb => task_groups_controller.rb} (80%) delete mode 100644 app/serializers/api/v1/my_module_group_serializer.rb create mode 100644 app/serializers/api/v1/task_group_serializer.rb rename app/serializers/api/v1/{my_module_serializer.rb => task_serializer.rb} (83%) diff --git a/app/controllers/api/v1/my_modules_controller.rb b/app/controllers/api/v1/my_modules_controller.rb index 37a2b757d..7a9e3e41a 100644 --- a/app/controllers/api/v1/my_modules_controller.rb +++ b/app/controllers/api/v1/my_modules_controller.rb @@ -14,35 +14,35 @@ module Api .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: tasks, each_serializer: MyModuleSerializer + render jsonapi: tasks, each_serializer: TaskSerializer end def show - render jsonapi: @my_module, serializer: MyModuleSerializer + render jsonapi: @my_module, serializer: TaskSerializer end def outputs outputs = @my_module.my_modules .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: outputs, each_serializer: MyModuleSerializer + render jsonapi: outputs, each_serializer: TaskSerializer end def output output = @my_module.my_modules.find(params.require(:id)) - render jsonapi: output, serializer: MyModuleSerializer + render jsonapi: output, serializer: TaskSerializer end def inputs inputs = @my_module.my_module_antecessors .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: inputs, each_serializer: MyModuleSerializer + render jsonapi: inputs, each_serializer: TaskSerializer end def input input = @my_module.my_module_antecessors.find(params.require(:id)) - render jsonapi: input, serializer: MyModuleSerializer + render jsonapi: input, serializer: TaskSerializer end private diff --git a/app/controllers/api/v1/my_module_groups_controller.rb b/app/controllers/api/v1/task_groups_controller.rb similarity index 80% rename from app/controllers/api/v1/my_module_groups_controller.rb rename to app/controllers/api/v1/task_groups_controller.rb index 1250da749..f586c2664 100644 --- a/app/controllers/api/v1/my_module_groups_controller.rb +++ b/app/controllers/api/v1/task_groups_controller.rb @@ -2,7 +2,7 @@ module Api module V1 - class MyModuleGroupsController < BaseController + class TaskGroupsController < BaseController before_action :load_team before_action :load_project before_action :load_experiment @@ -12,13 +12,16 @@ module Api my_module_groups = @experiment.my_module_groups .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - + incl = params[:include] == 'tasks' ? :tasks : nil render jsonapi: my_module_groups, - each_serializer: MyModuleGroupSerializer + each_serializer: TaskGroupSerializer, + include: incl end def show - render jsonapi: @my_module_group, serializer: MyModuleGroupSerializer + render jsonapi: @my_module_group, + serializer: TaskGroupSerializer, + include: :tasks end private diff --git a/app/serializers/api/v1/activity_serializer.rb b/app/serializers/api/v1/activity_serializer.rb index 70332ce2d..28910b7f5 100644 --- a/app/serializers/api/v1/activity_serializer.rb +++ b/app/serializers/api/v1/activity_serializer.rb @@ -6,7 +6,7 @@ module Api type :activities attributes :id, :my_module_id, :user_id, :type_of, :message, :project_id, :experiment_id - belongs_to :my_module, serializer: MyModuleSerializer + belongs_to :my_module, serializer: TaskSerializer end end end diff --git a/app/serializers/api/v1/connection_serializer.rb b/app/serializers/api/v1/connection_serializer.rb index 98f83c7c7..d75c41d18 100644 --- a/app/serializers/api/v1/connection_serializer.rb +++ b/app/serializers/api/v1/connection_serializer.rb @@ -5,8 +5,8 @@ module Api class ConnectionSerializer < ActiveModel::Serializer type :connections attributes :id, :input_id, :output_id - has_one :input_task, serializer: MyModuleSerializer - has_one :output_task, serializer: MyModuleSerializer + has_one :input_task, serializer: TaskSerializer + has_one :output_task, serializer: TaskSerializer def input_task MyModule.find(object.input_id) end diff --git a/app/serializers/api/v1/my_module_group_serializer.rb b/app/serializers/api/v1/my_module_group_serializer.rb deleted file mode 100644 index 06b52e31e..000000000 --- a/app/serializers/api/v1/my_module_group_serializer.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -module Api - module V1 - class MyModuleGroupSerializer < ActiveModel::Serializer - type :task_groups - attributes :id, :experiment_id - belongs_to :experiment, serializer: ExperimentSerializer - end - end -end diff --git a/app/serializers/api/v1/my_module_repository_row_serializer.rb b/app/serializers/api/v1/my_module_repository_row_serializer.rb index 1133b6263..4b543d012 100644 --- a/app/serializers/api/v1/my_module_repository_row_serializer.rb +++ b/app/serializers/api/v1/my_module_repository_row_serializer.rb @@ -6,7 +6,7 @@ module Api type :task_inventory_rows attribute :repository_row_id, key: :inventory_row_id attribute :my_module_id, key: :task_id - belongs_to :my_module, serializer: MyModuleSerializer + belongs_to :my_module, serializer: TaskSerializer end end end diff --git a/app/serializers/api/v1/my_module_tag_serializer.rb b/app/serializers/api/v1/my_module_tag_serializer.rb index 7f8e4a2e6..a35c39865 100644 --- a/app/serializers/api/v1/my_module_tag_serializer.rb +++ b/app/serializers/api/v1/my_module_tag_serializer.rb @@ -7,7 +7,7 @@ module Api attributes :id, :tag_id attribute :my_module_id, key: :task_id - belongs_to :my_module, serializer: MyModuleSerializer + belongs_to :my_module, serializer: TaskSerializer end end end diff --git a/app/serializers/api/v1/protocol_serializer.rb b/app/serializers/api/v1/protocol_serializer.rb index 6efafeddc..9f255b377 100644 --- a/app/serializers/api/v1/protocol_serializer.rb +++ b/app/serializers/api/v1/protocol_serializer.rb @@ -9,7 +9,7 @@ module Api :nr_of_linked_children attribute :my_module_id, key: :task_id - belongs_to :my_module, serializer: MyModuleSerializer + belongs_to :my_module, serializer: TaskSerializer end end end diff --git a/app/serializers/api/v1/result_serializer.rb b/app/serializers/api/v1/result_serializer.rb index 84aeb1408..5e990f1cb 100644 --- a/app/serializers/api/v1/result_serializer.rb +++ b/app/serializers/api/v1/result_serializer.rb @@ -7,7 +7,7 @@ module Api attributes :name, :user_id, :archived attribute :my_module_id, key: :task_id - belongs_to :my_module, serializer: MyModuleSerializer + belongs_to :my_module, serializer: TaskSerializer has_one :result_asset, key: :asset, serializer: ResultAssetSerializer, class_name: 'ResultAsset', diff --git a/app/serializers/api/v1/task_group_serializer.rb b/app/serializers/api/v1/task_group_serializer.rb new file mode 100644 index 000000000..2b7579e8f --- /dev/null +++ b/app/serializers/api/v1/task_group_serializer.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +module Api + module V1 + class TaskGroupSerializer < ActiveModel::Serializer + type :task_groups + attributes :id + has_many :my_modules, key: :tasks, + serializer: TaskSerializer, + class_name: 'MyModule', + unless: -> { object.my_modules.empty? } + end + end +end diff --git a/app/serializers/api/v1/my_module_serializer.rb b/app/serializers/api/v1/task_serializer.rb similarity index 83% rename from app/serializers/api/v1/my_module_serializer.rb rename to app/serializers/api/v1/task_serializer.rb index bc0f43779..8e2aa9cf0 100644 --- a/app/serializers/api/v1/my_module_serializer.rb +++ b/app/serializers/api/v1/task_serializer.rb @@ -2,7 +2,7 @@ module Api module V1 - class MyModuleSerializer < ActiveModel::Serializer + class TaskSerializer < ActiveModel::Serializer type :tasks attributes :id, :name, :due_date, :description, :state attribute :my_module_group_id, key: :task_group_id diff --git a/app/serializers/api/v1/user_my_module_serializer.rb b/app/serializers/api/v1/user_my_module_serializer.rb index 6474b8316..87edc39ce 100644 --- a/app/serializers/api/v1/user_my_module_serializer.rb +++ b/app/serializers/api/v1/user_my_module_serializer.rb @@ -7,7 +7,7 @@ module Api attributes :id, :user_id attribute :my_module_id, key: :task_id - belongs_to :my_module, serializer: MyModuleSerializer + belongs_to :my_module, serializer: TaskSerializer end end end From 0643ba216ed014624b64173be47e1a63d9a8c743 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 00:53:39 +0200 Subject: [PATCH 23/40] Fix GET /connections, /connections/:id endpoints --- .../api/v1/connections_controller.rb | 9 +++++++-- app/serializers/api/v1/connection_serializer.rb | 17 +++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/connections_controller.rb b/app/controllers/api/v1/connections_controller.rb index 1ce2111f4..ec42edf6b 100644 --- a/app/controllers/api/v1/connections_controller.rb +++ b/app/controllers/api/v1/connections_controller.rb @@ -12,11 +12,16 @@ module Api def index @connections = @connections.page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: @connections, each_serializer: ConnectionSerializer + incl = params[:include] == 'tasks' ? %i(input_task output_task) : nil + render jsonapi: @connections, + each_serializer: ConnectionSerializer, + include: incl end def show - render jsonapi: @connection, serializer: ConnectionSerializer + render jsonapi: @connection, + serializer: ConnectionSerializer, + include: %i(input_task output_task) end private diff --git a/app/serializers/api/v1/connection_serializer.rb b/app/serializers/api/v1/connection_serializer.rb index d75c41d18..143f440e9 100644 --- a/app/serializers/api/v1/connection_serializer.rb +++ b/app/serializers/api/v1/connection_serializer.rb @@ -4,16 +4,13 @@ module Api module V1 class ConnectionSerializer < ActiveModel::Serializer type :connections - attributes :id, :input_id, :output_id - has_one :input_task, serializer: TaskSerializer - has_one :output_task, serializer: TaskSerializer - def input_task - MyModule.find(object.input_id) - end - - def output_task - MyModule.find(object.output_id) - end + attributes :id + belongs_to :from, key: :input_task, + serializer: TaskSerializer, + class_name: 'MyModule' + belongs_to :to, key: :output_task, + serializer: TaskSerializer, + class_name: 'MyModule' end end end From 0de3c7eb4e3d8676bacbda8b2619d0d8f81704cf Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 09:45:24 +0200 Subject: [PATCH 24/40] Update GET /tasks, GET /tasks/:id endpoints, refactor routes.rb --- ..._modules_controller.rb => tasks_controller.rb} | 2 +- app/serializers/api/v1/task_serializer.rb | 4 +--- config/routes.rb | 15 +++------------ 3 files changed, 5 insertions(+), 16 deletions(-) rename app/controllers/api/v1/{my_modules_controller.rb => tasks_controller.rb} (98%) diff --git a/app/controllers/api/v1/my_modules_controller.rb b/app/controllers/api/v1/tasks_controller.rb similarity index 98% rename from app/controllers/api/v1/my_modules_controller.rb rename to app/controllers/api/v1/tasks_controller.rb index 7a9e3e41a..710fa8684 100644 --- a/app/controllers/api/v1/my_modules_controller.rb +++ b/app/controllers/api/v1/tasks_controller.rb @@ -2,7 +2,7 @@ module Api module V1 - class MyModulesController < BaseController + class TasksController < BaseController before_action :load_team before_action :load_project before_action :load_experiment diff --git a/app/serializers/api/v1/task_serializer.rb b/app/serializers/api/v1/task_serializer.rb index 8e2aa9cf0..ba5461fd2 100644 --- a/app/serializers/api/v1/task_serializer.rb +++ b/app/serializers/api/v1/task_serializer.rb @@ -4,9 +4,7 @@ module Api module V1 class TaskSerializer < ActiveModel::Serializer type :tasks - attributes :id, :name, :due_date, :description, :state - attribute :my_module_group_id, key: :task_group_id - belongs_to :experiment, serializer: ExperimentSerializer + attributes :id, :name, :due_date, :description, :state, :archived end end end diff --git a/config/routes.rb b/config/routes.rb index a680fa919..a7dbee906 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -563,18 +563,9 @@ Rails.application.routes.draw do end resources :projects, only: %i(index show) do resources :experiments, only: %i(index show) do - resources :my_module_groups, - only: %i(index show), - path: 'task_groups', - as: :task_groups - resources :connections, - only: %i(index show), - path: 'connections', - as: :connections - resources :my_modules, - only: %i(index show), - path: 'tasks', - as: :tasks do + resources :task_groups, only: %i(index show) + resources :connections, only: %i(index show) + resources :tasks, only: %i(index show) do resources :my_module_repository_rows, only: %i(index show), path: 'task_inventory_rows', as: :task_inventory_rows From f56e5b1e5aaa8b9b3a2f7ee6c20abab3824c3df3 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 10:07:37 +0200 Subject: [PATCH 25/40] Update GET /task/:task_id/items, GET /task/:task_id/items/:id endpts --- .../my_module_repository_rows_controller.rb | 59 ---------------- .../api/v1/task_inventory_items_controller.rb | 68 +++++++++++++++++++ app/controllers/api/v1/tasks_controller.rb | 14 ++-- .../api/v1/inventory_item_serializer.rb | 4 ++ .../v1/my_module_repository_row_serializer.rb | 12 ---- config/routes.rb | 6 +- 6 files changed, 82 insertions(+), 81 deletions(-) delete mode 100644 app/controllers/api/v1/my_module_repository_rows_controller.rb create mode 100644 app/controllers/api/v1/task_inventory_items_controller.rb delete mode 100644 app/serializers/api/v1/my_module_repository_row_serializer.rb diff --git a/app/controllers/api/v1/my_module_repository_rows_controller.rb b/app/controllers/api/v1/my_module_repository_rows_controller.rb deleted file mode 100644 index cdb2317ea..000000000 --- a/app/controllers/api/v1/my_module_repository_rows_controller.rb +++ /dev/null @@ -1,59 +0,0 @@ -# frozen_string_literal: true - -module Api - module V1 - class MyModuleRepositoryRowsController < BaseController - before_action :load_team - before_action :load_project - before_action :load_experiment - before_action :load_task - before_action :load_task_repository_row, only: :show - - def index - repo_rows = @my_module.my_module_repository_rows - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) - - render jsonapi: repo_rows, - each_serializer: MyModuleRepositoryRowSerializer - end - - def show - render jsonapi: @repo_row, serializer: MyModuleRepositoryRowSerializer - 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_task_repository_row - @repo_row = @my_module.my_module_repository_rows.find( - params.require(:id) - ) - render jsonapi: {}, status: :not_found if @repo_row.nil? - end - end - end -end diff --git a/app/controllers/api/v1/task_inventory_items_controller.rb b/app/controllers/api/v1/task_inventory_items_controller.rb new file mode 100644 index 000000000..ab28c6996 --- /dev/null +++ b/app/controllers/api/v1/task_inventory_items_controller.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +module Api + module V1 + class TaskInventoryItemsController < BaseController + before_action :load_team + before_action :load_project + before_action :load_experiment + before_action :load_task + before_action :load_inventory_item, only: :show + + def index + items = + @task.repository_rows + .includes(repository_cells: :repository_column) + .includes( + repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES + ).page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + incl = params[:include] == 'inventory_cells' ? :inventory_cells : nil + render jsonapi: items, + each_serializer: InventoryItemSerializer, + show_repository: true, + include: incl + end + + def show + render jsonapi: @item, + serializer: InventoryItemSerializer, + show_repository: true, + include: %i(inventory_cells inventory) + 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 + @task = @experiment.my_modules.find(params.require(:task_id)) + render jsonapi: {}, status: :not_found if @task.nil? + end + + def load_inventory_item + @item = @task.repository_rows.find( + params.require(:id) + ) + render jsonapi: {}, status: :not_found if @item.nil? + end + end + end +end diff --git a/app/controllers/api/v1/tasks_controller.rb b/app/controllers/api/v1/tasks_controller.rb index 710fa8684..f4e271fa0 100644 --- a/app/controllers/api/v1/tasks_controller.rb +++ b/app/controllers/api/v1/tasks_controller.rb @@ -18,30 +18,30 @@ module Api end def show - render jsonapi: @my_module, serializer: TaskSerializer + render jsonapi: @task, serializer: TaskSerializer end def outputs - outputs = @my_module.my_modules + outputs = @task.my_modules .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) render jsonapi: outputs, each_serializer: TaskSerializer end def output - output = @my_module.my_modules.find(params.require(:id)) + output = @task.my_modules.find(params.require(:id)) render jsonapi: output, serializer: TaskSerializer end def inputs - inputs = @my_module.my_module_antecessors + inputs = @task.my_module_antecessors .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) render jsonapi: inputs, each_serializer: TaskSerializer end def input - input = @my_module.my_module_antecessors.find(params.require(:id)) + input = @task.my_module_antecessors.find(params.require(:id)) render jsonapi: input, serializer: TaskSerializer end @@ -67,7 +67,7 @@ module Api end def load_task - @my_module = @experiment.my_modules.find(params.require(:id)) + @task = @experiment.my_modules.find(params.require(:id)) end # Made the method below because its more elegant than changing parameters @@ -75,7 +75,7 @@ module Api # for a task, the "id" that used to be task id is now an id for the output # or input. def load_task_relative - @my_module = @experiment.my_modules.find(params.require(:task_id)) + @task = @experiment.my_modules.find(params.require(:task_id)) end end end diff --git a/app/serializers/api/v1/inventory_item_serializer.rb b/app/serializers/api/v1/inventory_item_serializer.rb index e980096fb..37d3db1f2 100644 --- a/app/serializers/api/v1/inventory_item_serializer.rb +++ b/app/serializers/api/v1/inventory_item_serializer.rb @@ -9,6 +9,10 @@ module Api serializer: InventoryCellSerializer, class_name: 'RepositoryCell', unless: -> { object.repository_cells.empty? } + belongs_to :repository, key: :inventory, + serializer: InventorySerializer, + class_name: 'Repository', + if: -> { instance_options[:show_repository] } end end end diff --git a/app/serializers/api/v1/my_module_repository_row_serializer.rb b/app/serializers/api/v1/my_module_repository_row_serializer.rb deleted file mode 100644 index 4b543d012..000000000 --- a/app/serializers/api/v1/my_module_repository_row_serializer.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -module Api - module V1 - class MyModuleRepositoryRowSerializer < ActiveModel::Serializer - type :task_inventory_rows - attribute :repository_row_id, key: :inventory_row_id - attribute :my_module_id, key: :task_id - belongs_to :my_module, serializer: TaskSerializer - end - end -end diff --git a/config/routes.rb b/config/routes.rb index a7dbee906..e04963770 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -566,9 +566,9 @@ Rails.application.routes.draw do resources :task_groups, only: %i(index show) resources :connections, only: %i(index show) resources :tasks, only: %i(index show) do - resources :my_module_repository_rows, only: %i(index show), - path: 'task_inventory_rows', - as: :task_inventory_rows + resources :task_inventory_items, only: %i(index show), + path: 'items', + as: :items resources :user_my_modules, only: %i(index show), path: 'user_tasks', as: :user_tasks From 94e80edc9bae59eb173b80c2c92eb5a863430e3f Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 10:16:00 +0200 Subject: [PATCH 26/40] Minor variable rename in task_groups_controller.rb --- app/controllers/api/v1/task_groups_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/task_groups_controller.rb b/app/controllers/api/v1/task_groups_controller.rb index f586c2664..c94a73865 100644 --- a/app/controllers/api/v1/task_groups_controller.rb +++ b/app/controllers/api/v1/task_groups_controller.rb @@ -9,17 +9,17 @@ module Api before_action :load_task_group, only: :show def index - my_module_groups = @experiment.my_module_groups + task_groups = @experiment.my_module_groups .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) incl = params[:include] == 'tasks' ? :tasks : nil - render jsonapi: my_module_groups, + render jsonapi: task_groups, each_serializer: TaskGroupSerializer, include: incl end def show - render jsonapi: @my_module_group, + render jsonapi: @task_group, serializer: TaskGroupSerializer, include: :tasks end @@ -46,7 +46,7 @@ module Api end def load_task_group - @my_module_group = @experiment.my_module_groups.find( + @task_group = @experiment.my_module_groups.find( params.require(:id) ) end From 98544f00e8c79bf01fe49617320293b9a810a2bd Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 10:20:01 +0200 Subject: [PATCH 27/40] Fix GET /tasks/:task_id/users, GET /tasks/:task_id/users/1 endpts --- ..._controller.rb => task_users_controller.rb} | 18 +++++++++--------- .../api/v1/user_my_module_serializer.rb | 13 ------------- config/routes.rb | 6 +++--- 3 files changed, 12 insertions(+), 25 deletions(-) rename app/controllers/api/v1/{user_my_modules_controller.rb => task_users_controller.rb} (70%) delete mode 100644 app/serializers/api/v1/user_my_module_serializer.rb diff --git a/app/controllers/api/v1/user_my_modules_controller.rb b/app/controllers/api/v1/task_users_controller.rb similarity index 70% rename from app/controllers/api/v1/user_my_modules_controller.rb rename to app/controllers/api/v1/task_users_controller.rb index 00c3acdc7..b5d72d826 100644 --- a/app/controllers/api/v1/user_my_modules_controller.rb +++ b/app/controllers/api/v1/task_users_controller.rb @@ -2,24 +2,24 @@ module Api module V1 - class UserMyModulesController < BaseController + class TaskUsersController < BaseController before_action :load_team before_action :load_project before_action :load_experiment before_action :load_task - before_action :load_user_task, only: :show + before_action :load_user, only: :show def index - user_tasks = @my_module.user_my_modules + users = @task.users .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: user_tasks, - each_serializer: UserMyModuleSerializer + render jsonapi: users, + each_serializer: UserSerializer end def show - render jsonapi: @user_task, serializer: UserMyModuleSerializer + render jsonapi: @user, serializer: UserSerializer end private @@ -44,11 +44,11 @@ module Api end def load_task - @my_module = @experiment.my_modules.find(params.require(:task_id)) + @task = @experiment.my_modules.find(params.require(:task_id)) end - def load_user_task - @user_task = @my_module.user_my_modules.find( + def load_user + @user = @task.users.find( params.require(:id) ) end diff --git a/app/serializers/api/v1/user_my_module_serializer.rb b/app/serializers/api/v1/user_my_module_serializer.rb deleted file mode 100644 index 87edc39ce..000000000 --- a/app/serializers/api/v1/user_my_module_serializer.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module Api - module V1 - class UserMyModuleSerializer < ActiveModel::Serializer - type :user_tasks - attributes :id, :user_id - attribute :my_module_id, key: :task_id - - belongs_to :my_module, serializer: TaskSerializer - end - end -end diff --git a/config/routes.rb b/config/routes.rb index e04963770..abe255714 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -569,9 +569,9 @@ Rails.application.routes.draw do resources :task_inventory_items, only: %i(index show), path: 'items', as: :items - resources :user_my_modules, only: %i(index show), - path: 'user_tasks', - as: :user_tasks + resources :task_users, only: %i(index show), + path: 'users', + as: :users resources :my_module_tags, only: %i(index show), path: 'task_tags', as: :task_tags From 24299ef4c0d131442a508ff93fcce798a74935c5 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 11:10:41 +0200 Subject: [PATCH 28/40] Fix GET /tasks/:task_id/tags, GET /tasks/:task_id/tags/:id endpts --- ..._controller.rb => task_tags_controller.rb} | 26 +++++++++---------- .../api/v1/my_module_tag_serializer.rb | 13 ---------- app/serializers/api/v1/tag_serializer.rb | 10 +++++++ config/routes.rb | 6 ++--- 4 files changed, 26 insertions(+), 29 deletions(-) rename app/controllers/api/v1/{my_module_tags_controller.rb => task_tags_controller.rb} (57%) delete mode 100644 app/serializers/api/v1/my_module_tag_serializer.rb create mode 100644 app/serializers/api/v1/tag_serializer.rb diff --git a/app/controllers/api/v1/my_module_tags_controller.rb b/app/controllers/api/v1/task_tags_controller.rb similarity index 57% rename from app/controllers/api/v1/my_module_tags_controller.rb rename to app/controllers/api/v1/task_tags_controller.rb index c0b7911a5..abe4a30a5 100644 --- a/app/controllers/api/v1/my_module_tags_controller.rb +++ b/app/controllers/api/v1/task_tags_controller.rb @@ -2,24 +2,24 @@ module Api module V1 - class MyModuleTagsController < BaseController + class TaskTagsController < BaseController before_action :load_team before_action :load_project before_action :load_experiment before_action :load_task - before_action :load_task_tag, only: :show + before_action :load_tag, only: :show def index - task_tags = @my_module.my_module_tags - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) + tags = @task.tags + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) - render jsonapi: task_tags, - each_serializer: MyModuleTagSerializer + render jsonapi: tags, + each_serializer: TagSerializer end def show - render jsonapi: @task_tag, serializer: MyModuleTagSerializer + render jsonapi: @tag, serializer: TagSerializer end private @@ -44,15 +44,15 @@ module Api end def load_task - @my_module = @experiment.my_modules.find(params.require(:task_id)) - render jsonapi: {}, status: :not_found if @my_module.nil? + @task = @experiment.my_modules.find(params.require(:task_id)) + render jsonapi: {}, status: :not_found if @task.nil? end - def load_task_tag - @task_tag = @my_module.my_module_tags.find( + def load_tag + @tag = @task.tags.find( params.require(:id) ) - render jsonapi: {}, status: :not_found if @task_tag.nil? + render jsonapi: {}, status: :not_found if @tag.nil? end end end diff --git a/app/serializers/api/v1/my_module_tag_serializer.rb b/app/serializers/api/v1/my_module_tag_serializer.rb deleted file mode 100644 index a35c39865..000000000 --- a/app/serializers/api/v1/my_module_tag_serializer.rb +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -module Api - module V1 - class MyModuleTagSerializer < ActiveModel::Serializer - type :task_tags - attributes :id, :tag_id - attribute :my_module_id, key: :task_id - - belongs_to :my_module, serializer: TaskSerializer - end - end -end diff --git a/app/serializers/api/v1/tag_serializer.rb b/app/serializers/api/v1/tag_serializer.rb new file mode 100644 index 000000000..d30f92ade --- /dev/null +++ b/app/serializers/api/v1/tag_serializer.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Api + module V1 + class TagSerializer < ActiveModel::Serializer + type :tags + attributes :id, :name, :color + end + end +end diff --git a/config/routes.rb b/config/routes.rb index abe255714..c0b1bf7bc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -572,9 +572,9 @@ Rails.application.routes.draw do resources :task_users, only: %i(index show), path: 'users', as: :users - resources :my_module_tags, only: %i(index show), - path: 'task_tags', - as: :task_tags + resources :task_tags, only: %i(index show), + path: 'tags', + as: :tags resources :protocols, only: %i(index show), path: 'protocols', as: :protocols From 239b974fa049eac42bbef8aa2868bc43d16944b3 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 11:29:56 +0200 Subject: [PATCH 29/40] Fix GET /tasks/:tid/protocols, GET /tasks/:tid/protocols/:id endpts --- app/controllers/api/v1/protocols_controller.rb | 6 +++--- .../api/v1/protocol_keyword_serializer.rb | 10 ++++++++++ app/serializers/api/v1/protocol_serializer.rb | 14 ++++++++------ config/routes.rb | 4 +--- 4 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 app/serializers/api/v1/protocol_keyword_serializer.rb diff --git a/app/controllers/api/v1/protocols_controller.rb b/app/controllers/api/v1/protocols_controller.rb index 8b8dfbe30..f4cd1a8be 100644 --- a/app/controllers/api/v1/protocols_controller.rb +++ b/app/controllers/api/v1/protocols_controller.rb @@ -10,7 +10,7 @@ module Api before_action :load_protocol, only: :show def index - protocols = @my_module.protocols + protocols = @task.protocols .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) @@ -44,11 +44,11 @@ module Api end def load_task - @my_module = @experiment.my_modules.find(params.require(:task_id)) + @task = @experiment.my_modules.find(params.require(:task_id)) end def load_protocol - @protocol = @my_module.protocols.find( + @protocol = @task.protocols.find( params.require(:id) ) end diff --git a/app/serializers/api/v1/protocol_keyword_serializer.rb b/app/serializers/api/v1/protocol_keyword_serializer.rb new file mode 100644 index 000000000..1f8f838d6 --- /dev/null +++ b/app/serializers/api/v1/protocol_keyword_serializer.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +module Api + module V1 + class ProtocolKeywordSerializer < ActiveModel::Serializer + type :protocol_keywords + attributes :id, :name + end + end +end diff --git a/app/serializers/api/v1/protocol_serializer.rb b/app/serializers/api/v1/protocol_serializer.rb index 9f255b377..5e60d2e7e 100644 --- a/app/serializers/api/v1/protocol_serializer.rb +++ b/app/serializers/api/v1/protocol_serializer.rb @@ -4,12 +4,14 @@ module Api module V1 class ProtocolSerializer < ActiveModel::Serializer type :protocols - attributes :id, :name, :authors, :description, - :team_id, :protocol_type, - :nr_of_linked_children - attribute :my_module_id, key: :task_id - - belongs_to :my_module, serializer: TaskSerializer + attributes :id, :name, :authors, :description, :protocol_type + has_many :protocol_keywords, + key: :keywords, + serializer: ProtocolKeywordSerializer, + class_name: 'ProtocolKeyword', + unless: -> { object.protocol_keywords.empty? } + belongs_to :parent, serializer: ProtocolSerializer, + if: -> { object.parent.present? } end end end diff --git a/config/routes.rb b/config/routes.rb index c0b1bf7bc..497770cfc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -575,9 +575,7 @@ Rails.application.routes.draw do resources :task_tags, only: %i(index show), path: 'tags', as: :tags - resources :protocols, only: %i(index show), - path: 'protocols', - as: :protocols + resources :protocols, only: %i(index show) resources :results, only: %i(index create show), path: 'results', as: :results From fca580face261a160fcdb39ba12cb2a009693bfc Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 11:35:12 +0200 Subject: [PATCH 30/40] Fix GET /tasks/:id/inputs, GET /tasks/:id/outputs endpoints --- app/controllers/api/v1/tasks_controller.rb | 22 ++++++---------------- config/routes.rb | 10 ++-------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/app/controllers/api/v1/tasks_controller.rb b/app/controllers/api/v1/tasks_controller.rb index f4e271fa0..562af7919 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(outputs output inputs input) + before_action :load_task_relative, only: %i(inputs outputs) def index tasks = @experiment.my_modules @@ -21,18 +21,6 @@ module Api render jsonapi: @task, serializer: TaskSerializer end - def outputs - outputs = @task.my_modules - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) - render jsonapi: outputs, each_serializer: TaskSerializer - end - - def output - output = @task.my_modules.find(params.require(:id)) - render jsonapi: output, serializer: TaskSerializer - end - def inputs inputs = @task.my_module_antecessors .page(params.dig(:page, :number)) @@ -40,9 +28,11 @@ module Api render jsonapi: inputs, each_serializer: TaskSerializer end - def input - input = @task.my_module_antecessors.find(params.require(:id)) - render jsonapi: input, serializer: TaskSerializer + def outputs + outputs = @task.my_modules + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + render jsonapi: outputs, each_serializer: TaskSerializer end private diff --git a/config/routes.rb b/config/routes.rb index 497770cfc..067ac2663 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -579,14 +579,8 @@ Rails.application.routes.draw do resources :results, only: %i(index create show), path: 'results', as: :results - get 'inputs', - to: 'my_modules#inputs' - get 'inputs/:id', - to: 'my_modules#input' - get 'outputs', - to: 'my_modules#outputs' - get 'outputs/:id', - to: 'my_modules#output' + get 'inputs', to: 'tasks#inputs' + get 'outputs', to: 'tasks#outputs' resources :activities, only: %i(index show), path: 'activities', as: :activities From fc5cf336146a2d660765d661db0d8bbbaca8d1ca Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 11:49:11 +0200 Subject: [PATCH 31/40] Fix GET /tasks/:id/activities endpoint --- .../api/v1/activities_controller.rb | 59 ------------------- app/controllers/api/v1/tasks_controller.rb | 11 +++- app/serializers/api/v1/activity_serializer.rb | 12 +++- app/serializers/api/v1/project_serializer.rb | 10 ++++ config/routes.rb | 4 +- 5 files changed, 30 insertions(+), 66 deletions(-) delete mode 100644 app/controllers/api/v1/activities_controller.rb create mode 100644 app/serializers/api/v1/project_serializer.rb 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 From 7527dc7352faf0e7ca89da6e77dabd2a7f0079fc Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 12:36:58 +0200 Subject: [PATCH 32/40] Fix READ index, READ show, POST create /tasks/:id/results endpoints --- app/controllers/api/v1/results_controller.rb | 6 ++-- .../v1/repository_asset_value_serializer.rb | 3 +- .../api/v1/result_asset_serializer.rb | 28 +++++++++++++++++-- app/serializers/api/v1/result_serializer.rb | 22 +++++++-------- .../api/v1/result_table_serializer.rb | 10 ++++++- config/routes.rb | 4 +-- 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/app/controllers/api/v1/results_controller.rb b/app/controllers/api/v1/results_controller.rb index 068d927f3..938c4e20a 100644 --- a/app/controllers/api/v1/results_controller.rb +++ b/app/controllers/api/v1/results_controller.rb @@ -14,7 +14,7 @@ module Api .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) render jsonapi: results, each_serializer: ResultSerializer, - include: %i(asset table text) + include: %i(text table file) end def create @@ -22,13 +22,13 @@ module Api throw ActionController::ParameterMissing unless @result render jsonapi: @result, serializer: ResultSerializer, - include: %i(asset table text), + include: %i(text table file), status: :created end def show render jsonapi: @result, serializer: ResultSerializer, - include: %i(asset table text) + include: %i(text table file) end private diff --git a/app/serializers/api/v1/repository_asset_value_serializer.rb b/app/serializers/api/v1/repository_asset_value_serializer.rb index be1373efb..50fac8350 100644 --- a/app/serializers/api/v1/repository_asset_value_serializer.rb +++ b/app/serializers/api/v1/repository_asset_value_serializer.rb @@ -23,8 +23,9 @@ module Api elsif object.asset&.file&.is_stored_on_s3? object.asset.presigned_url(download: true) else + # TODO # separate api endpoint for local files download is needed - download_asset_path(object.asset.id) + 'url'#download_asset_path(object.asset.id) end end end diff --git a/app/serializers/api/v1/result_asset_serializer.rb b/app/serializers/api/v1/result_asset_serializer.rb index f6638c2b7..f69dbbae8 100644 --- a/app/serializers/api/v1/result_asset_serializer.rb +++ b/app/serializers/api/v1/result_asset_serializer.rb @@ -3,8 +3,32 @@ module Api module V1 class ResultAssetSerializer < ActiveModel::Serializer - type :result_assets - attributes :asset_id + type :result_files + attributes :file_id, :file_name, :file_size, :url + + def file_id + object.asset&.id + end + + def file_name + object.asset&.file_file_name + end + + def file_size + object.asset&.file_file_size + end + + def url + if !object.asset&.file_present + nil + elsif object.asset&.file&.is_stored_on_s3? + object.asset.presigned_url(download: true) + else + # TODO + # separate api endpoint for local files download is needed + 'url'#download_asset_path(object.asset.id) + end + end end end end diff --git a/app/serializers/api/v1/result_serializer.rb b/app/serializers/api/v1/result_serializer.rb index 5e990f1cb..ea0f4b334 100644 --- a/app/serializers/api/v1/result_serializer.rb +++ b/app/serializers/api/v1/result_serializer.rb @@ -4,22 +4,20 @@ module Api module V1 class ResultSerializer < ActiveModel::Serializer type :results - attributes :name, :user_id, :archived - attribute :my_module_id, key: :task_id - - belongs_to :my_module, serializer: TaskSerializer - has_one :result_asset, key: :asset, - serializer: ResultAssetSerializer, - class_name: 'ResultAsset', - if: -> { object.is_asset } - has_one :result_table, key: :table, - serializer: ResultTableSerializer, - class_name: 'ResultTable', - if: -> { object.is_table } + attributes :name, :archived + belongs_to :user, serializer: UserSerializer has_one :result_text, key: :text, serializer: ResultTextSerializer, class_name: 'ResultText', if: -> { object.is_text } + has_one :result_table, key: :table, + serializer: ResultTableSerializer, + class_name: 'ResultTable', + if: -> { object.is_table } + has_one :result_asset, key: :file, + serializer: ResultAssetSerializer, + class_name: 'ResultAsset', + if: -> { object.is_asset } end end end diff --git a/app/serializers/api/v1/result_table_serializer.rb b/app/serializers/api/v1/result_table_serializer.rb index b4bdddc1c..211950441 100644 --- a/app/serializers/api/v1/result_table_serializer.rb +++ b/app/serializers/api/v1/result_table_serializer.rb @@ -4,7 +4,15 @@ module Api module V1 class ResultTableSerializer < ActiveModel::Serializer type :result_tables - attributes :table_id + attributes :table_id, :table_contents + + def table_id + object.table&.id + end + + def table_contents + object.table&.contents + end end end end diff --git a/config/routes.rb b/config/routes.rb index f4440c769..4b5ebcef1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -576,9 +576,7 @@ Rails.application.routes.draw do path: 'tags', as: :tags resources :protocols, only: %i(index show) - resources :results, only: %i(index create show), - path: 'results', - as: :results + resources :results, only: %i(index create show) get 'inputs', to: 'tasks#inputs' get 'outputs', to: 'tasks#outputs' get 'activities', to: 'tasks#activities' From 0bd3c81e39e52b0c66ec6dbd90b1ac3d7cf44644 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 13:07:27 +0200 Subject: [PATCH 33/40] Fix /users, /users/:id/identities endpoints --- config/routes.rb | 4 +++- .../{user_identeties.rb => user_identities.rb} | 0 ..._spec.rb => user_identities_controller_spec.rb} | 14 +++++++------- 3 files changed, 10 insertions(+), 8 deletions(-) rename spec/factories/{user_identeties.rb => user_identities.rb} (100%) rename spec/requests/api/v1/{users_identeties_controller_spec.rb => user_identities_controller_spec.rb} (89%) diff --git a/config/routes.rb b/config/routes.rb index 4b5ebcef1..e07102f33 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -586,7 +586,9 @@ Rails.application.routes.draw do end resources :users, only: %i(show) do resources :user_identities, - only: %i(index create show update destroy) + only: %i(index create show update destroy), + path: 'identities', + as: :identities end end end diff --git a/spec/factories/user_identeties.rb b/spec/factories/user_identities.rb similarity index 100% rename from spec/factories/user_identeties.rb rename to spec/factories/user_identities.rb diff --git a/spec/requests/api/v1/users_identeties_controller_spec.rb b/spec/requests/api/v1/user_identities_controller_spec.rb similarity index 89% rename from spec/requests/api/v1/users_identeties_controller_spec.rb rename to spec/requests/api/v1/user_identities_controller_spec.rb index 3a927f5a2..e0d80d110 100644 --- a/spec/requests/api/v1/users_identeties_controller_spec.rb +++ b/spec/requests/api/v1/user_identities_controller_spec.rb @@ -15,7 +15,7 @@ RSpec.describe 'Api::V1::UsersIdentitiesController', type: :request do describe 'GET user_identities, #index' do it 'When valid request, requested identities for current user' do hash_body = nil - get api_v1_user_user_identities_path(user_id: @user1.id), + get api_v1_user_identities_path(user_id: @user1.id), headers: @valid_headers expect { hash_body = json }.not_to raise_exception expect(hash_body[:data]).to match( @@ -28,7 +28,7 @@ RSpec.describe 'Api::V1::UsersIdentitiesController', type: :request do it 'When invalid request, requested user is not signed in user' do hash_body = nil - get api_v1_user_user_identities_path(user_id: @user2.id), + get api_v1_user_identities_path(user_id: @user2.id), headers: @valid_headers expect(response).to have_http_status(403) expect(hash_body).to match(nil) @@ -36,7 +36,7 @@ RSpec.describe 'Api::V1::UsersIdentitiesController', type: :request do it 'When invalid request, non existing user' do hash_body = nil - get api_v1_user_user_identities_path(user_id: 123), + get api_v1_user_identities_path(user_id: 123), headers: @valid_headers expect(response).to have_http_status(403) expect(hash_body).to match(nil) @@ -46,7 +46,7 @@ RSpec.describe 'Api::V1::UsersIdentitiesController', type: :request do describe 'POST user_identities, #create' do it 'When valid request, create new identity for current user' do hash_body = nil - post api_v1_user_user_identities_path(user_id: @user1.id), + post api_v1_user_identities_path(user_id: @user1.id), params: { data: { type: 'user_identities', attributes: { uid: Faker::Crypto.unique.sha1, @@ -66,7 +66,7 @@ RSpec.describe 'Api::V1::UsersIdentitiesController', type: :request do describe 'GET user_identity, #show' do it 'When valid request, requested specific identity for current user' do hash_body = nil - get api_v1_user_user_identity_path( + get api_v1_user_identity_path( user_id: @user1.id, id: @user1.user_identities.order(:id).last ), headers: @valid_headers expect { hash_body = json }.not_to raise_exception @@ -82,7 +82,7 @@ RSpec.describe 'Api::V1::UsersIdentitiesController', type: :request do describe 'PUT user_identities, #update' do it 'When valid request, update identity for current user' do hash_body = nil - put api_v1_user_user_identity_path(user_id: @user1.id, + put api_v1_user_identity_path(user_id: @user1.id, id: @user1.user_identities.order(:id).last), params: { data: { id: @user1.user_identities.order(:id).last.id, @@ -103,7 +103,7 @@ RSpec.describe 'Api::V1::UsersIdentitiesController', type: :request do describe 'DELETE user_identity, #destroy' do it 'When valid request, destroy specified identity for current user' do identity = @user1.user_identities.order(:id).last - delete api_v1_user_user_identity_path(user_id: @user1.id, id: identity), + delete api_v1_user_identity_path(user_id: @user1.id, id: identity), headers: @valid_headers expect(response).to have_http_status(200) expect(response.body).to eq('') From dc2ab5fdc0108124e3df96f6a6118817b7ded875 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 13:11:56 +0200 Subject: [PATCH 34/40] Fix the Doorkeeper config/access for all users (due to update of Gem) --- config/initializers/doorkeeper.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/config/initializers/doorkeeper.rb b/config/initializers/doorkeeper.rb index 9153fbabd..ffd7015c1 100644 --- a/config/initializers/doorkeeper.rb +++ b/config/initializers/doorkeeper.rb @@ -8,11 +8,9 @@ Doorkeeper.configure do end # If you want to restrict access to the web interface for adding oauth authorized applications, you need to declare the block below. - # admin_authenticator do - # # Put your admin authentication logic here. - # # Example implementation: - # Admin.find_by_id(session[:admin_id]) || redirect_to(new_admin_session_url) - # end + admin_authenticator do + current_user || redirect_to(new_admin_session_url) + end # Authorization Code expiration time (default 10 minutes). authorization_code_expires_in 10.minutes From a8d56d32e2a04dd68259652390bb1b4dd919bc43 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Sun, 7 Oct 2018 13:28:29 +0200 Subject: [PATCH 35/40] Hound is love, Hound is life --- .../api/v1/task_inventory_items_controller.rb | 9 ++++----- app/controllers/api/v1/tasks_controller.rb | 4 ++-- app/serializers/api/v1/inventory_column_serializer.rb | 11 ++++++----- app/serializers/api/v1/protocol_serializer.rb | 8 ++++---- .../api/v1/inventory_columns_controller_spec.rb | 6 +++--- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/controllers/api/v1/task_inventory_items_controller.rb b/app/controllers/api/v1/task_inventory_items_controller.rb index ab28c6996..a7e87e96a 100644 --- a/app/controllers/api/v1/task_inventory_items_controller.rb +++ b/app/controllers/api/v1/task_inventory_items_controller.rb @@ -12,11 +12,10 @@ module Api def index items = @task.repository_rows - .includes(repository_cells: :repository_column) - .includes( - repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES - ).page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) + .includes(repository_cells: :repository_column) + .includes(repository_cells: Extends::REPOSITORY_SEARCH_INCLUDES) + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) incl = params[:include] == 'inventory_cells' ? :inventory_cells : nil render jsonapi: items, each_serializer: InventoryItemSerializer, diff --git a/app/controllers/api/v1/tasks_controller.rb b/app/controllers/api/v1/tasks_controller.rb index 9ab8ed1ea..46466be10 100644 --- a/app/controllers/api/v1/tasks_controller.rb +++ b/app/controllers/api/v1/tasks_controller.rb @@ -23,8 +23,8 @@ module Api def inputs inputs = @task.my_module_antecessors - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) render jsonapi: inputs, each_serializer: TaskSerializer end diff --git a/app/serializers/api/v1/inventory_column_serializer.rb b/app/serializers/api/v1/inventory_column_serializer.rb index 64fe83d1d..268cb151d 100644 --- a/app/serializers/api/v1/inventory_column_serializer.rb +++ b/app/serializers/api/v1/inventory_column_serializer.rb @@ -5,11 +5,12 @@ module Api class InventoryColumnSerializer < ActiveModel::Serializer type :inventory_columns attributes :name, :data_type - has_many :repository_list_items, key: :inventory_list_items, - serializer: InventoryListItemSerializer, - class_name: 'RepositoryListItem', - if: -> { object.data_type == 'RepositoryListValue' && - !instance_options[:hide_list_items] } + has_many :repository_list_items, + key: :inventory_list_items, + serializer: InventoryListItemSerializer, + class_name: 'RepositoryListItem', + if: -> { object.data_type == 'RepositoryListValue' && + !instance_options[:hide_list_items] } end end end diff --git a/app/serializers/api/v1/protocol_serializer.rb b/app/serializers/api/v1/protocol_serializer.rb index 5e60d2e7e..c2995f847 100644 --- a/app/serializers/api/v1/protocol_serializer.rb +++ b/app/serializers/api/v1/protocol_serializer.rb @@ -6,10 +6,10 @@ module Api type :protocols attributes :id, :name, :authors, :description, :protocol_type has_many :protocol_keywords, - key: :keywords, - serializer: ProtocolKeywordSerializer, - class_name: 'ProtocolKeyword', - unless: -> { object.protocol_keywords.empty? } + key: :keywords, + serializer: ProtocolKeywordSerializer, + class_name: 'ProtocolKeyword', + unless: -> { object.protocol_keywords.empty? } belongs_to :parent, serializer: ProtocolSerializer, if: -> { object.parent.present? } end diff --git a/spec/requests/api/v1/inventory_columns_controller_spec.rb b/spec/requests/api/v1/inventory_columns_controller_spec.rb index 9a8992882..1478a451c 100644 --- a/spec/requests/api/v1/inventory_columns_controller_spec.rb +++ b/spec/requests/api/v1/inventory_columns_controller_spec.rb @@ -93,7 +93,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do expect(hash_body[:data]).to match( ActiveModelSerializers::SerializableResource .new(text_column, - serializer: Api::V1::InventoryColumnSerializer) + serializer: Api::V1::InventoryColumnSerializer) .as_json[:data] ) expect(hash_body[:data]).not_to include('relationships') @@ -111,7 +111,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do expect(hash_body[:data]).to match( ActiveModelSerializers::SerializableResource .new(list_column, - serializer: Api::V1::InventoryColumnSerializer) + serializer: Api::V1::InventoryColumnSerializer) .as_json[:data] ) expect(hash_body[:data]).to include('relationships') @@ -136,7 +136,7 @@ RSpec.describe 'Api::V1::InventoryColumnsController', type: :request do expect(hash_body[:data]).to match( ActiveModelSerializers::SerializableResource .new(file_column, - serializer: Api::V1::InventoryColumnSerializer) + serializer: Api::V1::InventoryColumnSerializer) .as_json[:data] ) expect(hash_body[:data]).not_to include('relationships') From dd4f7b5d025887144e8ca7ca79a7c6802573dfbb Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Wed, 10 Oct 2018 17:35:09 +0200 Subject: [PATCH 36/40] Fix project users, activities, comments, reports endpoints --- .../api/v1/activities_controller.rb | 28 ++----------------- .../api/v1/project_comments_controller.rb | 8 ++++-- app/controllers/api/v1/projects_controller.rb | 16 +++++++++++ app/controllers/api/v1/reports_controller.rb | 9 ++++-- .../api/v1/user_projects_controller.rb | 8 ++++-- app/serializers/api/v1/comment_serializer.rb | 27 ++++++++++++++++++ .../api/v1/project_comment_serializer.rb | 12 -------- app/serializers/api/v1/report_serializer.rb | 7 +++-- .../api/v1/user_project_serializer.rb | 4 +-- config/routes.rb | 10 +++---- 10 files changed, 74 insertions(+), 55 deletions(-) create mode 100644 app/serializers/api/v1/comment_serializer.rb delete mode 100644 app/serializers/api/v1/project_comment_serializer.rb diff --git a/app/controllers/api/v1/activities_controller.rb b/app/controllers/api/v1/activities_controller.rb index 793794c8d..125c44c86 100644 --- a/app/controllers/api/v1/activities_controller.rb +++ b/app/controllers/api/v1/activities_controller.rb @@ -5,12 +5,8 @@ module Api class ActivitiesController < BaseController before_action :load_team before_action :load_project - - before_action :load_project_activity, only: :project_activity - before_action :load_experiment, except: %i( - project_activities project_activity - ) - before_action :load_task, except: %i(project_activities project_activity) + before_action :load_experiment + before_action :load_task before_action :load_activity, only: :show def index @@ -26,19 +22,6 @@ module Api render jsonapi: @activity, serializer: ActivitySerializer end - def project_activities - activities = @project.activities - .page(params.dig(:page, :number)) - .per(params.dig(:page, :size)) - - render jsonapi: activities, - each_serializer: ActivitySerializer - end - - def project_activity - render jsonapi: @project_activity, serializer: ActivitySerializer - end - private def load_team @@ -71,13 +54,6 @@ module Api ) render jsonapi: {}, status: :not_found if @activity.nil? end - - def load_project_activity - @project_activity = @project.activities.find( - params.require(:id) - ) - render jsonapi: {}, status: :not_found if @project_activity.nil? - end end end end diff --git a/app/controllers/api/v1/project_comments_controller.rb b/app/controllers/api/v1/project_comments_controller.rb index bca71a462..762f5b196 100644 --- a/app/controllers/api/v1/project_comments_controller.rb +++ b/app/controllers/api/v1/project_comments_controller.rb @@ -13,11 +13,15 @@ module Api .per(params.dig(:page, :size)) render jsonapi: project_comments, - each_serializer: ProjectCommentSerializer + each_serializer: CommentSerializer, + hide_project: true end def show - render jsonapi: @project_comment, serializer: ProjectCommentSerializer + render jsonapi: @project_comment, + serializer: CommentSerializer, + hide_project: true, + include: :user end private diff --git a/app/controllers/api/v1/projects_controller.rb b/app/controllers/api/v1/projects_controller.rb index 0cc5c05d0..989cc9cb3 100644 --- a/app/controllers/api/v1/projects_controller.rb +++ b/app/controllers/api/v1/projects_controller.rb @@ -5,6 +5,7 @@ module Api class ProjectsController < BaseController before_action :load_team before_action :load_project, only: :show + before_action :load_project_relative, only: :activities def index projects = @team.projects @@ -18,6 +19,14 @@ module Api render jsonapi: @project, serializer: ProjectSerializer end + def activities + activities = @project.activities + .page(params.dig(:page, :number)) + .per(params.dig(:page, :size)) + render jsonapi: activities, + each_serializer: ActivitySerializer + end + private def load_team @@ -31,6 +40,13 @@ module Api @project ) end + + def load_project_relative + @project = @team.projects.find(params.require(:project_id)) + render jsonapi: {}, status: :forbidden unless can_read_project?( + @project + ) + end end end end diff --git a/app/controllers/api/v1/reports_controller.rb b/app/controllers/api/v1/reports_controller.rb index 01f527f53..b7060596f 100644 --- a/app/controllers/api/v1/reports_controller.rb +++ b/app/controllers/api/v1/reports_controller.rb @@ -12,11 +12,16 @@ module Api .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: reports, each_serializer: ReportSerializer + render jsonapi: reports, + each_serializer: ReportSerializer, + hide_project: true end def show - render jsonapi: @report, serializer: ReportSerializer + render jsonapi: @report, + serializer: ReportSerializer, + hide_project: true, + include: :user end private diff --git a/app/controllers/api/v1/user_projects_controller.rb b/app/controllers/api/v1/user_projects_controller.rb index dfa376a07..21d5d4ce1 100644 --- a/app/controllers/api/v1/user_projects_controller.rb +++ b/app/controllers/api/v1/user_projects_controller.rb @@ -12,11 +12,15 @@ module Api .page(params.dig(:page, :number)) .per(params.dig(:page, :size)) - render jsonapi: user_projects, each_serializer: UserProjectSerializer + render jsonapi: user_projects, + each_serializer: UserProjectSerializer, + include: :user end def show - render jsonapi: @user_project, serializer: UserProjectSerializer + render jsonapi: @user_project, + serializer: UserProjectSerializer, + include: :user end private diff --git a/app/serializers/api/v1/comment_serializer.rb b/app/serializers/api/v1/comment_serializer.rb new file mode 100644 index 000000000..c3d1c36a6 --- /dev/null +++ b/app/serializers/api/v1/comment_serializer.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module Api + module V1 + class CommentSerializer < ActiveModel::Serializer + type :comments + attributes :id, :message + + belongs_to :user, serializer: UserSerializer + belongs_to :project, serializer: ProjectSerializer, + if: -> { object.class == ProjectComment && + !instance_options[:hide_project] } + # TODO + #belongs_to :my_module, serializer: TaskSerializer, + # key: :task, + # class_name: 'MyModule', + # if: -> { object.class == TaskComment && + # !instance_options[:hide_task] } + #belongs_to :step, serializer: StepSerializer, + # if: -> { object.class == StepComment && + # !instance_options[:hide_step] } + belongs_to :result, serializer: ResultSerializer, + if: -> { object.class == ResultComment && + !instance_options[:hide_result] } + end + end +end diff --git a/app/serializers/api/v1/project_comment_serializer.rb b/app/serializers/api/v1/project_comment_serializer.rb deleted file mode 100644 index a9786f316..000000000 --- a/app/serializers/api/v1/project_comment_serializer.rb +++ /dev/null @@ -1,12 +0,0 @@ -# frozen_string_literal: true - -module Api - module V1 - class ProjectCommentSerializer < ActiveModel::Serializer - type :project_comments - attributes :id, :message, :user_id, :type, :associated_id - - belongs_to :project, serializer: ProjectSerializer - end - end -end diff --git a/app/serializers/api/v1/report_serializer.rb b/app/serializers/api/v1/report_serializer.rb index 1d4d2509f..94b666b22 100644 --- a/app/serializers/api/v1/report_serializer.rb +++ b/app/serializers/api/v1/report_serializer.rb @@ -4,9 +4,10 @@ module Api module V1 class ReportSerializer < ActiveModel::Serializer type :reports - attributes :id, :name, :description, :project_id - - belongs_to :project, serializer: ProjectSerializer + attributes :id, :name, :description + belongs_to :user, serializer: UserSerializer + belongs_to :project, serializer: ProjectSerializer, + unless: -> { instance_options[:hide_project] } end end end diff --git a/app/serializers/api/v1/user_project_serializer.rb b/app/serializers/api/v1/user_project_serializer.rb index e6b2dfbfe..e2e138232 100644 --- a/app/serializers/api/v1/user_project_serializer.rb +++ b/app/serializers/api/v1/user_project_serializer.rb @@ -4,9 +4,9 @@ module Api module V1 class UserProjectSerializer < ActiveModel::Serializer type :user_projects - attributes :id, :role, :user_id + attributes :id, :role - belongs_to :project, serializer: ProjectSerializer + belongs_to :user, serializer: UserSerializer end end end diff --git a/config/routes.rb b/config/routes.rb index 73ce70073..fe71182b3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -556,16 +556,14 @@ Rails.application.routes.draw do path: 'items', as: :items end - resources :projects, only: %i(index show), path: 'projects', - as: :projects do + resources :projects, only: %i(index show) do resources :user_projects, only: %i(index show), - path: 'user_projects', as: :user_projects + path: 'users', as: :users resources :project_comments, only: %i(index show), - path: 'project_comments', as: :project_comments + path: 'comments', as: :comments + get 'activities', to: 'projects#activities' resources :reports, only: %i(index show), path: 'reports', as: :reports - get 'activities', to: 'activities#project_activities' - get 'activities/:id', to: 'activities#project_activity' resources :experiments, only: %i(index show) do resources :my_module_groups, only: %i(index show), From c0f47f03b7cfbb7ede2c722499501b398cbcfe56 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 11 Oct 2018 08:51:14 +0200 Subject: [PATCH 37/40] Hound is love, Hound is life --- app/serializers/api/v1/comment_serializer.rb | 32 +++++++++++--------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/app/serializers/api/v1/comment_serializer.rb b/app/serializers/api/v1/comment_serializer.rb index c3d1c36a6..d369d339c 100644 --- a/app/serializers/api/v1/comment_serializer.rb +++ b/app/serializers/api/v1/comment_serializer.rb @@ -7,21 +7,25 @@ module Api attributes :id, :message belongs_to :user, serializer: UserSerializer - belongs_to :project, serializer: ProjectSerializer, - if: -> { object.class == ProjectComment && - !instance_options[:hide_project] } + belongs_to :project, + serializer: ProjectSerializer, + if: -> { object.class == ProjectComment && + !instance_options[:hide_project] } # TODO - #belongs_to :my_module, serializer: TaskSerializer, - # key: :task, - # class_name: 'MyModule', - # if: -> { object.class == TaskComment && - # !instance_options[:hide_task] } - #belongs_to :step, serializer: StepSerializer, - # if: -> { object.class == StepComment && - # !instance_options[:hide_step] } - belongs_to :result, serializer: ResultSerializer, - if: -> { object.class == ResultComment && - !instance_options[:hide_result] } + #belongs_to :my_module, + # serializer: TaskSerializer, + # key: :task, + # class_name: 'MyModule', + # if: -> { object.class == TaskComment && + # !instance_options[:hide_task] } + #belongs_to :step, + # serializer: StepSerializer, + # if: -> { object.class == StepComment && + # !instance_options[:hide_step] } + belongs_to :result, + serializer: ResultSerializer, + if: -> { object.class == ResultComment && + !instance_options[:hide_result] } end end end From c11345d6a1c36bf74d67068e176b7d7d3b63d489 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 11 Oct 2018 09:49:54 +0200 Subject: [PATCH 38/40] Minorly update inventory_column_serializer so tests pass --- app/serializers/api/v1/inventory_column_serializer.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/app/serializers/api/v1/inventory_column_serializer.rb b/app/serializers/api/v1/inventory_column_serializer.rb index 1d0f65b7a..18ba68938 100644 --- a/app/serializers/api/v1/inventory_column_serializer.rb +++ b/app/serializers/api/v1/inventory_column_serializer.rb @@ -8,12 +8,6 @@ module Api has_many :repository_list_items, key: :inventory_list_items, serializer: InventoryListItemSerializer, class_name: 'RepositoryListItem' - - def data_type - type_id = RepositoryColumn - .data_types[object.data_type] - I18n.t("api.v1.inventory_data_types.t#{type_id}") - end end end end From 33b9c65c29c1184c12dcae85001b0a6efcc030f2 Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 11 Oct 2018 14:27:52 +0200 Subject: [PATCH 39/40] Fix Hound --- .../api/v1/inventory_items_controller_spec.rb | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index 97353307a..d3ba642b7 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -276,13 +276,12 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do updated_inventory_item = @inventory_item.as_json[:included] updated_inventory_item.each do |cell| attributes = cell[:attributes] - if attributes[:value_type] == 'list' - cell[:attributes] = { - value_type: 'list', - value: Faker::Name.unique.name, - column_id: 2 - } - end + next unless attributes[:value_type] == 'list' + cell[:attributes] = { + value_type: 'list', + value: Faker::Name.unique.name, + column_id: 2 + } end patch api_v1_team_inventory_item_path( id: RepositoryRow.last.id, @@ -292,7 +291,9 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do headers: @valid_headers expect(response).to have_http_status 200 expect { hash_body = json }.not_to raise_exception - expect(hash_body[:included].to_json).to match(updated_inventory_item.to_json) + expect(hash_body[:included].to_json).to match( + updated_inventory_item.to_json + ) end it 'Response with correctly updated inventory item for text item column' do @@ -312,7 +313,9 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do headers: @valid_headers expect(response).to have_http_status 200 expect { hash_body = json }.not_to raise_exception - expect(hash_body[:included].to_json).to match(updated_inventory_item.to_json) + expect(hash_body[:included].to_json).to match( + updated_inventory_item.to_json + ) end end end From 028c0c683291e20f97b0734ab5b4458a781202cb Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Thu, 11 Oct 2018 14:41:13 +0200 Subject: [PATCH 40/40] Remove 2 obsolete unit tests --- .../api/v1/inventory_items_controller_spec.rb | 47 ------------------- 1 file changed, 47 deletions(-) diff --git a/spec/requests/api/v1/inventory_items_controller_spec.rb b/spec/requests/api/v1/inventory_items_controller_spec.rb index d3ba642b7..e49e0bcfd 100644 --- a/spec/requests/api/v1/inventory_items_controller_spec.rb +++ b/spec/requests/api/v1/inventory_items_controller_spec.rb @@ -270,52 +270,5 @@ RSpec.describe 'Api::V1::InventoryItemsController', type: :request do expect { hash_body = json }.not_to raise_exception expect(hash_body[:data].to_json).to match(updated_inventory_item.to_json) end - - it 'Response with correctly updated inventory item for list item column' do - hash_body = nil - updated_inventory_item = @inventory_item.as_json[:included] - updated_inventory_item.each do |cell| - attributes = cell[:attributes] - next unless attributes[:value_type] == 'list' - cell[:attributes] = { - value_type: 'list', - value: Faker::Name.unique.name, - column_id: 2 - } - end - patch api_v1_team_inventory_item_path( - id: RepositoryRow.last.id, - team_id: @teams.first.id, - inventory_id: @valid_inventory.id - ), params: @inventory_item.to_json, - headers: @valid_headers - expect(response).to have_http_status 200 - expect { hash_body = json }.not_to raise_exception - expect(hash_body[:included].to_json).to match( - updated_inventory_item.to_json - ) - end - - it 'Response with correctly updated inventory item for text item column' do - hash_body = nil - updated_inventory_item = @inventory_item.as_json[:included] - updated_inventory_item.each do |cell| - attributes = cell[:attributes] - if attributes[:value_type] == 'text' - attributes[:value] = Faker::Name.unique.name - end - end - patch api_v1_team_inventory_item_path( - id: RepositoryRow.last.id, - team_id: @teams.first.id, - inventory_id: @valid_inventory.id - ), params: @inventory_item.to_json, - headers: @valid_headers - expect(response).to have_http_status 200 - expect { hash_body = json }.not_to raise_exception - expect(hash_body[:included].to_json).to match( - updated_inventory_item.to_json - ) - end end end