the stations are ordered by their sequence in the route. it should only return where destination station is after departure station. so modify the code without comparing the time
render json: schedules.map { |schedule|
{
trip: schedule.trip,
stations: schedule.stations.map { |station|
{
station: station.name,
arrivalTime: station.arrival_time,
departureTime: station.departure_time
}
}
}
}, status: :ok
else
render json: { error: ‘Departure and destination stations are required.’ }, status: :unprocessable_entity
end
end
end
class BusSchedule < ApplicationRecord
has_many :routes
end
app/models/route.rb
class Route < ApplicationRecord
belongs_to :bus_schedule
has_many :schedules
end
app/models/schedule.rb
class Schedule < ApplicationRecord
belongs_to :route
has_many :stations
end
app/models/station.rb
class Station < ApplicationRecord
belongs_to :schedule
end
Based on the provided code and the indicated requirement that you want to ensure that the destination station comes after the departure station without comparing the times, you can assume that the sequence in which stations are added to the schedule implies the route's order. However, your current code needs a clear definition of how sequences are stored and compared between different stations within the same route.
For simplicity, I'll assume that each Station has a sequence_number field, which indicates its order within the route. You can alter your code to utilize this field to ensure the destination station has a higher sequence number than the departure station.
Below, I've modified the code in the controller to use the assumption stated above. I'm also making use of the ActiveRecord's eager loading feature includes to prevent N+1 queries:
# app/controllers/api/v1/schedules_controller.rb
class Api::V1::SchedulesController < ApplicationController
def index
departure_station = params[:departure]
destination_station = params[:destination]
if departure_station.present? && destination_station.present?
schedules = Schedule.includes(stations: :schedule)
.where(stations: { name: [departure_station, destination_station] })
.select('schedules.id, schedules.trip, stations.name as station_name, stations.sequence_number, stations.arrival_time, stations.departure_time')
.order('schedules.trip asc, stations.sequence_number asc')
# Assuming schedules have stations in order
filtered_schedules = schedules.each_with_object({}) do |station, hash|
trip_id = station.id
hash[trip_id] ||= { trip: station.trip, stations: [] }
hash[trip_id][:stations] << {
station: station.station_name,
arrivalTime: station.arrival_time.strftime('%I:%M %p'),
departureTime: station.departure_time.strftime('%I:%M %p')
}
end.values.select do |schedule|
dep_index = schedule[:stations].index { |hash| hash[:station] == departure_station }
dest_index = schedule[:stations].index { |hash| hash[:station] == destination_station }
dep_index && dest_index && dep_index < dest_index
end
render json: filtered_schedules, status: :ok
else
render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
end
end
end
In this modification:
Schedules are joined with stations and filtered to only include those with the desired departure and destination stations.
There's an assumption that there is a sequence_number field within the stations model. Routes are ordered by sequence_number to maintain the integrity of the route's progression.
The selection of schedules now depends on the indexed position of the departure and destination stations. The destination must come after the departure station (hence, dep_index < dest_index).
Due to the joins being more complex, the selection combines fields across Schedule and Station into a flattened structure, which is then filtered for correct ordering.
Note that the time comparison has been entirely removed, as per your request.
Make sure your models have the necessary associations and fields (like sequence_number) to support route ordering. If you don't have a sequence field, you will need to implement an alternative method that reflects the route ordering within your database schema and adjust the code accordingly.