Files
national-parks-api/app/controllers/api/v1/parks_controller.rb
2025-10-07 23:05:12 -07:00

22 lines
454 B
Ruby

class Api::V1::ParksController < ApplicationController
DEFAULT_PAGE_SIZE = 10
def index
parks = Park.all
if params[:state].present?
parks = parks.where("states like '%' || ? || '%'", params[:state])
end
render json: parks.limit(per_page).offset((page - 1) * per_page)
end
private
def per_page
(params[:per_page].presence || DEFAULT_PAGE_SIZE).to_i
end
def page
(params[:page].presence || 1).to_i
end
end