Fix project users, activities, comments, reports endpoints

This commit is contained in:
Luka Murn 2018-10-10 17:35:09 +02:00
parent 2e0c40ed45
commit dd4f7b5d02
10 changed files with 74 additions and 55 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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),