Retrospring/app/controllers/ajax/question_controller.rb

62 lines
1.6 KiB
Ruby
Raw Normal View History

2022-06-12 04:46:48 -07:00
# frozen_string_literal: true
require "errors"
require "use_case/question/create"
require "use_case/question/create_followers"
class Ajax::QuestionController < AjaxController
2015-04-25 18:36:25 -07:00
def destroy
params.require :question
question = Question.find params[:question]
if question.nil?
@response[:status] = :not_found
@response[:message] = t(".notfound")
2015-04-25 18:36:25 -07:00
return
end
unless current_user&.mod? || question.user == current_user
@response[:status] = :not_authorized
@response[:message] = t(".noauth")
2015-04-25 18:39:51 -07:00
return
end
2015-04-25 18:36:25 -07:00
question.destroy!
@response[:status] = :okay
@response[:message] = t(".success")
@response[:success] = true
2015-04-25 18:36:25 -07:00
end
def create
params.require :question
params.require :anonymousQuestion
params.require :rcpt
# set up fake success response -- the use cases raise errors on exceptions
# which get rescued by the base class
@response = {
success: true,
message: t(".success"),
status: :okay
}
if user_signed_in? && params[:rcpt] == "followers"
UseCase::Question::CreateFollowers.call(
2022-01-24 13:46:11 -08:00
source_user_id: current_user.id,
content: params[:question],
author_identifier: AnonymousBlock.get_identifier(request.ip)
)
return
end
UseCase::Question::Create.call(
2022-01-24 13:46:11 -08:00
source_user_id: user_signed_in? ? current_user.id : nil,
target_user_id: params[:rcpt],
content: params[:question],
anonymous: params[:anonymousQuestion],
author_identifier: AnonymousBlock.get_identifier(request.ip)
)
end
end