File size: 1,002 Bytes
5010ad7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | module Api
module V1
class CommentsController < BaseController
before_action :set_post
def index
comments = @post.comments.kept.includes(:user).order(created_at: :desc)
@pagy, comments = pagy(comments, items: 20)
render json: CommentBlueprint.render(comments),
meta: pagy_metadata(@pagy)
end
def create
comment = @post.comments.build(comment_params)
comment.user = current_user
authorize comment
if comment.save
render json: CommentBlueprint.render(comment), status: :created
else
render json: { errors: comment.errors.full_messages }, status: :unprocessable_entity
end
end
private
def set_post
@post = Post.friendly.find(params[:post_slug])
end
def comment_params
params.require(:comment).permit(:content)
end
def action_allows_anonymous?
action_name == "index"
end
end
end
end
|