Skip to content

Commit ca222b7

Browse files
committed
Add pagination to the /api/v1/points endpoint
1 parent 39d6584 commit ca222b7

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

.app_version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.11.0
1+
0.11.1

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

8+
## [0.11.1] — 2024-08-21
9+
10+
### Changed
11+
12+
- `/api/v1/points` endpoint now returns 100 points by default. You can specify the number of points to return by passing the `per_page` query parameter. Example: `/api/v1/points?per_page=50` will return 50 points. Also, `page` query parameter is now available to paginate the results. Example: `/api/v1/points?per_page=50&page=2` will return the second page of 50 points.
13+
814
## [0.11.0] — 2024-08-21
915

1016
### Added

app/controllers/api/v1/points_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ def index
88
start_at = params[:start_at]&.to_datetime&.to_i
99
end_at = params[:end_at]&.to_datetime&.to_i || Time.zone.now.to_i
1010

11-
points = current_api_user.tracked_points.where(timestamp: start_at..end_at)
11+
points = current_api_user
12+
.tracked_points
13+
.where(timestamp: start_at..end_at)
14+
.order(:timestamp)
15+
.page(params[:page])
16+
.per(params[:per_page] || 100)
1217

1318
render json: points
1419
end

spec/requests/api/v1/points_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe 'Api::V1::Points', type: :request do
6+
let!(:user) { create(:user) }
7+
let!(:points) { create_list(:point, 150, user:) }
8+
9+
describe 'GET /index' do
10+
it 'renders a successful response' do
11+
get api_v1_points_url(api_key: user.api_key)
12+
13+
expect(response).to be_successful
14+
end
15+
16+
it 'returns a list of points' do
17+
get api_v1_points_url(api_key: user.api_key)
18+
19+
expect(response).to have_http_status(:ok)
20+
21+
json_response = JSON.parse(response.body)
22+
23+
expect(json_response.size).to eq(100)
24+
end
25+
26+
it 'returns a list of points with pagination' do
27+
get api_v1_points_url(api_key: user.api_key, page: 2, per_page: 10)
28+
29+
expect(response).to have_http_status(:ok)
30+
31+
json_response = JSON.parse(response.body)
32+
33+
expect(json_response.size).to eq(10)
34+
end
35+
end
36+
end

0 commit comments

Comments
 (0)