diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..da07aa6 Binary files /dev/null and b/.DS_Store differ diff --git a/app/.DS_Store b/app/.DS_Store new file mode 100644 index 0000000..53960aa Binary files /dev/null and b/app/.DS_Store differ diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index a5b0a31..fa3529a 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -4,18 +4,16 @@ class CommentsController < ApplicationController include OwnershipConcern def index - render json: @resource_comments, status: 200 end def show - render json: @comment, status: 200 end def create comment = @resource_comments.new(content: comment_params[:content]) comment.user = current_user if comment.save - render json: comment, status: 201 + render :show else invalid_request("Comment body can not be empty!") end @@ -23,7 +21,7 @@ def create def update if @comment.update(content: comment_params[:content]) - render json: @comment, status: 200 + render :show else invalid_request("Comment body can not be empty!") end diff --git a/app/controllers/concerns/common.rb b/app/controllers/concerns/common.rb new file mode 100644 index 0000000..9f4b034 --- /dev/null +++ b/app/controllers/concerns/common.rb @@ -0,0 +1,26 @@ +module Common + def set_vars(allowed = {}) + @question_id = allowed[:question_id] + @answer_id = allowed[:answer_id] + @comment_id = allowed[:comment_id] + @id = allowed[:id] + @content = allowed[:content] + @user_id = current_user.id if current_user + end + + def action_on_question + question_id && answer_id.nil? + end + + def action_on_answer + answer_id && question_id.nil? + end + + def action_on_comment + comment_id.present? + end + + def error_response(message = false, status = 403) + render json: { error: message }, status: status + end +end diff --git a/app/views/comments/_comment.json.jbuilder b/app/views/comments/_comment.json.jbuilder index 3ef6a20..0c5499f 100644 --- a/app/views/comments/_comment.json.jbuilder +++ b/app/views/comments/_comment.json.jbuilder @@ -1 +1 @@ - json.comments comments, partial: 'comments/default', as: :data + json.comments @comment, partial: 'comments/default', as: :data diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder index 66d72b2..7f1ae9d 100644 --- a/app/views/comments/index.json.jbuilder +++ b/app/views/comments/index.json.jbuilder @@ -1,4 +1,5 @@ -json.array!(@comments) do |comment| - json.extract! comment, :id - json.url comment_url(comment, format: :json) +json.array!(@resource_comments) do |comment| + json.partial! 'comments/default', data: comment + json.extract! comment, :comment_on_id, :comment_on_type + json.partial! 'users/user', user: comment.user end diff --git a/app/views/comments/show.json.jbuilder b/app/views/comments/show.json.jbuilder index ca935ec..2d8a8bc 100644 --- a/app/views/comments/show.json.jbuilder +++ b/app/views/comments/show.json.jbuilder @@ -1 +1,3 @@ -json.extract! @comment, :id, :created_at, :updated_at +json.partial! 'comments/default', data: @comment +json.extract! @comment, :comment_on_id, :comment_on_type +json.partial! 'users/user', user: @comment.user