From 1f3ba2307ffaf47f1dbeecbe1cbd30c1fb4e82ff Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 14 Mar 2022 19:42:29 -0700 Subject: [PATCH 1/5] 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" From 6756c24896d6e36825ac8114c3705e98e8c8f616 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 21 Mar 2022 03:53:34 -0700 Subject: [PATCH 2/5] Remove duplicate current_user method --- app/controllers/api/v1/api_controller.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb index 97cdd8a..424b216 100644 --- a/app/controllers/api/v1/api_controller.rb +++ b/app/controllers/api/v1/api_controller.rb @@ -27,7 +27,6 @@ module Api::V1 # before returning def current_user @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 end @@ -43,12 +42,6 @@ module Api::V1 set_content_type("application/javascript; charset=utf-8") end - def current_user - @current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token - - return @current_user - end - ### Error response methods def render_unprocessable_entity_response(exception) @exception = exception From a99f0c31c50cc85896e2dd91d1dc40a748374dcf Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 21 Mar 2022 03:53:52 -0700 Subject: [PATCH 3/5] Add pagination to favorites and teams --- app/controllers/api/v1/parties_controller.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index bcb15db..12ecc3d 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -18,6 +18,8 @@ class Api::V1::PartiesController < Api::V1::ApiController end def index + @per_page = 15 + now = DateTime.current start_time = (now - request.params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank? @@ -27,10 +29,14 @@ 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).paginate(page: request.params[:page], per_page: 15) + @parties = Party + .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 end @@ -38,6 +44,8 @@ class Api::V1::PartiesController < Api::V1::ApiController def favorites raise Api::V1::UnauthorizedError unless current_user + @per_page = 15 + now = DateTime.current start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank? @@ -47,10 +55,15 @@ 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).paginate(page: request.params[:page], per_page: 15) + @parties = Party + .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 end From 5d45b8898e59dbaf2c4dcf362959b1cead055b8c Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 21 Mar 2022 03:53:59 -0700 Subject: [PATCH 4/5] Add favorites to parties --- app/controllers/api/v1/users_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index d7bf5fc..7aec107 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -36,6 +36,8 @@ class Api::V1::UsersController < Api::V1::ApiController def show if @user + @per_page = 15 + now = DateTime.current start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank? @@ -45,10 +47,14 @@ 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).paginate(page: request.params[:page], per_page: 15) + @parties = Party + .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 render_not_found_response end From 483e9fe2268516511e23d01093d04548b82296c6 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Mon, 21 Mar 2022 03:54:13 -0700 Subject: [PATCH 5/5] Update templates --- app/views/api/v1/parties/all.json.rabl | 12 ++++++++++-- app/views/api/v1/users/show.json.rabl | 22 +++++++++++++++++----- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/app/views/api/v1/parties/all.json.rabl b/app/views/api/v1/parties/all.json.rabl index 5afe05e..44d8477 100644 --- a/app/views/api/v1/parties/all.json.rabl +++ b/app/views/api/v1/parties/all.json.rabl @@ -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? diff --git a/app/views/api/v1/users/show.json.rabl b/app/views/api/v1/users/show.json.rabl index 2c35693..9a729da 100644 --- a/app/views/api/v1/users/show.json.rabl +++ b/app/views/api/v1/users/show.json.rabl @@ -1,7 +1,19 @@ -object @user +object false -extends 'api/v1/users/base' +node :user do + partial('users/base', object: @user) +end -node(:parties) { - partial('parties/base', object: @parties) -} unless @parties.empty? \ No newline at end of file +child :parties do + node :count do + @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 \ No newline at end of file