debian-mirror-gitlab/app/controllers/projects/boards/issues_controller.rb

94 lines
2.7 KiB
Ruby
Raw Normal View History

2016-09-13 17:45:13 +05:30
module Projects
module Boards
class IssuesController < Boards::ApplicationController
before_action :authorize_read_issue!, only: [:index]
2016-11-03 12:29:30 +05:30
before_action :authorize_create_issue!, only: [:create]
2016-09-13 17:45:13 +05:30
before_action :authorize_update_issue!, only: [:update]
def index
issues = ::Boards::Issues::ListService.new(project, current_user, filter_params).execute
2017-08-17 22:00:37 +05:30
issues = issues.page(params[:page]).per(params[:per] || 20)
make_sure_position_is_set(issues)
2016-09-13 17:45:13 +05:30
2016-09-29 09:46:39 +05:30
render json: {
2016-11-03 12:29:30 +05:30
issues: serialize_as_json(issues),
2016-09-29 09:46:39 +05:30
size: issues.total_count
}
2016-09-13 17:45:13 +05:30
end
2016-11-03 12:29:30 +05:30
def create
service = ::Boards::Issues::CreateService.new(project, current_user, issue_params)
issue = service.execute
if issue.valid?
render json: serialize_as_json(issue)
else
render json: issue.errors, status: :unprocessable_entity
end
end
2016-09-13 17:45:13 +05:30
def update
service = ::Boards::Issues::MoveService.new(project, current_user, move_params)
if service.execute(issue)
head :ok
else
head :unprocessable_entity
end
end
private
2017-08-17 22:00:37 +05:30
def make_sure_position_is_set(issues)
issues.each do |issue|
issue.move_to_end && issue.save unless issue.relative_position
end
end
2016-09-13 17:45:13 +05:30
def issue
@issue ||=
2016-11-03 12:29:30 +05:30
IssuesFinder.new(current_user, project_id: project.id)
2016-09-13 17:45:13 +05:30
.execute
.where(iid: params[:id])
.first!
end
def authorize_read_issue!
return render_403 unless can?(current_user, :read_issue, project)
end
2016-11-03 12:29:30 +05:30
def authorize_create_issue!
return render_403 unless can?(current_user, :admin_issue, project)
end
2016-09-13 17:45:13 +05:30
def authorize_update_issue!
return render_403 unless can?(current_user, :update_issue, issue)
end
def filter_params
2017-08-17 22:00:37 +05:30
params.merge(board_id: params[:board_id], id: params[:list_id]).compact
2016-09-13 17:45:13 +05:30
end
def move_params
2017-08-17 22:00:37 +05:30
params.permit(:board_id, :id, :from_list_id, :to_list_id, :move_before_iid, :move_after_iid)
2016-11-03 12:29:30 +05:30
end
def issue_params
params.require(:issue).permit(:title).merge(board_id: params[:board_id], list_id: params[:list_id], request: request)
end
def serialize_as_json(resource)
resource.as_json(
labels: true,
2017-08-17 22:00:37 +05:30
only: [:id, :iid, :title, :confidential, :due_date, :relative_position],
2016-11-03 12:29:30 +05:30
include: {
2017-08-17 22:00:37 +05:30
assignees: { only: [:id, :name, :username], methods: [:avatar_url] },
milestone: { only: [:id, :title] }
},
user: current_user
)
2016-09-13 17:45:13 +05:30
end
end
end
end