diff --git a/app/controllers/ajax/question_controller.rb b/app/controllers/ajax/question_controller.rb index e2963804..78b88635 100644 --- a/app/controllers/ajax/question_controller.rb +++ b/app/controllers/ajax/question_controller.rb @@ -3,25 +3,16 @@ require "errors" require "use_case/question/create" require "use_case/question/create_followers" +require "use_case/question/destroy" class Ajax::QuestionController < AjaxController def destroy params.require :question - question = Question.find params[:question] - if question.nil? - @response[:status] = :not_found - @response[:message] = t(".notfound") - return - end - - unless current_user&.mod? || question.user == current_user - @response[:status] = :not_authorized - @response[:message] = t(".noauth") - return - end - - question.destroy! + UseCase::Question::Destroy.call( + question_id: params[:question], + current_user: current_user + ) @response[:status] = :okay @response[:message] = t(".success") diff --git a/lib/use_case/question/destroy.rb b/lib/use_case/question/destroy.rb new file mode 100644 index 00000000..5ffc8f54 --- /dev/null +++ b/lib/use_case/question/destroy.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require "use_case/base" +require "errors" + +module UseCase + module Question + class Destroy < UseCase::Base + option :question_id, type: Types::Coercible::Integer + option :current_user, type: Types::Instance(::User) + + def call + question = ::Question.find(question_id) + + raise Errors::Forbidden unless current_user&.mod? || question.user == current_user + + question.destroy! + end + end + end +end diff --git a/spec/controllers/ajax/question_controller_spec.rb b/spec/controllers/ajax/question_controller_spec.rb index 98f6d683..cd3166a4 100644 --- a/spec/controllers/ajax/question_controller_spec.rb +++ b/spec/controllers/ajax/question_controller_spec.rb @@ -428,7 +428,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when the question exists and was not made by the current user" do let(:question_user) { FactoryBot.create(:user) } - include_examples "does not delete the question", "not_authorized" + include_examples "does not delete the question", "forbidden" %i[moderator administrator].each do |privileged_role| context "when the current user is a #{privileged_role}" do @@ -446,7 +446,7 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when the question exists and was not made by any registered user" do let(:question_user) { nil } - include_examples "does not delete the question", "not_authorized" + include_examples "does not delete the question", "forbidden" %i[moderator administrator].each do |privileged_role| context "when the current user is a #{privileged_role}" do @@ -470,12 +470,14 @@ describe Ajax::QuestionController, :ajax_controller, type: :controller do context "when the question is an invalid value" do let(:question_id) { "sonic_the_hedgehog" } - include_examples "does not delete the question", "not_found" + # This case returns an invalid parameter error in the use case due to a type constraint + include_examples "does not delete the question", "err" end end context "when user is not signed in" do - include_examples "does not delete the question", "not_authorized" + # This case returns an invalid parameter error in the use case due to a type constraint + include_examples "does not delete the question", "err" end end end