Add pagination to the /api/v1/points endpoint

This commit is contained in:
Eugene Burmakin 2024-08-21 19:20:04 +02:00
parent 39d6584249
commit ca222b78ec
4 changed files with 49 additions and 2 deletions

View file

@ -1 +1 @@
0.11.0
0.11.1

View file

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

View file

@ -8,7 +8,12 @@ class Api::V1::PointsController < ApplicationController
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

View file

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