Skip to content

Commit

Permalink
Add topics interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindew committed Apr 17, 2019
1 parent 09dbe27 commit 8673ced
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
25 changes: 11 additions & 14 deletions app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@ def edit
end

def update
Edition.find_and_lock_current(document: params[:document]) do |edition|
begin
edition.document_topics.patch(params.fetch(:topics, []), params[:version].to_i)
result = Topics::UpdateInteractor.call(params: params, user: current_user)
api_conflict, api_errored = result.to_h.values_at(:api_conflict, :api_errored)

redirect_to document_path(edition.document),
notice: t("documents.show.flashes.topics_updated")
rescue GdsApi::HTTPConflict
Rails.logger.warn("Conflict updating topics for #{edition.content_id} at version #{params[:version].to_i}")
redirect_to topics_path(edition.document),
alert_with_description: t("topics.edit.flashes.topic_update_conflict")
rescue GdsApi::BaseError => e
GovukError.notify(e)
redirect_to edition.document,
alert_with_description: t("documents.show.flashes.topic_update_error")
end
if api_conflict
redirect_to topics_path(params[:document]),
alert_with_description: t("topics.edit.flashes.topic_update_conflict")
elsif api_errored
redirect_to document_path(params[:document]),
alert_with_description: t("documents.show.flashes.topic_update_error")
else
redirect_to document_path(params[:document]),
notice: t("documents.show.flashes.topics_updated")
end
end
end
35 changes: 35 additions & 0 deletions app/interactors/topics/update_interactor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

class Topics::UpdateInteractor
include Interactor

delegate :params,
:user,
:edition,
:api_conflict,
:api_errored,
to: :context

def call
Edition.transaction do
find_and_lock_edition
update_topics
end
end

private

def find_and_lock_edition
context.edition = Edition.lock.find_current(document: params[:document])
end

def update_topics
edition.document_topics.patch(params.fetch(:topics, []), params[:version].to_i)
rescue GdsApi::HTTPConflict
Rails.logger.warn("Conflict updating topics for #{edition.content_id} at version #{params[:version].to_i}")
context.fail!(api_conflict: true)
rescue GdsApi::BaseError => e
GovukError.notify(e)
context.fail!(api_errored: true)
end
end

0 comments on commit 8673ced

Please sign in to comment.