Rails-8-API-Image-Processing / app /controllers /api /v1 /comments_controller.rb
denise-oneill-inbox-ru's picture
D8 staging from 92665ef4298ecff6735b080ff7901f2c19f719a7
5010ad7 verified
Raw History Blame Contribute Delete
1 kB
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