Update paginated responses to include summary data
This commit is contained in:
@@ -3,11 +3,11 @@ class Api::V1::BaseController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def per_page
|
||||
(params[:per_page].presence || DEFAULT_PAGE_SIZE).to_i
|
||||
def pagination_limit
|
||||
(params[:limit].presence || DEFAULT_PAGE_SIZE).to_i
|
||||
end
|
||||
|
||||
def page
|
||||
(params[:page].presence || 1).to_i
|
||||
def pagination_offset
|
||||
(params[:offset].presence || 0).to_i
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,7 +3,12 @@ module Api::V1::Parks
|
||||
before_action :set_park
|
||||
|
||||
def index
|
||||
render json: @park.alerts.limit(per_page).offset((page - 1) * per_page)
|
||||
render json: {
|
||||
total: @park.alerts.count,
|
||||
limit: pagination_limit,
|
||||
offset: pagination_offset,
|
||||
alerts: @park.alerts.limit(pagination_limit).offset(pagination_offset)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,7 +7,12 @@ module Api::V1
|
||||
if params[:state].present?
|
||||
parks = parks.where("states like '%' || ? || '%'", params[:state])
|
||||
end
|
||||
render json: parks.limit(per_page).offset((page - 1) * per_page)
|
||||
render json: {
|
||||
total: parks.count,
|
||||
limit: pagination_limit,
|
||||
offset: pagination_offset,
|
||||
parks: parks.limit(pagination_limit).offset(pagination_offset)
|
||||
}
|
||||
end
|
||||
|
||||
def show
|
||||
|
||||
@@ -5,8 +5,8 @@ RSpec.describe "Api::V1::Parks::Alerts", type: :request do
|
||||
it "returns the alerts associated to a park" do
|
||||
get api_v1_park_alerts_url(parks(:one).code)
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.parsed_body.count).to eq(1)
|
||||
expect(response.parsed_body).to include(
|
||||
expect(response.parsed_body["alerts"].count).to eq(1)
|
||||
expect(response.parsed_body["alerts"]).to include(
|
||||
hash_including(
|
||||
:description, park_code: "crla", title: "Fire Restrictions in Effect",
|
||||
category: "caution", indexed_date: "2025-07-04T22:07:59.000Z"
|
||||
|
||||
@@ -5,12 +5,12 @@ RSpec.describe "Api::V1::Parks", type: :request do
|
||||
it "returns parks" do
|
||||
get api_v1_parks_url
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(response.parsed_body).to include(
|
||||
expect(response.parsed_body["parks"]).to include(
|
||||
hash_including(
|
||||
code: "crla", name: "Crater Lake National Park", states: "OR"
|
||||
)
|
||||
)
|
||||
expect(response.parsed_body).to include(
|
||||
expect(response.parsed_body["parks"]).to include(
|
||||
hash_including(
|
||||
code: "olym", name: "Olympic National Park", states: "WA"
|
||||
)
|
||||
@@ -19,18 +19,21 @@ RSpec.describe "Api::V1::Parks", type: :request do
|
||||
|
||||
it "filters by state" do
|
||||
get api_v1_parks_url, params: { state: "OR" }
|
||||
expect(response.parsed_body.pluck("code")).to eq(["crla"])
|
||||
expect(response.parsed_body["parks"].pluck("code")).to eq(["crla"])
|
||||
end
|
||||
|
||||
context "with pagination" do
|
||||
it "respects page size param" do
|
||||
get api_v1_parks_url, params: { per_page: 1 }
|
||||
expect(response.parsed_body.size).to eq(1)
|
||||
it "respects limit param" do
|
||||
get api_v1_parks_url, params: { limit: 1 }
|
||||
expect(response.parsed_body["total"]).to eq(2)
|
||||
expect(response.parsed_body["limit"]).to eq(1)
|
||||
expect(response.parsed_body["offset"]).to eq(0)
|
||||
expect(response.parsed_body["parks"].size).to eq(1)
|
||||
end
|
||||
|
||||
it "respects page param" do
|
||||
get api_v1_parks_url, params: { per_page: 1, page: 2 }
|
||||
expect(response.parsed_body.first["code"]).to eq("crla")
|
||||
it "respects offset param" do
|
||||
get api_v1_parks_url, params: { offset: 1 }
|
||||
expect(response.parsed_body["parks"].first["code"]).to eq("crla")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user