Skip to content

Commit

Permalink
add API routes for comment likes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhass committed Nov 24, 2023
1 parent 389b187 commit 627c067
Show file tree
Hide file tree
Showing 4 changed files with 349 additions and 11 deletions.
88 changes: 80 additions & 8 deletions app/controllers/api/v1/likes_controller.rb
Expand Up @@ -23,7 +23,10 @@ def show
post = post_service.find!(params.require(:post_id))
raise ActiveRecord::RecordInvalid unless post.public? || private_read?

likes_query = like_service.find_for_post(params[:post_id])
likes_query = find_likes

return unless likes_query

likes_page = index_pager(likes_query).response
likes_page[:data] = likes_page[:data].map {|x| like_json(x) }
render_paged_api_response likes_page
Expand All @@ -33,31 +36,42 @@ def create
post = post_service.find!(params.require(:post_id))
raise ActiveRecord::RecordInvalid unless post.public? || private_read?

like_service.create_for_post(params[:post_id])
if params[:comment_id].present?
create_for_comment
else
create_for_post
end
rescue ActiveRecord::RecordInvalid => e
if e.message == "Validation failed: Target has already been taken"
return render_error 409, "Like already exists"
end

raise
else
head :no_content
end

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

success = like_service.unlike_post(params[:post_id])
if success
head :no_content
if params[:comment_id].present?
destroy_for_comment
else
render_error 410, "Like doesn’t exist"
destroy_for_post
end
end

private

def find_likes
if params[:comment_id].present?
return unless comment_and_post_validate(params[:post_id], params[:comment_id])

like_service.find_for_comment(params[:comment_id])
else
like_service.find_for_post(params[:post_id])
end
end

def like_service
@like_service ||= LikeService.new(current_user)
end
Expand All @@ -66,9 +80,67 @@ def post_service
@post_service ||= PostService.new(current_user)
end

def comment_service
@comment_service ||= CommentService.new(current_user)
end

def like_json(like)
LikesPresenter.new(like).as_api_json
end

def create_for_post
like_service.create_for_post(params[:post_id])

head :no_content
end

def create_for_comment
return unless comment_and_post_validate(params[:post_id], params[:comment_id])

like_service.create_for_comment(params[:comment_id])

head :no_content
end

def destroy_for_post
if like_service.unlike_post(params[:post_id])
head :no_content
else
render_error 410, "Like doesn’t exist"
end
end

def destroy_for_comment
return unless comment_and_post_validate(params[:post_id], params[:comment_id])

if like_service.unlike_comment(params[:comment_id])
head :no_content
else
render_error 410, "Like doesn’t exist"
end
end

def comment_and_post_validate(post_guid, comment_guid)
if !comment_exists(comment_guid) || !comment_is_for_post(post_guid, comment_guid)
render_error 404, "Comment not found for the given post"
false
else
true
end
end

def comment_exists(comment_guid)
comment = comment_service.find!(comment_guid)
comment ? true : false
rescue ActiveRecord::RecordNotFound
false
end

def comment_is_for_post(post_guid, comment_guid)
comments = comment_service.find_for_post(post_guid)
comment = comments.find {|comment| comment[:guid] == comment_guid }
comment ? true : false
end
end
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Expand Up @@ -231,7 +231,8 @@
resources :photos, only: %i[show index create destroy]
resources :posts, only: %i[show create destroy] do
resources :comments, only: %i[create index destroy] do
post "report" => "comments#report"
resource :likes, only: %i[show create destroy]
post :report
end
resource :reshares, only: %i[show create]
resource :likes, only: %i[show create destroy]
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/api/comments_controller_spec.rb
Expand Up @@ -135,7 +135,7 @@
end
end

describe "#read" do
describe "#index" do
before do
@comment_text1 = "This is a comment"
@comment_text2 = "This is a comment 2"
Expand Down

0 comments on commit 627c067

Please sign in to comment.