Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· openaiHugging Face Suzhen/CodeChat

# app/controllers/api/v1/schedulescontroller.rb class Api::V1::SchedulesController <…

  1. User

    # 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.joins(stations: { schedule: { route: :bus_schedule } })

    .where(stations: { name: departure_station })

    .where(id: Schedule.joins(stations: { schedule: { route: :bus_schedule } })

    .where(stations: { name: destination_station })

    .pluck(:id))

    .group(:trip)

    .order(:trip)

    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 Api::V1::SchedulesController < ApplicationController def index departure_station = params[:departure] destination_station = params[:destination]

    if departure_station.present? && destination_station.present?
      schedules = Schedule.joins(stations: { schedule: { route: :bus_schedule } })
                          .where(stations: { name: departure_station })
                          .where(id: Schedule.joins(stations: { schedule: { route: :bus_schedule } })
                                            .where(stations: { name: destination_station })
                                            .pluck(:id))
                          .group(:trip)
                          .order(:trip)
    
      render json: schedules.map { |schedule|
        {
          trip: schedule.trip,
          stations: schedule.stations.map { |station|
            {
              station: station.name,
              arrivalTime: station.arrival_time,
              departureTime: station.departure_time
            }
          }.reject { |s| s[:station] == departure_station || s[:station] == destination_station || station_sequence(s[:station], schedule.route_id) < station_sequence(departure_station, schedule.route_id) }
        }
      }, status: :ok
    else
      render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
    end
    

    end

    private

    def station_sequence(station_name, route_id) Station.where(name: station_name, route_id: route_id).pluck(:sequence).first end end this returns below when i want manjali to athani [{"trip":1,"stations":[{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"06:35 am","departureTime":"06:35 am"},{"station":"ATHANI","arrivalTime":"06:53 am","departureTime":"06:53 am"},{"station":"MANJALY","arrivalTime":"07:15 am","departureTime":"07:15 am"},{"station":"NORTH PARAVUR","arrivalTime":"07:25 am","departureTime":"07:25 am"}]},{"trip":2,"stations":[{"station":"NORTH PARAVUR","arrivalTime":"07:30 am","departureTime":"07:30 am"},{"station":"MANJALY","arrivalTime":"07:40 am","departureTime":"07:40 am"},{"station":"ATHANI","arrivalTime":"08:02 am","departureTime":"08:02 am"},{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:20 am","departureTime":"08:20 am"}]},{"trip":3,"stations":[{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:38 am","departureTime":"08:38 am"},{"station":"ATHANI","arrivalTime":"08:56 am","departureTime":"08:56 am"},{"station":"MANJALY","arrivalTime":"09:18 am","departureTime":"09:18 am"},{"station":"NORTH PARAVUR","arrivalTime":"09:28 am","departureTime":"09:28 am"}]}]

    but i only want

    [{"trip":2,"stations":[{"station":"NORTH PARAVUR","arrivalTime":"07:30 am","departureTime":"07:30 am"},{"station":"MANJALY","arrivalTime":"07:40 am","departureTime":"07:40 am"},{"station":"ATHANI","arrivalTime":"08:02 am","departureTime":"08:02 am"},{"station":"ANGAMALY PRIVATE BUS STATION","arrivalTime":"08:20 am","departureTime":"08:20 am"}]}]

    because other two are athani to manjali

  2. Assistant

    It looks like your station_sequence method works correctly to get the sequence number of a station within a given route_id. However, it appears the filtering of the stations within the render json block using .reject { ... } is not functioning as needed. As a result, you're getting unwanted trips in your output that don't match the correct direction from departure_station to destination_station.

    To ensure you only get the schedules that go from departure_station to destination_station, the following adjustments can be made:

    1. Fetch all the schedules that pass through both departure_station and destination_station. This can be done by ensuring the departure_station comes before the destination_station on the route.
    2. For each schedule, ensure that the itinerary from departure_station to destination_station is in the correct order.

    Let's refactor the code to achieve the correct functionality:

    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)  # Include related stations to reduce DB queries
                              .select { |schedule| 
                                schedule_has_correct_itinerary(schedule, departure_station, destination_station)
                              }
    
          render json: schedules.map { |schedule|
            {
              trip: schedule.trip,
              stations: schedule.stations.select { |station| 
                included_in_itinerary?(station, schedule, departure_station, destination_station) 
              }.map { |station|
                {
                  station: station.name,
                  arrivalTime: station.arrival_time.strftime('%I:%M %p'),
                  departureTime: station.departure_time.strftime('%I:%M %p')
                }
              }
            }
          }, status: :ok
        else
          render json: { error: 'Departure and destination stations are required.' }, status: :unprocessable_entity
        end
      end
    
      private
    
      def schedule_has_correct_itinerary(schedule, departure_station, destination_station)
        departure_index = station_index(schedule.stations, departure_station)
        destination_index = station_index(schedule.stations, destination_station)
    
        departure_index && destination_index && departure_index < destination_index
      end
    
      def included_in_itinerary?(station, schedule, departure_station, destination_station)
        departure_index = station_index(schedule.stations, departure_station)
        destination_index = station_index(schedule.stations, destination_station)
        current_station_index = station_index(schedule.stations, station.name)
    
        current_station_index && departure_index && destination_index &&
          current_station_index > departure_index && current_station_index <= destination_index
      end
    
      def station_index(stations, station_name)
        stations.index { |station| station.name == station_name }
      end
    end
    

    Here's what was changed/improved:

    • We're now using includes(:stations) to tell Active Record to retrieve the associated Station records in a single query. This minimizes the number of database calls (N+1 query problem).
    • Two new helper methods, schedule_has_correct_itinerary and included_in_itinerary?, have been added to clean up and clarify the filtering logic in the index method.
    • schedule_has_correct_itinerary checks if the given schedule contains both the departure and destination station, and if the departure station comes before the destination station.
    • included_in_itinerary? determines if a station is part of the itinerary between the departure and destination stations of a given schedule.
    • This refactored version should now correctly filter out schedules that do not go from departure_station to destination_station, and for those that do, only include the relevant stations in the itinerary.

    Make sure to handle time zones correctly in arrivalTime and departureTime formatting since %I:%M %p will use the server's default time zone. If your stations have a different time zone, you should adjust the code to handle the time conversion accurately.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.