2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-13 17:45:13 +05:30
|
|
|
module Boards
|
|
|
|
module Lists
|
2018-03-17 18:26:18 +05:30
|
|
|
class CreateService < Boards::BaseService
|
2018-11-08 19:23:39 +05:30
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2016-11-03 12:29:30 +05:30
|
|
|
def execute(board)
|
2021-02-22 17:27:13 +05:30
|
|
|
list = case type
|
|
|
|
when :backlog
|
|
|
|
create_backlog(board)
|
|
|
|
else
|
|
|
|
target = target(board)
|
|
|
|
position = next_position(board)
|
|
|
|
|
|
|
|
return ServiceResponse.error(message: _('%{board_target} not found') % { board_target: type.to_s.capitalize }) if target.blank?
|
|
|
|
|
|
|
|
create_list(board, type, target, position)
|
|
|
|
end
|
|
|
|
|
|
|
|
return ServiceResponse.error(message: list.errors.full_messages) unless list.persisted?
|
|
|
|
|
|
|
|
ServiceResponse.success(payload: { list: list })
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def type
|
2020-10-24 23:57:45 +05:30
|
|
|
# We don't ever expect to have more than one list
|
|
|
|
# type param at once.
|
|
|
|
if params.key?('backlog')
|
|
|
|
:backlog
|
|
|
|
else
|
|
|
|
:label
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def target(board)
|
|
|
|
strong_memoize(:target) do
|
2021-02-22 17:27:13 +05:30
|
|
|
available_labels.find_by(id: params[:label_id]) # rubocop: disable CodeReuse/ActiveRecord
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
def available_labels
|
|
|
|
::Labels::AvailableLabelsService.new(current_user, parent, {})
|
|
|
|
.available_labels
|
2016-11-03 12:29:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def next_position(board)
|
2016-09-13 17:45:13 +05:30
|
|
|
max_position = board.lists.movable.maximum(:position)
|
|
|
|
max_position.nil? ? 0 : max_position.succ
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def create_list(board, type, target, position)
|
2019-12-21 20:55:43 +05:30
|
|
|
board.lists.create(create_list_attributes(type, target, position))
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_list_attributes(type, target, position)
|
|
|
|
{ type => target, list_type: type, position: position }
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
def create_backlog(board)
|
|
|
|
return board.lists.backlog.first if board.lists.backlog.exists?
|
|
|
|
|
|
|
|
board.lists.create(list_type: :backlog, position: nil)
|
|
|
|
end
|
2016-09-13 17:45:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 20:38:33 +05:30
|
|
|
|
|
|
|
Boards::Lists::CreateService.prepend_if_ee('EE::Boards::Lists::CreateService')
|