Move common query limit method of changesets and notes to mixin
This commit is contained in:
parent
f352c1dfbb
commit
d92fc6e526
3 changed files with 40 additions and 33 deletions
|
@ -2,6 +2,8 @@
|
|||
|
||||
module Api
|
||||
class ChangesetsController < ApiController
|
||||
include QueryMethods
|
||||
|
||||
before_action :check_api_writable, :only => [:create, :update, :upload, :subscribe, :unsubscribe]
|
||||
before_action :setup_user_auth, :only => [:show]
|
||||
before_action :authorize, :only => [:create, :update, :upload, :close, :subscribe, :unsubscribe]
|
||||
|
@ -43,7 +45,7 @@ module Api
|
|||
end
|
||||
|
||||
# limit the result
|
||||
changesets = changesets.limit(result_limit)
|
||||
changesets = query_limit(changesets)
|
||||
|
||||
# preload users, tags and comments, and render result
|
||||
@changesets = changesets.preload(:user, :changeset_tags, :comments)
|
||||
|
@ -403,19 +405,5 @@ module Api
|
|||
changesets.where(:id => ids)
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# 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_changeset_query_limit
|
||||
params[:limit].to_i
|
||||
else
|
||||
raise OSM::APIBadUserInput, "Changeset limit must be between 1 and #{Settings.max_changeset_query_limit}"
|
||||
end
|
||||
else
|
||||
Settings.default_changeset_query_limit
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
27
app/controllers/concerns/query_methods.rb
Normal file
27
app/controllers/concerns/query_methods.rb
Normal file
|
@ -0,0 +1,27 @@
|
|||
module QueryMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
private
|
||||
|
||||
##
|
||||
# Limit the result according to request parameters and settings
|
||||
def query_limit(items)
|
||||
items.limit(query_limit_value)
|
||||
end
|
||||
|
||||
##
|
||||
# Get query limit value from request parameters and settings
|
||||
def query_limit_value
|
||||
max_limit = Settings["max_#{controller_name.singularize}_query_limit"]
|
||||
default_limit = Settings["default_#{controller_name.singularize}_query_limit"]
|
||||
if params[:limit]
|
||||
if params[:limit].to_i.positive? && params[:limit].to_i <= max_limit
|
||||
params[:limit].to_i
|
||||
else
|
||||
raise OSM::APIBadUserInput, "#{controller_name.classify} limit must be between 1 and #{max_limit}"
|
||||
end
|
||||
else
|
||||
default_limit
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue