2018-09-10 22:20:02 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class ProjectsController < BaseController
|
|
|
|
before_action :load_team
|
2018-11-07 22:43:44 +08:00
|
|
|
before_action only: :show do
|
|
|
|
load_project(:id)
|
|
|
|
end
|
2020-07-02 23:03:29 +08:00
|
|
|
before_action :load_project, only: :activities
|
2020-09-16 22:34:35 +08:00
|
|
|
before_action :load_project_for_managing, only: %i(update)
|
2018-09-10 22:20:02 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
projects = @team.projects
|
2018-10-26 22:46:01 +08:00
|
|
|
.visible_to(current_user, @team)
|
2018-09-10 22:20:02 +08:00
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
|
|
|
|
2021-08-18 16:00:01 +08:00
|
|
|
render jsonapi: projects, each_serializer: ProjectSerializer, include: include_params
|
2018-09-10 22:20:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2021-08-18 16:00:01 +08:00
|
|
|
render jsonapi: @project, serializer: ProjectSerializer, include: include_params
|
2018-09-10 22:20:02 +08:00
|
|
|
end
|
|
|
|
|
2020-09-16 22:34:35 +08:00
|
|
|
def create
|
|
|
|
raise PermissionError.new(Project, :create) unless can_create_projects?(@team)
|
|
|
|
|
2021-11-15 22:36:38 +08:00
|
|
|
project = @team.projects.build(project_params.merge!(created_by: current_user))
|
|
|
|
|
|
|
|
if project.visible? # set default viewer role for public projects
|
2021-11-16 04:36:06 +08:00
|
|
|
project.default_public_user_role = UserRole.predefined.find_by(name: I18n.t('user_roles.predefined.viewer'))
|
2021-11-15 22:36:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
project.save!
|
2020-09-16 22:34:35 +08:00
|
|
|
|
|
|
|
render jsonapi: project, serializer: ProjectSerializer, status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@project.assign_attributes(project_params)
|
|
|
|
|
|
|
|
return render body: nil, status: :no_content unless @project.changed?
|
|
|
|
|
|
|
|
if @project.archived_changed?
|
|
|
|
if @project.archived?
|
|
|
|
@project.archived_by = current_user
|
|
|
|
else
|
|
|
|
@project.restored_by = current_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@project.last_modified_by = current_user
|
|
|
|
@project.save!
|
|
|
|
render jsonapi: @project, serializer: ProjectSerializer, status: :ok
|
|
|
|
end
|
|
|
|
|
2018-10-10 23:35:09 +08:00
|
|
|
def activities
|
|
|
|
activities = @project.activities
|
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
|
|
|
render jsonapi: activities,
|
|
|
|
each_serializer: ActivitySerializer
|
|
|
|
end
|
2020-09-16 22:34:35 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def project_params
|
|
|
|
raise TypeError unless params.require(:data).require(:type) == 'projects'
|
|
|
|
|
2020-12-02 18:44:37 +08:00
|
|
|
params.require(:data).require(:attributes).permit(:name, :visibility, :archived, :project_folder_id)
|
2020-09-16 22:34:35 +08:00
|
|
|
end
|
|
|
|
|
2021-08-18 16:00:01 +08:00
|
|
|
def permitted_includes
|
|
|
|
%w(comments)
|
|
|
|
end
|
|
|
|
|
2020-09-16 22:34:35 +08:00
|
|
|
def load_project_for_managing
|
|
|
|
@project = @team.projects.find(params.require(:id))
|
|
|
|
raise PermissionError.new(Project, :manage) unless can_manage_project?(@project)
|
|
|
|
end
|
2018-09-10 22:20:02 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|