Created
July 13, 2020 12:29
-
-
Save ndeto/9ab8da9a0c88b74abb04b06ff520c5ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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