Merge pull request #13 from jedmund/paginate-collections
Paginate collections
This commit is contained in:
commit
e377306663
6 changed files with 60 additions and 25 deletions
|
|
@ -27,7 +27,6 @@ module Api::V1
|
||||||
# before returning
|
# before returning
|
||||||
def current_user
|
def current_user
|
||||||
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
||||||
@current_user.update_last_ip_and_last_seen!(request.remote_ip) if @current_user
|
|
||||||
|
|
||||||
return @current_user
|
return @current_user
|
||||||
end
|
end
|
||||||
|
|
@ -43,12 +42,6 @@ module Api::V1
|
||||||
set_content_type("application/javascript; charset=utf-8")
|
set_content_type("application/javascript; charset=utf-8")
|
||||||
end
|
end
|
||||||
|
|
||||||
def current_user
|
|
||||||
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
|
||||||
|
|
||||||
return @current_user
|
|
||||||
end
|
|
||||||
|
|
||||||
### Error response methods
|
### Error response methods
|
||||||
def render_unprocessable_entity_response(exception)
|
def render_unprocessable_entity_response(exception)
|
||||||
@exception = exception
|
@exception = exception
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,10 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
||||||
end
|
end
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
@per_page = 15
|
||||||
|
|
||||||
now = DateTime.current
|
now = DateTime.current
|
||||||
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
start_time = (now - request.params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
||||||
|
|
||||||
conditions = {}
|
conditions = {}
|
||||||
conditions[:element] = request.params['element'] unless request.params['element'].blank?
|
conditions[:element] = request.params['element'] unless request.params['element'].blank?
|
||||||
|
|
@ -27,9 +29,14 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
||||||
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
||||||
conditions[:weapons_count] = 5..13
|
conditions[:weapons_count] = 5..13
|
||||||
|
|
||||||
@parties = Party.where(conditions).each { |party|
|
@parties = Party
|
||||||
party.favorited = (current_user) ? party.is_favorited(current_user) : false
|
.where(conditions)
|
||||||
}
|
.order(created_at: :desc)
|
||||||
|
.paginate(page: request.params[:page], per_page: @per_page)
|
||||||
|
.each { |party|
|
||||||
|
party.favorited = (current_user) ? party.is_favorited(current_user) : false
|
||||||
|
}
|
||||||
|
@count = Party.where(conditions).count
|
||||||
|
|
||||||
render :all, status: :ok
|
render :all, status: :ok
|
||||||
end
|
end
|
||||||
|
|
@ -37,6 +44,8 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
||||||
def favorites
|
def favorites
|
||||||
raise Api::V1::UnauthorizedError unless current_user
|
raise Api::V1::UnauthorizedError unless current_user
|
||||||
|
|
||||||
|
@per_page = 15
|
||||||
|
|
||||||
now = DateTime.current
|
now = DateTime.current
|
||||||
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
||||||
|
|
||||||
|
|
@ -46,9 +55,15 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
||||||
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
||||||
conditions[:favorites] = { user_id: current_user.id }
|
conditions[:favorites] = { user_id: current_user.id }
|
||||||
|
|
||||||
@parties = Party.joins(:favorites).where(conditions).each { |party|
|
@parties = Party
|
||||||
party.favorited = party.is_favorited(current_user)
|
.joins(:favorites)
|
||||||
}
|
.where(conditions)
|
||||||
|
.order('favorites.created_at DESC')
|
||||||
|
.paginate(page: request.params[:page], per_page: @per_page)
|
||||||
|
.each { |party|
|
||||||
|
party.favorited = party.is_favorited(current_user)
|
||||||
|
}
|
||||||
|
@count = Party.joins(:favorites).where(conditions).count
|
||||||
|
|
||||||
render :all, status: :ok
|
render :all, status: :ok
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,8 @@ class Api::V1::UsersController < Api::V1::ApiController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
if @user
|
if @user
|
||||||
|
@per_page = 15
|
||||||
|
|
||||||
now = DateTime.current
|
now = DateTime.current
|
||||||
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
||||||
|
|
||||||
|
|
@ -45,9 +47,14 @@ class Api::V1::UsersController < Api::V1::ApiController
|
||||||
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
||||||
conditions[:user_id] = @user.id
|
conditions[:user_id] = @user.id
|
||||||
|
|
||||||
@parties = Party.where(conditions).each { |party|
|
@parties = Party
|
||||||
party.favorited = (current_user) ? party.is_favorited(current_user) : false
|
.where(conditions)
|
||||||
}
|
.order(created_at: :desc)
|
||||||
|
.paginate(page: request.params[:page], per_page: @per_page)
|
||||||
|
.each { |party|
|
||||||
|
party.favorited = (current_user) ? party.is_favorited(current_user) : false
|
||||||
|
}
|
||||||
|
@count = Party.where(conditions).count
|
||||||
else
|
else
|
||||||
render_not_found_response
|
render_not_found_response
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,11 @@
|
||||||
collection @parties
|
node :count do
|
||||||
|
@count
|
||||||
|
end
|
||||||
|
|
||||||
extends 'parties/base'
|
node :total_pages do
|
||||||
|
(@count.to_f / @per_page > 1) ? (@count.to_f / @per_page).ceil() : 1
|
||||||
|
end
|
||||||
|
|
||||||
|
node(:results) {
|
||||||
|
partial('parties/base', object: @parties)
|
||||||
|
} unless @parties.empty?
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,19 @@
|
||||||
object @user
|
object false
|
||||||
|
|
||||||
extends 'api/v1/users/base'
|
node :user do
|
||||||
|
partial('users/base', object: @user)
|
||||||
|
end
|
||||||
|
|
||||||
node(:parties) {
|
child :parties do
|
||||||
partial('parties/base', object: @parties)
|
node :count do
|
||||||
} unless @parties.empty?
|
@count
|
||||||
|
end
|
||||||
|
|
||||||
|
node :total_pages do
|
||||||
|
(@count.to_f / @per_page > 1) ? (@count.to_f / @per_page).ceil() : 1
|
||||||
|
end
|
||||||
|
|
||||||
|
node :results do
|
||||||
|
partial('parties/base', object: @parties)
|
||||||
|
end unless @parties.empty?
|
||||||
|
end
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2022_03_15_005952) do
|
ActiveRecord::Schema.define(version: 2022_03_15_011802) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "btree_gin"
|
enable_extension "btree_gin"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue