File size: 1,827 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class Api::PostsController < ApplicationController
  def show
    @post = Post.find(params[:id])
    
    if @post
      render "api/posts/show"
    else
      render json: ["A post with that id does not exist"], status: 404
    end
  end
  
  def wall_posts
    @user = User.find(params[:user_id])
    
    if @user
      @posts = @user.wall_posts.includes(
        :recipient,
        author: {profile_picture_attachment: :blob},
        comments: {author: {profile_picture_attachment: :blob}}
      )

      render "api/posts/index"
    else
      render json: ["A user with that id does not exist"], status: 404
    end
  end

  def feed_posts
    @user = User.find(params[:user_id])

    if @user
      @posts = @user.feed_posts.includes(
        :recipient,
        author: {profile_picture_attachment: :blob},
        comments: {author: {profile_picture_attachment: :blob}}
      )
      
      render "api/posts/index"
    else
      render json: ["A post with that id does not exist"], status: 404
    end
  end

  def create
    @post = Post.new(post_params)

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

  def update
    @post = Post.find(params[:id])

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

  def destroy
    @post = Post.find(params[:id])

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

  private

  def post_params
    params.require(:post).permit(:author_id, :recipient_id, :body, :image)
  end
end