X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/989b110bfc804e2c681b12523f20940af205680d..a5ee93b30110e2c89879e0a67ab2b1c18158faf2:/app/controllers/concerns/query_methods.rb diff --git a/app/controllers/concerns/query_methods.rb b/app/controllers/concerns/query_methods.rb index 2656eb4af..eb0684283 100644 --- a/app/controllers/concerns/query_methods.rb +++ b/app/controllers/concerns/query_methods.rb @@ -3,6 +3,70 @@ module QueryMethods private + ## + # Filter the resulting items by user + def query_conditions_user(items, filter_property) + user = query_conditions_user_value + items = items.where(filter_property => user) if user + items + end + + ## + # Get user value for query filtering by user + # Raises OSM::APIBadUserInput if user not found like notes api does, changesets api raises OSM::APINotFoundError instead + def query_conditions_user_value + if params[:display_name] || params[:user] + if params[:display_name] + user = User.find_by(:display_name => params[:display_name]) + + raise OSM::APIBadUserInput, "User #{params[:display_name]} not known" unless user + else + user = User.find_by(:id => params[:user]) + + raise OSM::APIBadUserInput, "User #{params[:user]} not known" unless user + end + + user + end + end + + ## + # Restrict the resulting items to those created during a particular time period + # Using 'to' requires specifying 'from' as well for historical reasons + def query_conditions_time(items, filter_property = :created_at) + interval = query_conditions_time_value + + if interval + items.where(filter_property => interval) + else + items + end + end + + ## + # Get query time interval from request parameters or nil + def query_conditions_time_value + if params[:from] + begin + from = Time.parse(params[:from]).utc + rescue ArgumentError + raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format" + end + + begin + to = if params[:to] + Time.parse(params[:to]).utc + else + Time.now.utc + end + rescue ArgumentError + raise OSM::APIBadUserInput, "Date #{params[:to]} is in a wrong format" + end + + from..to + end + end + ## # Limit the result according to request parameters and settings def query_limit(items)