From 1f3ba2307ffaf47f1dbeecbe1cbd30c1fb4e82ff Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 14 Mar 2022 19:42:29 -0700 Subject: [PATCH] Add pagination for collections --- app/controllers/api/v1/parties_controller.rb | 16 +++++++++------- app/controllers/api/v1/users_controller.rb | 7 ++++--- db/schema.rb | 2 +- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index a612e7f..bcb15db 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -19,7 +19,7 @@ class Api::V1::PartiesController < Api::V1::ApiController def index 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[:element] = request.params['element'] unless request.params['element'].blank? @@ -27,9 +27,10 @@ class Api::V1::PartiesController < Api::V1::ApiController conditions[:created_at] = start_time..now unless request.params['recency'].blank? conditions[:weapons_count] = 5..13 - @parties = Party.where(conditions).each { |party| - party.favorited = (current_user) ? party.is_favorited(current_user) : false - } + @parties = Party.where(conditions).paginate(page: request.params[:page], per_page: 15) + .each { |party| + party.favorited = (current_user) ? party.is_favorited(current_user) : false + } render :all, status: :ok end @@ -46,9 +47,10 @@ class Api::V1::PartiesController < Api::V1::ApiController conditions[:created_at] = start_time..now unless request.params['recency'].blank? conditions[:favorites] = { user_id: current_user.id } - @parties = Party.joins(:favorites).where(conditions).each { |party| - party.favorited = party.is_favorited(current_user) - } + @parties = Party.joins(:favorites).where(conditions).paginate(page: request.params[:page], per_page: 15) + .each { |party| + party.favorited = party.is_favorited(current_user) + } render :all, status: :ok end diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index b1ff491..d7bf5fc 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -45,9 +45,10 @@ class Api::V1::UsersController < Api::V1::ApiController conditions[:created_at] = start_time..now unless request.params['recency'].blank? conditions[:user_id] = @user.id - @parties = Party.where(conditions).each { |party| - party.favorited = (current_user) ? party.is_favorited(current_user) : false - } + @parties = Party.where(conditions).paginate(page: request.params[:page], per_page: 15) + .each { |party| + party.favorited = (current_user) ? party.is_favorited(current_user) : false + } else render_not_found_response end diff --git a/db/schema.rb b/db/schema.rb index e3f8601..a41e92d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # 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 enable_extension "btree_gin"