Add api/v1 base controller
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
module Api::V1::Parks
|
module Api::V1
|
||||||
class BaseController < ApplicationController
|
class Parks::BaseController < BaseController
|
||||||
before_action :set_park
|
before_action :set_park
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -1,31 +1,24 @@
|
|||||||
class Api::V1::ParksController < ApplicationController
|
module Api::V1
|
||||||
DEFAULT_PAGE_SIZE = 10
|
class ParksController < BaseController
|
||||||
before_action :set_park, only: :show
|
before_action :set_park, only: :show
|
||||||
|
|
||||||
def index
|
def index
|
||||||
parks = Park.all
|
parks = Park.all
|
||||||
if params[:state].present?
|
if params[:state].present?
|
||||||
parks = parks.where("states like '%' || ? || '%'", params[:state])
|
parks = parks.where("states like '%' || ? || '%'", params[:state])
|
||||||
|
end
|
||||||
|
render json: parks.limit(per_page).offset((page - 1) * per_page)
|
||||||
end
|
end
|
||||||
render json: parks.limit(per_page).offset((page - 1) * per_page)
|
|
||||||
end
|
|
||||||
|
|
||||||
def show
|
def show
|
||||||
render json: @park
|
render json: @park
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def per_page
|
def set_park
|
||||||
(params[:per_page].presence || DEFAULT_PAGE_SIZE).to_i
|
@park = Park.find_by(code: params[:code])
|
||||||
end
|
head :not_found unless @park.present?
|
||||||
|
end
|
||||||
def page
|
|
||||||
(params[:page].presence || 1).to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_park
|
|
||||||
@park = Park.find_by(code: params[:code])
|
|
||||||
head :not_found unless @park.present?
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user