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