Replace logic in Ajax::ReportController with use case call

This commit is contained in:
Andreas Nedbal 2025-03-08 15:09:51 +01:00 committed by Andreas Nedbal
parent e4624ce260
commit ae02840c69

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Ajax::ReportController < AjaxController class Ajax::ReportController < AjaxController
def create def create
params.require :id params.require :id
@ -11,35 +13,19 @@ class Ajax::ReportController < AjaxController
return return
end end
unless %w(answer comment question user).include? params[:type] result = UseCase::Report::Create.call(
@response[:message] = t(".unknown") reporter_id: current_user.id,
return object_id: params[:id],
object_type: params[:type],
reason: params[:reason],
)
if result[:status] == 201
@response[:status] = :okay
@response[:message] = t(".success", parameter: params[:type].titleize)
@response[:success] = true
else
@response[:message] = t(".error")
end end
obj = params[:type].strip.capitalize
object = case obj
when 'User'
User.find_by_screen_name! params[:id]
when 'Question'
Question.find params[:id]
when 'Answer'
Answer.find params[:id]
when 'Comment'
Comment.find params[:id]
else
Answer.find params[:id]
end
if object.nil?
@response[:message] = t(".notfound", parameter: params[:type])
return
end
current_user.report object, params[:reason]
@response[:status] = :okay
@response[:message] = t(".success", parameter: params[:type].titleize)
@response[:success] = true
end end
end end