]> git.openstreetmap.org Git - rails.git/commitdiff
Move query notes by user value to mixin
authorAnton Khorev <tony29@yandex.ru>
Tue, 21 Nov 2023 10:08:50 +0000 (13:08 +0300)
committerAnton Khorev <tony29@yandex.ru>
Sat, 15 Feb 2025 16:05:46 +0000 (19:05 +0300)
app/controllers/api/notes_controller.rb
app/controllers/concerns/query_methods.rb
test/controllers/api/notes_controller_test.rb

index 4b0c3be7c945629f1e2f044b91847094a4b1fc8b..af0c5e0398a4265ff6e122f927668e69b6910e9b 100644 (file)
@@ -256,19 +256,8 @@ module Api
       @notes = bbox_condition(@notes)
 
       # Add any user filter
       @notes = bbox_condition(@notes)
 
       # Add any user filter
-      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
-
-        @notes = @notes.joins(:comments).where(:note_comments => { :author_id => @user })
-      end
+      user = query_conditions_user_value
+      @notes = @notes.joins(:comments).where(:note_comments => { :author_id => user }) if user
 
       # Add any text filter
       if params[:q]
 
       # Add any text filter
       if params[:q]
index 672a8c6027e31018cc7e6fe56236c2d5bd2e15f4..eb06842833798937cfa61aaf93c0188fffa3cf5f 100644 (file)
@@ -3,6 +3,33 @@ module QueryMethods
 
   private
 
 
   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
   ##
   # Restrict the resulting items to those created during a particular time period
   # Using 'to' requires specifying 'from' as well for historical reasons
index a2c9dc8a4fd8f917b7e43d97b8014ed0cf5c4826..f1a0f766c9b23858c4bf9b8d2e646afeb42bb255 100644 (file)
@@ -1066,6 +1066,14 @@ module Api
       assert_select "gpx", :count => 1 do
         assert_select "wpt", :count => 1
       end
       assert_select "gpx", :count => 1 do
         assert_select "wpt", :count => 1
       end
+
+      user2 = create(:user)
+      get search_api_notes_path(:user => user2.id, :format => "xml")
+      assert_response :success
+      assert_equal "application/xml", @response.media_type
+      assert_select "osm", :count => 1 do
+        assert_select "note", :count => 0
+      end
     end
 
     def test_search_by_time_success
     end
 
     def test_search_by_time_success