Skip to content

Commit 1e1130e

Browse files
committed
add API routes for comment likes
1 parent 389b187 commit 1e1130e

File tree

4 files changed

+349
-11
lines changed

4 files changed

+349
-11
lines changed

app/controllers/api/v1/likes_controller.rb

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ def show
2323
post = post_service.find!(params.require(:post_id))
2424
raise ActiveRecord::RecordInvalid unless post.public? || private_read?
2525

26-
likes_query = like_service.find_for_post(params[:post_id])
26+
likes_query = find_likes
27+
28+
return unless likes_query
29+
2730
likes_page = index_pager(likes_query).response
2831
likes_page[:data] = likes_page[:data].map {|x| like_json(x) }
2932
render_paged_api_response likes_page
@@ -33,31 +36,42 @@ def create
3336
post = post_service.find!(params.require(:post_id))
3437
raise ActiveRecord::RecordInvalid unless post.public? || private_read?
3538

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
3744
rescue ActiveRecord::RecordInvalid => e
3845
if e.message == "Validation failed: Target has already been taken"
3946
return render_error 409, "Like already exists"
4047
end
4148

4249
raise
43-
else
44-
head :no_content
4550
end
4651

4752
def destroy
4853
post = post_service.find!(params.require(:post_id))
4954
raise ActiveRecord::RecordInvalid unless post.public? || private_read?
5055

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
5458
else
55-
render_error 410, "Like doesn’t exist"
59+
destroy_for_post
5660
end
5761
end
5862

5963
private
6064

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+
6175
def like_service
6276
@like_service ||= LikeService.new(current_user)
6377
end
@@ -66,9 +80,67 @@ def post_service
6680
@post_service ||= PostService.new(current_user)
6781
end
6882

83+
def comment_service
84+
@comment_service ||= CommentService.new(current_user)
85+
end
86+
6987
def like_json(like)
7088
LikesPresenter.new(like).as_api_json
7189
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
72144
end
73145
end
74146
end

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@
231231
resources :photos, only: %i[show index create destroy]
232232
resources :posts, only: %i[show create destroy] do
233233
resources :comments, only: %i[create index destroy] do
234-
post "report" => "comments#report"
234+
resource :likes, only: %i[show create destroy]
235+
post :report
235236
end
236237
resource :reshares, only: %i[show create]
237238
resource :likes, only: %i[show create destroy]

spec/integration/api/comments_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@
135135
end
136136
end
137137

138-
describe "#read" do
138+
describe "#index" do
139139
before do
140140
@comment_text1 = "This is a comment"
141141
@comment_text2 = "This is a comment 2"

0 commit comments

Comments
 (0)