Last active
May 23, 2024 20:45
-
-
Save manish-shrivastava/67a09b8c4648a0e3ee6c88f3ce0aea9c to your computer and use it in GitHub Desktop.
ev_stations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# model code will have filters scope | |
class EvStation < ApplicationRecord | |
has_many :connections | |
has_many :amenities | |
scope :filter_by_free, ->(is_free) { where(is_free: is_free) } | |
scope :filter_by_usage_type, ->(usage_type_id) { where(usage_type_id: usage_type_id) } | |
scope :filter_by_power, ->(power_kw) { where(id: Connection.select(:ev_station_id).where("power_kw > ?", power_kw)) } | |
scope :filter_by_amenities, ->(amenity_ids) { joins(:amenities).where(amenities: { id: amenity_ids.split(',').map(&:to_i) }).distinct } | |
scope :filter_by_usage_types, ->(usage_type_ids) { where(usage_type_id: usage_type_ids.split(',').map(&:to_i)) } | |
scope :filter_by_connection_types, ->(connection_type_ids) { joins(:connections).where(connections: { connection_type_id: connection_type_ids.split(',').map(&:to_i) }).distinct } | |
scope :filter_by_rating, ->(rating) { where("rating >= ?", rating) } | |
geocoded_by :address | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EvStationsController < ApplicationController | |
def index | |
ev_stations = EvStation.all | |
# Filtering by geographical bounds | |
if bounds_provided? | |
ev_stations = ev_stations.where( | |
"latitude >= ? AND latitude <= ? AND longitude >= ? AND longitude <= ?", | |
bounds_sw_lat, bounds_ne_lat, bounds_sw_lng, bounds_ne_lng | |
) | |
end | |
# Applying additional filters based on parameters | |
ev_stations = ev_stations.filter_by_free(params[:is_free]) if params[:is_free].present? | |
ev_stations = ev_stations.filter_by_usage_type(params[:usage_type_id]) if params[:usage_type_id].present? | |
ev_stations = ev_stations.filter_by_power(params[:power_kw]) if params[:power_kw].present? | |
ev_stations = ev_stations.filter_by_amenities(params[:amenity_ids]) if params[:amenity_ids].present? | |
ev_stations = ev_stations.filter_by_usage_types(params[:usage_type_ids]) if params[:usage_type_ids].present? | |
ev_stations = ev_stations.filter_by_connection_types(params[:connection_type_ids]) if params[:connection_type_ids].present? | |
ev_stations = ev_stations.filter_by_rating(params[:rating]) if params[:rating].present? | |
# Applying proximity search | |
ev_stations = ev_stations.near([latitude, longitude], searching_distance) if latitude.present? && longitude.present? | |
render json: ev_stations, each_serializer: CompactEvStationSerializerSerializer | |
end | |
private | |
def bounds_provided? | |
bounds_sw_lat.present? && bounds_ne_lat.present? && bounds_sw_lng.present? && bounds_ne_lng.present? | |
end | |
def latitude | |
params[:latitude] | |
end | |
def longitude | |
params[:longitude] | |
end | |
def bounds_sw_lat | |
params[:bounds_sw_lat] | |
end | |
def bounds_ne_lat | |
params[:bounds_ne_lat] | |
end | |
def bounds_sw_lng | |
params[:bounds_sw_lng] | |
end | |
def bounds_ne_lng | |
params[:bounds_ne_lng] | |
end | |
def searching_distance | |
params[:searching_distance] || 10 # Default searching distance in miles or km | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment