From ae02840c696342cd16be199c204e42d7270acfeb Mon Sep 17 00:00:00 2001 From: Andreas Nedbal Date: Sat, 8 Mar 2025 15:09:51 +0100 Subject: [PATCH] Replace logic in Ajax::ReportController with use case call --- app/controllers/ajax/report_controller.rb | 44 ++++++++--------------- 1 file changed, 15 insertions(+), 29 deletions(-) diff --git a/app/controllers/ajax/report_controller.rb b/app/controllers/ajax/report_controller.rb index 6451bd6d..d61d0f81 100644 --- a/app/controllers/ajax/report_controller.rb +++ b/app/controllers/ajax/report_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class Ajax::ReportController < AjaxController def create params.require :id @@ -11,35 +13,19 @@ class Ajax::ReportController < AjaxController return end - unless %w(answer comment question user).include? params[:type] - @response[:message] = t(".unknown") - return + result = UseCase::Report::Create.call( + reporter_id: current_user.id, + 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 - - 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