-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from andela-iamadi/ft-refactor-comments-contro…
…ller Ft refactor comments controller
- Loading branch information
Showing
11 changed files
with
311 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,56 @@ | ||
class CommentsController < ApplicationController | ||
before_action :custom_initializer | ||
attr_reader :user_id, :question_id, :answer_id, :id, :content | ||
|
||
include Common | ||
before_action :set_resource | ||
before_action :set_comment, only: [:show, :update, :destroy] | ||
include OwnershipConcern | ||
|
||
def index | ||
question = Question.find_by(id: question_id) if action_on_question | ||
answer = Answer.find_by(id: answer_id) if action_on_answer | ||
comments = question.comments if question | ||
comments = answer.comments if answer | ||
render json: comments, status: 200 unless comments.nil? | ||
render json: { error: false }, status: 404 if comments.nil? | ||
render json: @resource_comments, status: 200 | ||
end | ||
|
||
def show | ||
comments = Question.find_question_comment(question_id, id) if action_on_question | ||
comments = Answer.find_answer_comment(answer_id, id) if action_on_answer | ||
render json: comments , status: 200 | ||
rescue | ||
render json: { error: false }, status: 404 | ||
render json: @comment, status: 200 | ||
end | ||
|
||
def create | ||
unless content.nil? || content == "" | ||
if action_on_question | ||
comments = Question.add_comment_to_question(question_id, user_id, content) | ||
elsif action_on_answer | ||
comments = Answer.add_comment_to_answer(answer_id, user_id, content) | ||
end | ||
render json: comments, root: false | ||
comment = @resource_comments.new(content: comment_params[:content]) | ||
comment.user = current_user | ||
if comment.save | ||
render json: comment, status: 201 | ||
else | ||
render json: { error: "Comment body can not be empty!" }, status: 403 | ||
invalid_request("Comment body can not be empty!") | ||
end | ||
end | ||
|
||
def update | ||
status = update_subject(id, user_id, "content", content) if content && content != "" | ||
message = { response: status ? true : false } | ||
render json: message | ||
rescue | ||
render json: { error: false }, status: 403 | ||
if @comment.update(content: comment_params[:content]) | ||
render json: @comment, status: 200 | ||
else | ||
invalid_request("Comment body can not be empty!") | ||
end | ||
end | ||
|
||
def destroy | ||
if action_on_question | ||
deleted if Question.delete_question_comment(id, user_id, question_id) | ||
elsif action_on_answer | ||
deleted if Answer.delete_answer_comment(id, user_id, answer_id) | ||
if @comment.try(:destroy) | ||
render json: :head, status: 204 | ||
else | ||
invalid_request | ||
end | ||
rescue | ||
render json: { error: false }, status: 403 | ||
end | ||
|
||
private | ||
|
||
def comment_params | ||
params.permit(:question_id, :answer_id, :id, :content, :downvote, :upvote) | ||
params.permit(:resource_name, :resource_id, :content, :id) | ||
end | ||
|
||
def custom_initializer | ||
set_vars(comment_params) | ||
def set_resource | ||
resource = comment_params[:resource_name].singularize.camelize.constantize.find_by(id: comment_params[:resource_id]) | ||
resource_not_found && return unless resource | ||
@resource_comments = resource.comments | ||
end | ||
|
||
def deleted | ||
render json: { response: "Comment deleted." }, status: 410 | ||
end | ||
|
||
def update_subject(id, user_id, attribute, value = nil) | ||
if action_on_question | ||
comments = Question.update_question_comment(id, user_id, question_id, attribute, value) | ||
elsif action_on_answer | ||
comments = Answer.update_answer_comment(id, user_id, answer_id, attribute, value) | ||
end | ||
def set_comment | ||
@comment = @resource_comments.find_by(id: comment_params[:id]) | ||
resource_not_found && return unless @comment | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,20 @@ | ||
class VotesController < ApplicationController | ||
def upvote | ||
resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize | ||
vote = Vote.act_on_vote('plus', resource_to_upvote, vote_params[:id], current_user) | ||
vote = Vote.act_on_vote('plus', resource_to_upvote, vote_params[:resource_id], current_user) | ||
render json: { response: vote }, status: 200 unless vote.nil? | ||
render json: { error: "Invalid vote!" }, status: 403 if vote.nil? | ||
end | ||
|
||
def downvote | ||
resource_to_upvote = vote_params[:resource_name].singularize.camelize.constantize | ||
vote = Vote.act_on_vote('minus', resource_to_upvote, vote_params[:id], current_user) | ||
vote = Vote.act_on_vote('minus', resource_to_upvote, vote_params[:resource_id], current_user) | ||
render json: { response: vote }, status: 200 unless vote.nil? | ||
render json: { error: "Invalid vote!" }, status: 403 if vote.nil? | ||
end | ||
|
||
private | ||
def vote_params | ||
params.permit(:resource_name, :id) | ||
params.permit(:resource_name, :resource_id) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class CommentSerializer < MainSerializer | ||
|
||
attributes :user | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.