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.
This commit is contained in:
Benjamin Neff 2024-06-05 00:14:31 +02:00
parent 1e1130e211
commit 4b49270be7
No known key found for this signature in database
GPG key ID: 971464C3F1A90194

View file

@ -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