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))
|
|
|
|
|
|
|
|
render jsonapi: projects, each_serializer: ProjectSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
render jsonapi: @project, serializer: ProjectSerializer
|
|
|
|
end
|
|
|
|
|
2020-09-16 22:34:35 +08:00
|
|
|
def create
|
|
|
|
raise PermissionError.new(Project, :create) unless can_create_projects?(@team)
|
|
|
|
|
|
|
|
project = @team.projects.create!(project_params.merge!(created_by: current_user))
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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
|