diff --git a/app/controllers/ajax/answer_controller.rb b/app/controllers/ajax/answer_controller.rb new file mode 100644 index 00000000..245fa12f --- /dev/null +++ b/app/controllers/ajax/answer_controller.rb @@ -0,0 +1,37 @@ +class Ajax::AnswerController < ApplicationController + def destroy + params.require :answer + + unless current_user.nil? + current_user.increment! :asked_count unless params[:anonymousQuestion] == 'true' + end + + answer = Answer.find(params[:answer]) + + unless answer.user == current_user || privileged? + @status = :nopriv + @message = "check yuor privlegs" + @success = false + return + end + + answer.user.decrement! :answered_count + Inbox.create!(user: answer.user, question: answer.question, new: true) + answer.destroy + + @status = :okay + @message = "Successfully deleted answer." + @success = true + end + + private + + # TODO: + def privileged? + if current_user && current_user.admin? + true + else + false + end + end +end diff --git a/app/views/ajax/answer/destroy.json.jbuilder b/app/views/ajax/answer/destroy.json.jbuilder new file mode 100644 index 00000000..2c193af4 --- /dev/null +++ b/app/views/ajax/answer/destroy.json.jbuilder @@ -0,0 +1 @@ +json.partial! 'ajax/shared/status' \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 093fe72a..65a6b651 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,6 +26,7 @@ Rails.application.routes.draw do namespace :ajax do match '/ask', to: 'question#create', via: :post, as: :ask match '/answer', to: 'inbox#destroy', via: :post, as: :answer + match '/destroy_answer', to: 'answer#destroy', via: :post, as: :destroy_answer end match '/inbox', to: 'inbox#show', via: 'get'