Create use case for deleting questions
This commit is contained in:
parent
72906ca549
commit
0e9be78588
|
@ -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")
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue