BigW Consortium Gitlab

Refactoring service to create a new issue in a board list

parent 81c253de
...@@ -16,9 +16,8 @@ module Projects ...@@ -16,9 +16,8 @@ module Projects
end end
def create def create
list = project.board.lists.find(params[:list_id])
service = ::Boards::Issues::CreateService.new(project, current_user, issue_params) service = ::Boards::Issues::CreateService.new(project, current_user, issue_params)
issue = service.execute(list) issue = service.execute
if issue.valid? if issue.valid?
render json: serialize_as_json(issue) render json: serialize_as_json(issue)
...@@ -68,7 +67,7 @@ module Projects ...@@ -68,7 +67,7 @@ module Projects
end end
def issue_params def issue_params
params.require(:issue).permit(:title).merge(request: request) params.require(:issue).permit(:title).merge(board_id: params[:board_id], list_id: params[:list_id], request: request)
end end
def serialize_as_json(resource) def serialize_as_json(resource)
......
module Boards module Boards
module Issues module Issues
class CreateService < Boards::BaseService class CreateService < Boards::BaseService
def execute(list) def execute
params.merge!(label_ids: [list.label_id]) create_issue(params.merge(label_ids: [list.label_id]))
create_issue
end end
private private
def create_issue def board
@board ||= project.boards.find(params.delete(:board_id))
end
def list
@list ||= board.lists.find(params.delete(:list_id))
end
def create_issue(params)
::Issues::CreateService.new(project, current_user, params).execute ::Issues::CreateService.new(project, current_user, params).execute
end end
end end
......
...@@ -76,13 +76,13 @@ describe Projects::Boards::IssuesController do ...@@ -76,13 +76,13 @@ describe Projects::Boards::IssuesController do
describe 'POST create' do describe 'POST create' do
context 'with valid params' do context 'with valid params' do
it 'returns a successful 200 response' do it 'returns a successful 200 response' do
create_issue user: user, list: list1, title: 'New issue' create_issue user: user, board: board, list: list1, title: 'New issue'
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
end end
it 'returns the created issue' do it 'returns the created issue' do
create_issue user: user, list: list1, title: 'New issue' create_issue user: user, board: board, list: list1, title: 'New issue'
expect(response).to match_response_schema('issue') expect(response).to match_response_schema('issue')
end end
...@@ -91,7 +91,7 @@ describe Projects::Boards::IssuesController do ...@@ -91,7 +91,7 @@ describe Projects::Boards::IssuesController do
context 'with invalid params' do context 'with invalid params' do
context 'when title is nil' do context 'when title is nil' do
it 'returns an unprocessable entity 422 response' do it 'returns an unprocessable entity 422 response' do
create_issue user: user, list: list1, title: nil create_issue user: user, board: board, list: list1, title: nil
expect(response).to have_http_status(422) expect(response).to have_http_status(422)
end end
...@@ -101,7 +101,7 @@ describe Projects::Boards::IssuesController do ...@@ -101,7 +101,7 @@ describe Projects::Boards::IssuesController do
it 'returns a not found 404 response' do it 'returns a not found 404 response' do
list = create(:list) list = create(:list)
create_issue user: user, list: list, title: 'New issue' create_issue user: user, board: board, list: list, title: 'New issue'
expect(response).to have_http_status(404) expect(response).to have_http_status(404)
end end
...@@ -110,17 +110,18 @@ describe Projects::Boards::IssuesController do ...@@ -110,17 +110,18 @@ describe Projects::Boards::IssuesController do
context 'with unauthorized user' do context 'with unauthorized user' do
it 'returns a forbidden 403 response' do it 'returns a forbidden 403 response' do
create_issue user: guest, list: list1, title: 'New issue' create_issue user: guest, board: board, list: list1, title: 'New issue'
expect(response).to have_http_status(403) expect(response).to have_http_status(403)
end end
end end
def create_issue(user:, list:, title:) def create_issue(user:, board:, list:, title:)
sign_in(user) sign_in(user)
post :create, namespace_id: project.namespace.to_param, post :create, namespace_id: project.namespace.to_param,
project_id: project.to_param, project_id: project.to_param,
board_id: board.to_param,
list_id: list.to_param, list_id: list.to_param,
issue: { title: title }, issue: { title: title },
format: :json format: :json
......
...@@ -2,13 +2,13 @@ require 'spec_helper' ...@@ -2,13 +2,13 @@ require 'spec_helper'
describe Boards::Issues::CreateService, services: true do describe Boards::Issues::CreateService, services: true do
describe '#execute' do describe '#execute' do
let(:project) { create(:project_with_board) } let(:project) { create(:empty_project) }
let(:board) { project.board } let(:board) { create(:board, project: project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:label) { create(:label, project: project, name: 'in-progress') } let(:label) { create(:label, project: project, name: 'in-progress') }
let!(:list) { create(:list, board: board, label: label, position: 0) } let!(:list) { create(:list, board: board, label: label, position: 0) }
subject(:service) { described_class.new(project, user, title: 'New issue') } subject(:service) { described_class.new(project, user, board_id: board.id, list_id: list.id, title: 'New issue') }
before do before do
project.team << [user, :developer] project.team << [user, :developer]
...@@ -17,15 +17,15 @@ describe Boards::Issues::CreateService, services: true do ...@@ -17,15 +17,15 @@ describe Boards::Issues::CreateService, services: true do
it 'delegates the create proceedings to Issues::CreateService' do it 'delegates the create proceedings to Issues::CreateService' do
expect_any_instance_of(Issues::CreateService).to receive(:execute).once expect_any_instance_of(Issues::CreateService).to receive(:execute).once
service.execute(list) service.execute
end end
it 'creates a new issue' do it 'creates a new issue' do
expect { service.execute(list) }.to change(project.issues, :count).by(1) expect { service.execute }.to change(project.issues, :count).by(1)
end end
it 'adds the label of the list to the issue' do it 'adds the label of the list to the issue' do
issue = service.execute(list) issue = service.execute
expect(issue.labels).to eq [label] expect(issue.labels).to eq [label]
end end
......
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