From 4b49270be7d1407ca7932a1afe4162ddde73d2bf Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Wed, 5 Jun 2024 00:14:31 +0200 Subject: [PATCH] Simplify comment and post validation It's enough to check if the comment exists on the specified post, if it doesn't exist at all, that check will also fail. Also do that check directly on SQL level and just check if the comment exist instead of looping through all comments. --- app/controllers/api/v1/likes_controller.rb | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/likes_controller.rb b/app/controllers/api/v1/likes_controller.rb index 25b461bd8..94cf41f5e 100644 --- a/app/controllers/api/v1/likes_controller.rb +++ b/app/controllers/api/v1/likes_controller.rb @@ -121,25 +121,17 @@ module Api end def comment_and_post_validate(post_guid, comment_guid) - if !comment_exists(comment_guid) || !comment_is_for_post(post_guid, comment_guid) + if comment_is_for_post(post_guid, comment_guid) + true + else 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 + comments.exists?(guid: comment_guid) end end end