File size: 1,361 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
class Api::FriendRequestsController < ApplicationController

  def create
    @friend_request = FriendRequest.new(friend_params)
    if @friend_request.save
      render :show
    else
      render json: @friend_request.errors.full_messages, status: 422
    end
  end

  def index
    @friends = User.find(params[:user_id]).accepted_friends.limit(9)
  end

  def show

  end

  def update
    @friend_request = FriendRequest.find_by(
      approver_id: params[:user_id],
      requestor_id: params[:id]
    )
    if @friend_request.update_attributes(approval_status: params[:friend_request][:approval_status])
      render :show
    else
      render json: @friend_request.errors.full_messages, status: 422
    end
  end

  def destroy
    @friend_request = FriendRequest.find_by(
      approver_id: params[:user_id],
      requestor_id: params[:id]
    )
    if @friend_request.try(:destroy)
      render json: {}
      return
    else
      @friend_request = FriendRequest.find_by(
      approver_id: params[:id],
      requestor_id: params[:user_id]
      )
      if @friend_request.try(:destroy)
        render json: {}
      else
        render json: @friend_request.errors.full_messages, status: 422
      end
    end
  end

  private

  def friend_params
    params.require(:friend_request).permit(:requestor_id, :approver_id,:approval_status)
  end
end