Move common query time condition to mixin
This commit is contained in:
parent
989b110bfc
commit
500c1bddf2
4 changed files with 67 additions and 51 deletions
|
@ -32,7 +32,7 @@ module Api
|
||||||
changesets = conditions_bbox(changesets, bbox)
|
changesets = conditions_bbox(changesets, bbox)
|
||||||
changesets = conditions_user(changesets, params["user"], params["display_name"])
|
changesets = conditions_user(changesets, params["user"], params["display_name"])
|
||||||
changesets = conditions_time(changesets, params["time"])
|
changesets = conditions_time(changesets, params["time"])
|
||||||
changesets = conditions_from_to(changesets, params["from"], params["to"])
|
changesets = query_conditions_time(changesets)
|
||||||
changesets = conditions_open(changesets, params["open"])
|
changesets = conditions_open(changesets, params["open"])
|
||||||
changesets = conditions_closed(changesets, params["closed"])
|
changesets = conditions_closed(changesets, params["closed"])
|
||||||
changesets = conditions_ids(changesets, params["changesets"])
|
changesets = conditions_ids(changesets, params["changesets"])
|
||||||
|
@ -339,33 +339,6 @@ module Api
|
||||||
raise OSM::APIBadUserInput, e.message.to_s
|
raise OSM::APIBadUserInput, e.message.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
|
||||||
# restrict changesets to those opened during a particular time period
|
|
||||||
# works similar to from..to of notes controller, including the requirement of 'from' when specifying 'to'
|
|
||||||
def conditions_from_to(changesets, from, to)
|
|
||||||
if from
|
|
||||||
begin
|
|
||||||
from = Time.parse(from).utc
|
|
||||||
rescue ArgumentError
|
|
||||||
raise OSM::APIBadUserInput, "Date #{from} is in a wrong format"
|
|
||||||
end
|
|
||||||
|
|
||||||
begin
|
|
||||||
to = if to
|
|
||||||
Time.parse(to).utc
|
|
||||||
else
|
|
||||||
Time.now.utc
|
|
||||||
end
|
|
||||||
rescue ArgumentError
|
|
||||||
raise OSM::APIBadUserInput, "Date #{to} is in a wrong format"
|
|
||||||
end
|
|
||||||
|
|
||||||
changesets.where(:created_at => from..to)
|
|
||||||
else
|
|
||||||
changesets
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# return changesets which are open (haven't been closed yet)
|
# return changesets which are open (haven't been closed yet)
|
||||||
# we do this by seeing if the 'closed at' time is in the future. Also if we've
|
# we do this by seeing if the 'closed at' time is in the future. Also if we've
|
||||||
|
|
|
@ -276,29 +276,12 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
# Add any date filter
|
# Add any date filter
|
||||||
if params[:from]
|
time_filter_property = if params[:sort] == "updated_at"
|
||||||
begin
|
:updated_at
|
||||||
from = Time.parse(params[:from]).utc
|
else
|
||||||
rescue ArgumentError
|
:created_at
|
||||||
raise OSM::APIBadUserInput, "Date #{params[:from]} is in a wrong format"
|
end
|
||||||
end
|
@notes = query_conditions_time(@notes, time_filter_property)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
@notes = if params[:sort] == "updated_at"
|
|
||||||
@notes.where(:updated_at => from..to)
|
|
||||||
else
|
|
||||||
@notes.where(:created_at => from..to)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Choose the sort order
|
# Choose the sort order
|
||||||
@notes = if params[:sort] == "created_at"
|
@notes = if params[:sort] == "created_at"
|
||||||
|
|
|
@ -3,6 +3,43 @@ module QueryMethods
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
##
|
||||||
|
# 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
|
# Limit the result according to request parameters and settings
|
||||||
def query_limit(items)
|
def query_limit(items)
|
||||||
|
|
|
@ -1068,6 +1068,29 @@ module Api
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_search_by_time_success
|
||||||
|
note1 = create(:note, :created_at => "2020-02-01T00:00:00Z", :updated_at => "2020-04-01T00:00:00Z")
|
||||||
|
note2 = create(:note, :created_at => "2020-03-01T00:00:00Z", :updated_at => "2020-05-01T00:00:00Z")
|
||||||
|
|
||||||
|
get search_api_notes_path(:from => "2020-02-15T00:00:00Z", :to => "2020-04-15T00:00:00Z", :format => "xml")
|
||||||
|
assert_response :success
|
||||||
|
assert_equal "application/xml", @response.media_type
|
||||||
|
assert_select "osm", :count => 1 do
|
||||||
|
assert_select "note", :count => 1 do
|
||||||
|
assert_select "id", note2.id.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
get search_api_notes_path(:from => "2020-02-15T00:00:00Z", :to => "2020-04-15T00:00:00Z", :sort => "updated_at", :format => "xml")
|
||||||
|
assert_response :success
|
||||||
|
assert_equal "application/xml", @response.media_type
|
||||||
|
assert_select "osm", :count => 1 do
|
||||||
|
assert_select "note", :count => 1 do
|
||||||
|
assert_select "id", note1.id.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_search_by_bbox_success
|
def test_search_by_bbox_success
|
||||||
notes = Array.new(5) do |i|
|
notes = Array.new(5) do |i|
|
||||||
position = ((1.0 + (i * 0.1)) * GeoRecord::SCALE).to_i
|
position = ((1.0 + (i * 0.1)) * GeoRecord::SCALE).to_i
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue