Skip to content

Commit

Permalink
Add pagination to the /api/v1/points endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Freika committed Aug 21, 2024
1 parent 39d6584 commit ca222b7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .app_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.0
0.11.1
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ 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.11.1] — 2024-08-21

### Changed

- `/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.

## [0.11.0] — 2024-08-21

### Added
Expand Down
7 changes: 6 additions & 1 deletion app/controllers/api/v1/points_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ def index
start_at = params[:start_at]&.to_datetime&.to_i
end_at = params[:end_at]&.to_datetime&.to_i || Time.zone.now.to_i

points = current_api_user.tracked_points.where(timestamp: start_at..end_at)
points = current_api_user
.tracked_points
.where(timestamp: start_at..end_at)
.order(:timestamp)
.page(params[:page])
.per(params[:per_page] || 100)

render json: points
end
Expand Down
36 changes: 36 additions & 0 deletions spec/requests/api/v1/points_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Api::V1::Points', type: :request do
let!(:user) { create(:user) }
let!(:points) { create_list(:point, 150, user:) }

describe 'GET /index' do
it 'renders a successful response' do
get api_v1_points_url(api_key: user.api_key)

expect(response).to be_successful
end

it 'returns a list of points' do
get api_v1_points_url(api_key: user.api_key)

expect(response).to have_http_status(:ok)

json_response = JSON.parse(response.body)

expect(json_response.size).to eq(100)
end

it 'returns a list of points with pagination' do
get api_v1_points_url(api_key: user.api_key, page: 2, per_page: 10)

expect(response).to have_http_status(:ok)

json_response = JSON.parse(response.body)

expect(json_response.size).to eq(10)
end
end
end

0 comments on commit ca222b7

Please sign in to comment.