@@ -23,7 +23,10 @@ def show
23
23
post = post_service . find! ( params . require ( :post_id ) )
24
24
raise ActiveRecord ::RecordInvalid unless post . public? || private_read?
25
25
26
- likes_query = like_service . find_for_post ( params [ :post_id ] )
26
+ likes_query = find_likes
27
+
28
+ return unless likes_query
29
+
27
30
likes_page = index_pager ( likes_query ) . response
28
31
likes_page [ :data ] = likes_page [ :data ] . map { |x | like_json ( x ) }
29
32
render_paged_api_response likes_page
@@ -33,31 +36,42 @@ def create
33
36
post = post_service . find! ( params . require ( :post_id ) )
34
37
raise ActiveRecord ::RecordInvalid unless post . public? || private_read?
35
38
36
- like_service . create_for_post ( params [ :post_id ] )
39
+ if params [ :comment_id ] . present?
40
+ create_for_comment
41
+ else
42
+ create_for_post
43
+ end
37
44
rescue ActiveRecord ::RecordInvalid => e
38
45
if e . message == "Validation failed: Target has already been taken"
39
46
return render_error 409 , "Like already exists"
40
47
end
41
48
42
49
raise
43
- else
44
- head :no_content
45
50
end
46
51
47
52
def destroy
48
53
post = post_service . find! ( params . require ( :post_id ) )
49
54
raise ActiveRecord ::RecordInvalid unless post . public? || private_read?
50
55
51
- success = like_service . unlike_post ( params [ :post_id ] )
52
- if success
53
- head :no_content
56
+ if params [ :comment_id ] . present?
57
+ destroy_for_comment
54
58
else
55
- render_error 410 , "Like doesn’t exist"
59
+ destroy_for_post
56
60
end
57
61
end
58
62
59
63
private
60
64
65
+ def find_likes
66
+ if params [ :comment_id ] . present?
67
+ return unless comment_and_post_validate ( params [ :post_id ] , params [ :comment_id ] )
68
+
69
+ like_service . find_for_comment ( params [ :comment_id ] )
70
+ else
71
+ like_service . find_for_post ( params [ :post_id ] )
72
+ end
73
+ end
74
+
61
75
def like_service
62
76
@like_service ||= LikeService . new ( current_user )
63
77
end
@@ -66,9 +80,67 @@ def post_service
66
80
@post_service ||= PostService . new ( current_user )
67
81
end
68
82
83
+ def comment_service
84
+ @comment_service ||= CommentService . new ( current_user )
85
+ end
86
+
69
87
def like_json ( like )
70
88
LikesPresenter . new ( like ) . as_api_json
71
89
end
90
+
91
+ def create_for_post
92
+ like_service . create_for_post ( params [ :post_id ] )
93
+
94
+ head :no_content
95
+ end
96
+
97
+ def create_for_comment
98
+ return unless comment_and_post_validate ( params [ :post_id ] , params [ :comment_id ] )
99
+
100
+ like_service . create_for_comment ( params [ :comment_id ] )
101
+
102
+ head :no_content
103
+ end
104
+
105
+ def destroy_for_post
106
+ if like_service . unlike_post ( params [ :post_id ] )
107
+ head :no_content
108
+ else
109
+ render_error 410 , "Like doesn’t exist"
110
+ end
111
+ end
112
+
113
+ def destroy_for_comment
114
+ return unless comment_and_post_validate ( params [ :post_id ] , params [ :comment_id ] )
115
+
116
+ if like_service . unlike_comment ( params [ :comment_id ] )
117
+ head :no_content
118
+ else
119
+ render_error 410 , "Like doesn’t exist"
120
+ end
121
+ end
122
+
123
+ def comment_and_post_validate ( post_guid , comment_guid )
124
+ if !comment_exists ( comment_guid ) || !comment_is_for_post ( post_guid , comment_guid )
125
+ render_error 404 , "Comment not found for the given post"
126
+ false
127
+ else
128
+ true
129
+ end
130
+ end
131
+
132
+ def comment_exists ( comment_guid )
133
+ comment = comment_service . find! ( comment_guid )
134
+ comment ? true : false
135
+ rescue ActiveRecord ::RecordNotFound
136
+ false
137
+ end
138
+
139
+ def comment_is_for_post ( post_guid , comment_guid )
140
+ comments = comment_service . find_for_post ( post_guid )
141
+ comment = comments . find { |comment | comment [ :guid ] == comment_guid }
142
+ comment ? true : false
143
+ end
72
144
end
73
145
end
74
146
end
0 commit comments