From 667a1b2e3d529b5b51c442843ff815ffe45c16eb Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 14 Sep 2024 22:52:25 +0200 Subject: [PATCH 1/4] Add pagination headers to the API response --- .app_version | 2 +- CHANGELOG.md | 7 +++++++ app/controllers/api/v1/points_controller.rb | 3 +++ app/controllers/points_controller.rb | 1 - app/views/points/index.html.erb | 4 ++-- spec/requests/api/v1/points_spec.rb | 9 +++++++++ 6 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.app_version b/.app_version index ebf55b3d..5daaa7ba 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.13.6 +0.13.7 diff --git a/CHANGELOG.md b/CHANGELOG.md index 621af4c4..1c7d1364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.13.7] — 2024-09-15 + +### Added + +- `GET /api/v1/points` response now will include `X-Total-Pages` and `X-Current-Page` headers to make it easier to work with the endpoint + + ## [0.13.6] — 2024-09-13 ### Fixed diff --git a/app/controllers/api/v1/points_controller.rb b/app/controllers/api/v1/points_controller.rb index d6a32c8d..6a7d2e59 100644 --- a/app/controllers/api/v1/points_controller.rb +++ b/app/controllers/api/v1/points_controller.rb @@ -12,6 +12,9 @@ class Api::V1::PointsController < ApiController .page(params[:page]) .per(params[:per_page] || 100) + response.set_header('X-Current-Page', points.current_page) + response.set_header('X-Total-Pages', points.total_pages) + render json: points end diff --git a/app/controllers/points_controller.rb b/app/controllers/points_controller.rb index fa9708d4..76bb9f24 100644 --- a/app/controllers/points_controller.rb +++ b/app/controllers/points_controller.rb @@ -16,7 +16,6 @@ class PointsController < ApplicationController @start_at = Time.zone.at(start_at) @end_at = Time.zone.at(end_at) - @points_number = @points.except(:limit, :offset).size @imports = current_user.imports.order(created_at: :desc) end diff --git a/app/views/points/index.html.erb b/app/views/points/index.html.erb index 8837a00d..7f7d7fd9 100644 --- a/app/views/points/index.html.erb +++ b/app/views/points/index.html.erb @@ -49,8 +49,8 @@
<%= f.submit "Delete Selected", class: "px-4 py-2 bg-red-500 text-white rounded-md", data: { confirm: "Are you sure?", turbo_confirm: "Are you sure?" } %> -
- <%= @points_number %> points found +
+ <%= page_entries_info @points, entry_name: 'point' %>
Order by: diff --git a/spec/requests/api/v1/points_spec.rb b/spec/requests/api/v1/points_spec.rb index 2378df2a..fa10c5c8 100644 --- a/spec/requests/api/v1/points_spec.rb +++ b/spec/requests/api/v1/points_spec.rb @@ -32,5 +32,14 @@ RSpec.describe 'Api::V1::Points', type: :request do expect(json_response.size).to eq(10) end + + it 'returns a list of points with pagination headers' do + get api_v1_points_url(api_key: user.api_key, page: 2, per_page: 10) + + expect(response).to have_http_status(:ok) + + expect(response.headers['X-Current-Page']).to eq(2) + expect(response.headers['X-Total-Pages']).to eq(15) + end end end From 1014af527c7fa790ea1463df73c61b39ea408ef9 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sat, 14 Sep 2024 22:53:27 +0200 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c7d1364..2134d493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added - `GET /api/v1/points` response now will include `X-Total-Pages` and `X-Current-Page` headers to make it easier to work with the endpoint +- The Pages point now shows total number of points found for provided date range ## [0.13.6] — 2024-09-13 From 6d05065659fb50cb06c71475a9c67b9e529ce4df Mon Sep 17 00:00:00 2001 From: sunstep <73174517+sunstep@users.noreply.github.com> Date: Sun, 15 Sep 2024 12:07:46 +0200 Subject: [PATCH 3/4] Convert the headers to string It seems like all HTTP headers are string, while other types may work, its more safe to convert the types to strings to ensure no trouble is caused by non string types. Even if it does work, at least now the types are consistent, as headers are being put as string and they are also retrieved as string. --- app/controllers/api/v1/points_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/points_controller.rb b/app/controllers/api/v1/points_controller.rb index 6a7d2e59..fbf200c4 100644 --- a/app/controllers/api/v1/points_controller.rb +++ b/app/controllers/api/v1/points_controller.rb @@ -12,8 +12,8 @@ class Api::V1::PointsController < ApiController .page(params[:page]) .per(params[:per_page] || 100) - response.set_header('X-Current-Page', points.current_page) - response.set_header('X-Total-Pages', points.total_pages) + response.set_header('X-Current-Page', points.current_page.to_s) + response.set_header('X-Total-Pages', points.total_pages.to_s) render json: points end From 4496cd948fa960932ffcbda3565831434309dc97 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sun, 15 Sep 2024 19:55:00 +0200 Subject: [PATCH 4/4] Fix spec expectations --- spec/requests/api/v1/points_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/requests/api/v1/points_spec.rb b/spec/requests/api/v1/points_spec.rb index fa10c5c8..d5d8957a 100644 --- a/spec/requests/api/v1/points_spec.rb +++ b/spec/requests/api/v1/points_spec.rb @@ -38,8 +38,8 @@ RSpec.describe 'Api::V1::Points', type: :request do expect(response).to have_http_status(:ok) - expect(response.headers['X-Current-Page']).to eq(2) - expect(response.headers['X-Total-Pages']).to eq(15) + expect(response.headers['X-Current-Page']).to eq('2') + expect(response.headers['X-Total-Pages']).to eq('15') end end end