File size: 1,349 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Api::CommentsController < ApplicationController
  def show
    @comment = Comment.find(params[:id])

    if @comment
      render "api/comments/show"
    else
      render json: ["A comment with that id does not exist"], status: 404
    end
  end

  def index
    post = Post.find(params[:post_id])

    if post
      @comments = post.comments
      render "api/comments/index"
    else
      render json: ["A post with that id does not exist"], status: 404
    end
  end

  def create
    @comment = Comment.new(comment_params)

    if @comment.save
      render "api/comments/show"
    else
      render json: @comment.errors.full_messages, status: 422
    end
  end

  def update
    @comment = Comment.find(params[:id])

    if @comment
      if @comment.update(comment_params)
        render "api/comments/show"
      else
        render json: @comment.errors.full_messages, status: 422
      end
    else
      render json: ["A comment with that id does not exist"], status: 404
    end
  end

  def destroy
    @comment = Comment.find(params[:id])

    if @comment
      @comment.delete
      render "api/comments/show"
    else
      render json: ["A comment with that id does not exist"], status: 404
    end
  end

  private

  def comment_params
    params.require(:comment).permit(:author_id, :post_id, :parent_id, :body)
  end
end