Refactoring service to create a new issue in a board list

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