Skip to content

Instantly share code, notes, and snippets.

@ndeto
Created July 13, 2020 12:29
Show Gist options
  • Save ndeto/9ab8da9a0c88b74abb04b06ff520c5ef to your computer and use it in GitHub Desktop.
Save ndeto/9ab8da9a0c88b74abb04b06ff520c5ef to your computer and use it in GitHub Desktop.
def filter
# receive params
allowed_constant_params = %w(property_type listing_type_id rooms bathrooms location)
allowed_varying_params = %w(price)
query = ""
allowed_constant_params.each do |param|
unless params[param] == ""
if query.empty?
query.concat("SELECT * FROM properties WHERE #{param} = '#{params[param]}'")
else
query.concat(" AND #{param} = '#{params[param]}'")
end
end
end
#price recieves a string with "min,max" from the front end
allowed_varying_params.each do |param|
unless params[param] == ""
min, max = params[param].delete('"').split(",")
if query.empty?
query.concat("SELECT * FROM properties WHERE CAST(price as Integer) >= #{min} AND CAST(price as Integer) <= #{max} ")
else
query.concat(" AND CAST(price as Integer) >= #{min} AND CAST(price as Integer) <= #{max}")
end
end
end
@properties = execute_statement(query)
render json: @properties
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment