mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-03-13 17:09:59 +01:00
Replace logic in Ajax::ReportController with use case call
This commit is contained in:
parent
e4624ce260
commit
ae02840c69
1 changed files with 15 additions and 29 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue