In my Ruby on Rails app, bike rental companies can manage all their bikes (reservations, payments etc.).
Context
I would like to offer a bike rental companies (shops
) the option to implement a booking form on their own website, so they can let customers make a reservation for a bike
.
- This booking form would then show
bike_categories
of whichbikes
are available for a givenarrival
anddeparture
date.
Question
In order to manage this, I would like to generate an API controller action showing the availability
for a certain bike_category
displaying the count
for the number of available bikes
belonging to this bike_category
.
According to this post
Design RESTful query API with a long list of query parameters
I should be able to deal with queries in my api, but how do I get the queries in my Rails controller?
Code
models
class Shop < ApplicationRecord
has_many :bike_categories, dependent: :destroy
has_many :bikes, through: :bike_categories
has_many :reservations, dependent: :destroy
end
class Reservation < ApplicationRecord
belongs_to :shop
belongs_to :bike
end
class Bike < ApplicationRecord
belongs_to :bike_category
has_many :reservations, dependent: :destroy
end
class BikeCategory < ApplicationRecord
belongs_to :shop
has_many :bikes, dependent: :destroy
end
routes
# api
namespace :api, defaults: { format: :json } do
namespace :v1 do
resources :shops, only: [ :show ]
resources :reservations, only: [ :show, :create ]
resources :bike_categories, only: [:index, :show, :availability]
end
end
controller/api/v1/bike_categories_controller.rb
class Api::V1::BikeCategoriesController < Api::V1::BaseController
acts_as_token_authentication_handler_for User, only: [:show, :index, availability]
def availability
# How to get the bike_category, arrival and departure?
end
end