Move common query limit method of changesets and notes to mixin

This commit is contained in:
Anton Khorev 2023-11-15 16:30:24 +03:00
parent f352c1dfbb
commit d92fc6e526
3 changed files with 40 additions and 33 deletions

View file

@ -1,5 +1,7 @@
module Api
class NotesController < ApiController
include QueryMethods
before_action :check_api_writable, :only => [:create, :comment, :close, :reopen, :destroy]
before_action :setup_user_auth, :only => [:create, :show]
before_action :authorize, :only => [:close, :reopen, :destroy, :comment]
@ -36,7 +38,9 @@ module Api
@max_lat = bbox.max_lat
# Find the notes we want to return
@notes = notes.bbox(bbox).order("updated_at DESC").limit(result_limit).preload(:comments)
notes = notes.bbox(bbox).order("updated_at DESC")
notes = query_limit(notes)
@notes = notes.preload(:comments)
# Render the result
respond_to do |format|
@ -234,8 +238,9 @@ module Api
# Find the comments we want to return
@comments = NoteComment.where(:note => notes)
.order(:created_at => :desc).limit(result_limit)
.preload(:author, :note => { :comments => :author })
.order(:created_at => :desc)
@comments = query_limit(@comments)
@comments = @comments.preload(:author, :note => { :comments => :author })
# Render the result
respond_to do |format|
@ -311,7 +316,8 @@ module Api
end
# Find the notes we want to return
@notes = @notes.distinct.limit(result_limit).preload(:comments)
@notes = query_limit(@notes.distinct)
@notes = @notes.preload(:comments)
# Render the result
respond_to do |format|
@ -328,20 +334,6 @@ module Api
# utility functions below.
#------------------------------------------------------------
##
# Get the maximum number of results to return
def result_limit
if params[:limit]
if params[:limit].to_i.positive? && params[:limit].to_i <= Settings.max_note_query_limit
params[:limit].to_i
else
raise OSM::APIBadUserInput, "Note limit must be between 1 and #{Settings.max_note_query_limit}"
end
else
Settings.default_note_query_limit
end
end
##
# Generate a condition to choose which notes we want based
# on their status and the user's request parameters