From 500c1bddf2640c0b59e8d761534f4bb99a83fbec Mon Sep 17 00:00:00 2001 From: Anton Khorev Date: Tue, 21 Nov 2023 12:34:10 +0300 Subject: [PATCH] Move common query time condition to mixin --- app/controllers/api/changesets_controller.rb | 29 +-------------- app/controllers/api/notes_controller.rb | 29 +++------------ app/controllers/concerns/query_methods.rb | 37 +++++++++++++++++++ test/controllers/api/notes_controller_test.rb | 23 ++++++++++++ 4 files changed, 67 insertions(+), 51 deletions(-) diff --git a/app/controllers/api/changesets_controller.rb b/app/controllers/api/changesets_controller.rb index 77d3d7824..3df7b75ce 100644 --- a/app/controllers/api/changesets_controller.rb +++ b/app/controllers/api/changesets_controller.rb @@ -32,7 +32,7 @@ module Api changesets = conditions_bbox(changesets, bbox) changesets = conditions_user(changesets, params["user"], params["display_name"]) 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_closed(changesets, params["closed"]) changesets = conditions_ids(changesets, params["changesets"]) @@ -339,33 +339,6 @@ module Api raise OSM::APIBadUserInput, e.message.to_s 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) # we do this by seeing if the 'closed at' time is in the future. Also if we've diff --git a/app/controllers/api/notes_controller.rb b/app/controllers/api/notes_controller.rb index 6f4803191..4b0c3be7c 100644 --- a/app/controllers/api/notes_controller.rb +++ b/app/controllers/api/notes_controller.rb @@ -276,29 +276,12 @@ module Api end # Add any date filter - 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 - - @notes = if params[:sort] == "updated_at" - @notes.where(:updated_at => from..to) - else - @notes.where(:created_at => from..to) - end - end + time_filter_property = if params[:sort] == "updated_at" + :updated_at + else + :created_at + end + @notes = query_conditions_time(@notes, time_filter_property) # Choose the sort order @notes = if params[:sort] == "created_at" diff --git a/app/controllers/concerns/query_methods.rb b/app/controllers/concerns/query_methods.rb index 2656eb4af..672a8c602 100644 --- a/app/controllers/concerns/query_methods.rb +++ b/app/controllers/concerns/query_methods.rb @@ -3,6 +3,43 @@ module QueryMethods 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 def query_limit(items) diff --git a/test/controllers/api/notes_controller_test.rb b/test/controllers/api/notes_controller_test.rb index 17ceb1b9e..a2c9dc8a4 100644 --- a/test/controllers/api/notes_controller_test.rb +++ b/test/controllers/api/notes_controller_test.rb @@ -1068,6 +1068,29 @@ module Api 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 notes = Array.new(5) do |i| position = ((1.0 + (i * 0.1)) * GeoRecord::SCALE).to_i -- 2.39.5