Commit 6a76d6ea authored by Jarka Košanová's avatar Jarka Košanová

Use ServiceResponse in Boards::CreateService

- refactor the service to use ServiceResponse
- change affected classes and specs
parent e96063ee
...@@ -21,11 +21,13 @@ module MultipleBoardsActions ...@@ -21,11 +21,13 @@ module MultipleBoardsActions
end end
def create 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| respond_to do |format|
format.json do format.json do
if board.persisted? board = response.payload
if response.success?
extra_json = { board_path: board_path(board) } extra_json = { board_path: board_path(board) }
render json: serialize_as_json(board).merge(extra_json) render json: serialize_as_json(board).merge(extra_json)
else else
......
...@@ -3,7 +3,11 @@ ...@@ -3,7 +3,11 @@
module Boards module Boards
class CreateService < Boards::BaseService class CreateService < Boards::BaseService
def execute 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 end
private private
...@@ -15,12 +19,16 @@ module Boards ...@@ -15,12 +19,16 @@ module Boards
def create_board! def create_board!
board = parent.boards.create(params) board = parent.boards.create(params)
if board.persisted? unless board.persisted?
board.lists.create(list_type: :backlog) return ServiceResponse.error(message: "There was an error when creating a board.", payload: board)
board.lists.create(list_type: :closed) end
board.tap do |created_board|
created_board.lists.create(list_type: :backlog)
created_board.lists.create(list_type: :closed)
end end
board ServiceResponse.success(payload: board)
end end
end end
end end
......
...@@ -10,10 +10,10 @@ module EE ...@@ -10,10 +10,10 @@ module EE
def create_board def create_board
forbidden! unless board_parent.multiple_issue_boards_available? forbidden! unless board_parent.multiple_issue_boards_available?
board = response =
::Boards::CreateService.new(board_parent, current_user, { name: params[:name] }).execute ::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 end
def update_board def update_board
......
...@@ -3,6 +3,10 @@ ...@@ -3,6 +3,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Boards::CreateService, services: true do RSpec.describe Boards::CreateService, services: true do
def created_board
service.execute.payload
end
shared_examples 'boards create service' do shared_examples 'boards create service' do
context 'With the feature available' do context 'With the feature available' do
before do before do
...@@ -16,8 +20,12 @@ RSpec.describe Boards::CreateService, services: true do ...@@ -16,8 +20,12 @@ RSpec.describe Boards::CreateService, services: true do
expect { service.execute }.to change(parent.boards, :count).by(1) expect { service.execute }.to change(parent.boards, :count).by(1)
end end
it 'returns a successful response' do
expect(service.execute).to be_success
end
it 'creates the default lists' do it 'creates the default lists' do
board = service.execute board = created_board
expect(board.lists.size).to eq 2 expect(board.lists.size).to eq 2
expect(board.lists.first).to be_backlog expect(board.lists.first).to be_backlog
...@@ -32,10 +40,12 @@ RSpec.describe Boards::CreateService, services: true do ...@@ -32,10 +40,12 @@ RSpec.describe Boards::CreateService, services: true do
expect { service.execute }.not_to change(parent.boards, :count) expect { service.execute }.not_to change(parent.boards, :count)
end end
it "does not create board's default lists" do it 'returns an error response' do
board = service.execute 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
end end
...@@ -46,8 +56,12 @@ RSpec.describe Boards::CreateService, services: true do ...@@ -46,8 +56,12 @@ RSpec.describe Boards::CreateService, services: true do
expect { service.execute }.to change(parent.boards, :count).by(1) expect { service.execute }.to change(parent.boards, :count).by(1)
end end
it 'returns a successful response' do
expect(service.execute).to be_success
end
it "creates board's default lists" do it "creates board's default lists" do
board = service.execute board = created_board
expect(board.lists.size).to eq 2 expect(board.lists.size).to eq 2
expect(board.lists.last).to be_closed expect(board.lists.last).to be_closed
...@@ -68,13 +82,13 @@ RSpec.describe Boards::CreateService, services: true do ...@@ -68,13 +82,13 @@ RSpec.describe Boards::CreateService, services: true do
stub_licensed_features(multiple_group_issue_boards: false) stub_licensed_features(multiple_group_issue_boards: false)
service = described_class.new(parent, double) 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) expect { service.execute }.not_to change(parent.boards, :count)
end end
end end
it_behaves_like 'setting a milestone scope' do 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 end
end end
...@@ -7,7 +7,7 @@ RSpec.shared_examples 'boards create service' do ...@@ -7,7 +7,7 @@ RSpec.shared_examples 'boards create service' do
end end
it 'creates the default lists' do it 'creates the default lists' do
board = service.execute board = service.execute.payload
expect(board.lists.size).to eq 2 expect(board.lists.size).to eq 2
expect(board.lists.first).to be_backlog 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