Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1808 modify search endpoint #553

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,66 @@ class ServicesController < ApplicationController

def index
if params[:category_id]
all_services = find_by_category(params[:category_id])
all_services = find_by_categories(params[:category_id], params[:eligibility_id])
elsif params[:eligibility_id]
all_services = find_by_eligibility(params[:eligibility_id])
end

render json: ServicesWithResourcePresenter.present(all_services)
end

# rubocop:disable AbcSize
# rubocop:disable MethodLength
# rubocop:disable PerceivedComplexity
def find_by_categories(categories_id_string, eligibilities_id_string)
query_string = ""
if categories_id_string.include? ","
category_ids = categories_id_string.split ","
first_cat = true
category_ids.each do |cat_id|
if first_cat
query_string = "services.id in (select service_id from categories_services where category_id=" + cat_id + ")"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will expose us to a SQL injection attack. Can this be written using the ActiveRecord methods that properly escape external values? Or if you have to drop into raw SQL, at least use the sanitization methods available in ActiveRecord: https://api.rubyonrails.org/v5.1.0/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh that's a good point. I could just check if all of the incoming IDs are integers for example.

first_cat = false
else
query_string = query_string +
" and services.id in (select service_id from categories_services where category_id=" + cat_id + ")"
end
Comment on lines +28 to +37
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you're trying to do might be simpler using an approach like this: https://stackoverflow.com/a/15977167

I think that'd avoid having to dynamically generate a query with an arbitrary number of expressions, since you can push everything into that inner join with a HAVING clause. Man, I haven't used the HAVING keyword in forever...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how that would make things easier. I'll contact you to get more information.

end
else
query_string = "services.id in (select service_id from categories_services where category_id=" + cat_id + ")"
end

if eligibilities_id_string
if eligibilities_id_string.include? ","
eligibility_ids = eligibilities_id_string.split ","
eligibility_ids.each do |elig_id|
query_string = query_string +
" and services_id in (select service_id from eligibilities_services where eligibility_id=" +
elig_id + ")"
end
else
query_string = query_string +
" and services_id in (select service_id from eligibilities_services where eligibility_id=" +
eligibilities_id_string + ")"
end
end
find_services(query_string)
end
# rubocop:enable AbcSize
# rubocop:enable MethodLength
# rubocop:enable PerceivedComplexity

def find_services(query_string)
services.includes(
resource: [
:addresses, :phones, :categories, :notes,
schedule: :schedule_days,
services: [:notes, :categories, :addresses, :eligibilities, { schedule: :schedule_days }],
ratings: [:review]
]
).where(query_string)
end

def find_by_category(category_id_string)
services.includes(
resource: [
Expand Down
Loading