From dd4f7b5d025887144e8ca7ca79a7c6802573dfbb Mon Sep 17 00:00:00 2001 From: Luka Murn Date: Wed, 10 Oct 2018 17:35:09 +0200 Subject: [PATCH] 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),