Commit d76b63ea authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch '217354-service-respone-create' into 'master'

Use ServiceResponse in wiki create service

Closes #217354

See merge request gitlab-org/gitlab!38068
parents 304eb13b 49dc5e3c
......@@ -111,14 +111,16 @@ module WikiActions
# rubocop:disable Gitlab/ModuleWithInstanceVariables
def create
@page = WikiPages::CreateService.new(container: container, current_user: current_user, params: wiki_params).execute
response = WikiPages::CreateService.new(container: container, current_user: current_user, params: wiki_params).execute
@page = response.payload[:page]
if page.persisted?
if response.success?
redirect_to(
wiki_page_path(wiki, page),
notice: _('Wiki was successfully updated.')
)
else
flash[:alert] = response.message
render 'shared/wikis/edit'
end
rescue Gitlab::Git::Wiki::OperationError => e
......
......@@ -10,7 +10,11 @@ module WikiPages
execute_hooks(page)
end
page
if page.persisted?
ServiceResponse.success(payload: { page: page })
else
ServiceResponse.error(message: _('Could not create wiki page'), payload: { page: page })
end
end
def usage_counter_action
......
......@@ -61,9 +61,10 @@ module API
post ':id/wikis' do
authorize! :create_wiki, container
page = WikiPages::CreateService.new(container: container, current_user: current_user, params: params).execute
response = WikiPages::CreateService.new(container: container, current_user: current_user, params: params).execute
page = response.payload[:page]
if page.valid?
if response.success?
present page, with: Entities::WikiPage
else
render_validation_error!(page)
......
......@@ -6934,6 +6934,9 @@ msgstr ""
msgid "Could not create project"
msgstr ""
msgid "Could not create wiki page"
msgstr ""
msgid "Could not delete %{design}. Please try again."
msgstr ""
......
......@@ -355,6 +355,44 @@ RSpec.shared_examples 'wiki controller actions' do
end
end
describe 'POST #create' do
let(:new_title) { 'New title' }
let(:new_content) { 'New content' }
subject do
post(:create,
params: routing_params.merge(
wiki: { title: new_title, content: new_content }
))
end
context 'when page is valid' do
it 'creates the page' do
expect do
subject
end.to change { wiki.list_pages.size }.by 1
wiki_page = wiki.find_page(new_title)
expect(wiki_page.title).to eq new_title
expect(wiki_page.content).to eq new_content
end
end
context 'when page is not valid' do
let(:new_title) { '' }
it 'renders the edit state' do
expect do
subject
end.not_to change { wiki.list_pages.size }
expect(response).to render_template('shared/wikis/edit')
expect(flash[:alert]).to eq('Could not create wiki page')
end
end
end
def redirect_to_wiki(wiki, page)
redirect_to(controller.wiki_page_path(wiki, page))
end
......
......@@ -16,8 +16,10 @@ RSpec.shared_examples 'WikiPages::CreateService#execute' do |container_type|
subject(:service) { described_class.new(container: container, current_user: user, params: opts) }
it 'creates wiki page with valid attributes' do
page = service.execute
response = service.execute
page = response.payload[:page]
expect(response).to be_success
expect(page).to be_valid
expect(page).to be_persisted
expect(page.title).to eq(opts[:title])
......@@ -77,7 +79,12 @@ RSpec.shared_examples 'WikiPages::CreateService#execute' do |container_type|
end
it 'reports the error' do
expect(service.execute).to be_invalid
response = service.execute
page = response.payload[:page]
expect(response).to be_error
expect(page).to be_invalid
.and have_attributes(errors: be_present)
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