示例#1
0
 private void ValidateInput(BusInput input)
 {
     if (input.XCoordinate == null || input.YCoordinate == null || input.RouteId == null || input.IMEI == null)
     {
         throw new ArgumentException("A Route ID and X and Y coordinates must be included in the body of the request.");
     }
 }
示例#2
0
        public async Task <IActionResult> PostBusLocation(BusInput input)
        {
            ValidateInput(input);

            var buses = _context.Buses.Where(b => b.IMEI == input.IMEI.Value).ToList();

            if (buses.Count == 0)
            {
                var bus = await _busManager.GetNewBus(input);

                _context.Buses.Add(bus);
            }
            else if (buses.Count == 1)
            {
                await _busManager.UpdateExistingBus(buses[0], input);
            }
            else
            {
                throw new ArgumentException($"Multiple buses were found with IMEI number '{input.IMEI}'! None were updated.");
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }
示例#3
0
        /// <summary>
        /// Create a new <see cref="Bus"/> from the data given in the POST request
        /// </summary>
        /// <param name="input">The input <see cref="BusInput"/> from the request</param>
        /// <returns>A new <see cref="Bus"/></returns>
        public async Task <Bus> GetNewBus(BusInput input)
        {
            var route = await _context.Routes.FindAsync(input.RouteId);

            var nextSchedule = GetNextSchedule(route, DateTime.Now.TimeOfDay);

            return(new Bus
            {
                XCoordinate = input.XCoordinate.Value,
                YCoordinate = input.YCoordinate.Value,
                RouteId = input.RouteId.Value,
                TripId = nextSchedule.TripId,
                IMEI = input.IMEI.Value,
                InactiveFlag = input.RouteId.Value == 0 ? true : false
            });
        }
示例#4
0
        /// <summary>
        /// Set the <see cref="Bus"/> attributes based on the given <see cref="BusInput"/>
        /// </summary>
        /// <param name="bus">The bus to update</param>
        /// <param name="input">The input <see cref="BusInput"/> from the request</param>
        public async Task UpdateExistingBus(Bus bus, BusInput input)
        {
            bus.XCoordinate  = input.XCoordinate.Value;
            bus.YCoordinate  = input.YCoordinate.Value;
            bus.RouteId      = input.RouteId.Value;
            bus.InactiveFlag = input.RouteId.Value == 0 ? true : false;

            var route = await _context.Routes.FindAsync(input.RouteId);

            var closestStop = GetClosestStop(bus, route);

            if (closestStop != null)
            {
                bus.LastStopId   = closestStop.StopId;
                bus.LastStopTime = DateTime.Now.TimeOfDay;

                if (closestStop.FirstStopFlg)
                {
                    var firstStopSchedules = route.Schedules.Where(s => s.Stop.FirstStopFlg);
                    var closestSchedule    = GetClosestSchedule(firstStopSchedules, DateTime.Now.TimeOfDay);
                    bus.TripId = closestSchedule.TripId;
                }
            }
        }