-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathlike_controller.rb
37 lines (32 loc) · 993 Bytes
/
like_controller.rb
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
class LikeController < ApplicationController
respond_to :html, :xml, :json
before_action :require_user, only: %i(create delete)
# list all recent likes
def index
@paginated = true
@pagy_a, @likes = pagy_array(NodeSelection.all.reverse)
end
# return a count of likes for a given node
# This does not support non-nodes very well
def show
render json: Node.find(params[:id]).likers.size
end
# for the current user, return whether is presently liked or not
def liked?
result = NodeSelection.find_by_user_id_and_nid(current_user.uid, params[:id])
result = if result.nil?
false
else
result.liking
end
render json: result
end
# for the current user, register as liking the given node
def create
render json: Node.like(params[:id], current_user)
end
# for the current user, remove the like from the given node
def delete
render json: Node.unlike(params[:id], current_user)
end
end