edit this code so thatwhen finding the departure/destination statiion search like start…
- User
edit this code so thatwhen finding the departure/destination statiion search like start with the provided string. for example if i search pala it should also return pala private bus stand require ‘time’
class Api::V1::SchedulesController < ApplicationController def index departure_station = params[:departure].upcase if params[:departure].present? destination_station = params[:destination].upcase if params[:destination].present? time_param = params[:time] exclude_param = params[:restrict] time = time_param.present? ? Time.parse(time_param) : nil exclude = exclude_param.present? ? exclude_param.downcase == ‘true’ : false
if departure_station.present? && destination_station.present?
all schedules with both dep and dest
if postgres use the query in below
schedules = Schedule.joins(:stations)
.where(stations: { name: [departure_station, destination_station] })
.select(‘schedules.*’)
.group(‘schedules.id’)
.having(‘COUNT(DISTINCT stations.name) = 2’)
.distinct
if sqlite use the query in below
schedules = Schedule.joins(:stations) .where(stations: { name: [departure_station, destination_station] }) .group(‘schedules.id’) .having(‘COUNT(DISTINCT stations.name) = 2’) .distinct .includes(:stations, route: :bus_schedule) trips = [] schedules.each do |schedule| stations = schedule.stations.order(:id) station_names = stations.map(&:name)
check if dep station comes before dest station using index
departure_index = station_names.find_index(departure_station) destination_index = station_names.find_index(destination_station)
if departure_index && destination_index && departure_index < destination_index departure_station_data = stations[departure_index] departure_time = Time.parse(departure_station_data.departure_time)
optional time parameter
next if time && departure_time < time
trip_data = { vehicle_number: schedule.route.bus_schedule.vehicle_number, trip: schedule.trip, stations: stations.map do |station| { station: station.name, arrivalTime: station.arrival_time, departureTime: station.departure_time } end }
Exclude stations before departure and after destination if exclude parameter is true
if exclude trip_data[:stations] = trip_data[:stations][departure_index…destination_index] end
trips << trip_data end end
sort based on dep time of dep station
sorted_trips = trips.sort_by do |trip| time_str = trip[:stations].find { |station| station[:station] == departure_station }[:departureTime] Time.parse(time_str) # 12-h am/pm format end render json: sorted_trips, status: :ok
else render json: { error: 'Departure and destination stations are required. ’ }, status: :unprocessable_entity end end end
- Assistant
To allow searching for station names that start with the provided string (like "pala" also returning "pala private bus stand"), you'll need to adjust the
whereclause to use a 'LIKE' or 'ILIKE' query (for case-insensitive search). This adjustment will apply to both and . Also, make sure to add a wildcard character () at the end of the search string. - User
full code
- Assistant
- User
/workspaces/Kerala-Private-Bus-Timing-API/app/controllers/api/v1/schedules_controller.rb:20: syntax error, unexpected local variable or method, expecting ')' trips = [] ^~~~~
- Assistant
The error message suggests there's a syntax issue with the code around the line where
trips = []is defined. This specific error could result from multiple causes, including issues earlier in the code or an incorrect use of quotes or parentheses.Let's verify the code. The error may not necessarily be on the line with . It could be due to a syntax issue before this line. For example, if a parenthesis or quote is not closed properly, Ruby might see the variable declaration as an error because it's looking for a closing or quote.