Commit 5fb1e44f authored by Igor Drozdov's avatar Igor Drozdov

Merge branch 'refactor-create-board-service' into 'master'

Use ServiceResponse in Boards::CreateService

See merge request gitlab-org/gitlab!43398
parents 264140ed 6a76d6ea
......@@ -21,11 +21,13 @@ module MultipleBoardsActions
end
def create
board = Boards::CreateService.new(parent, current_user, board_params).execute
response = Boards::CreateService.new(parent, current_user, board_params).execute
respond_to do |format|
format.json do
if board.persisted?
board = response.payload
if response.success?
extra_json = { board_path: board_path(board) }
render json: serialize_as_json(board).merge(extra_json)
else
......
......@@ -3,7 +3,11 @@
module Boards
class CreateService < Boards::BaseService
def execute
create_board! if can_create_board?
unless can_create_board?
return ServiceResponse.error(message: "You don't have the permission to create a board for this resource.")
end
create_board!
end
private
......@@ -15,12 +19,16 @@ module Boards
def create_board!
board = parent.boards.create(params)
if board.persisted?
board.lists.create(list_type: :backlog)
board.lists.create(list_type: :closed)
unless board.persisted?
return ServiceResponse.error(message: "There was an error when creating a board.", payload: board)
end
board.tap do |created_board|
created_board.lists.create(list_type: :backlog)
created_board.lists.create(list_type: :closed)
end
board
ServiceResponse.success(payload: board)
end
end
end
......
......@@ -10,10 +10,10 @@ module EE
def create_board
forbidden! unless board_parent.multiple_issue_boards_available?
board =
response =
::Boards::CreateService.new(board_parent, current_user, { name: params[:name] }).execute
present board, with: ::API::Entities::Board
present response.payload, with: ::API::Entities::Board
end
def update_board
......
......@@ -3,6 +3,10 @@
require 'spec_helper'
RSpec.describe Boards::CreateService, services: true do
def created_board
service.execute.payload
end
shared_examples 'boards create service' do
context 'With the feature available' do
before do
......@@ -16,8 +20,12 @@ RSpec.describe Boards::CreateService, services: true do
expect { service.execute }.to change(parent.boards, :count).by(1)
end
it 'returns a successful response' do
expect(service.execute).to be_success
end
it 'creates the default lists' do
board = service.execute
board = created_board
expect(board.lists.size).to eq 2
expect(board.lists.first).to be_backlog
......@@ -32,10 +40,12 @@ RSpec.describe Boards::CreateService, services: true do
expect { service.execute }.not_to change(parent.boards, :count)
end
it "does not create board's default lists" do
board = service.execute
it 'returns an error response' do
expect(service.execute).to be_error
end
expect(board.lists.size).to eq 0
it "does not create board's default lists" do
expect(created_board.lists.size).to eq 0
end
end
......@@ -46,8 +56,12 @@ RSpec.describe Boards::CreateService, services: true do
expect { service.execute }.to change(parent.boards, :count).by(1)
end
it 'returns a successful response' do
expect(service.execute).to be_success
end
it "creates board's default lists" do
board = service.execute
board = created_board
expect(board.lists.size).to eq 2
expect(board.lists.last).to be_closed
......@@ -68,13 +82,13 @@ RSpec.describe Boards::CreateService, services: true do
stub_licensed_features(multiple_group_issue_boards: false)
service = described_class.new(parent, double)
expect(service.execute).not_to be_nil
expect(service.execute.payload).not_to be_nil
expect { service.execute }.not_to change(parent.boards, :count)
end
end
it_behaves_like 'setting a milestone scope' do
subject { described_class.new(parent, double, milestone_id: milestone.id).execute }
subject { described_class.new(parent, double, milestone_id: milestone.id).execute.payload }
end
end
end
......@@ -7,7 +7,7 @@ RSpec.shared_examples 'boards create service' do
end
it 'creates the default lists' do
board = service.execute
board = service.execute.payload
expect(board.lists.size).to eq 2
expect(board.lists.first).to be_backlog
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment