From 83717e8655ce5222ed8c7642bd8b057692f1e3e7 Mon Sep 17 00:00:00 2001 From: Zanz2 Date: Mon, 10 Sep 2018 16:20:02 +0200 Subject: [PATCH 1/6] 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 2/6] 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 3/6] 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 4/6] 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 5/6] 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 6/6] 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