Add pagination headers to the API response

This commit is contained in:
Eugene Burmakin 2024-09-14 22:52:25 +02:00
parent cf9699a932
commit 667a1b2e3d
6 changed files with 22 additions and 4 deletions

View file

@ -1 +1 @@
0.13.6
0.13.7

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -49,8 +49,8 @@
<div class="flex justify-between my-5">
<%= 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?" } %>
<div class="flex justify-center">
<%= @points_number %> points found
<div>
<%= page_entries_info @points, entry_name: 'point' %>
</div>
<div class="flex justify-end">
<span class="mr-2">Order by:</span>

View file

@ -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