public void UpdateFueling(SqlConnection connection, TruckUpdateRequest request) { foreach (Tank tank in request.FuelTanks) { var tankId = tank.Id; var stopId = request.StopId; var queryString = @"Declare @OldFuelLevel decimal(6, 2) Set @OldFuelLevel = (Select FuelLevel From Tank Where Tank.Id = @TankId) Declare @EventId int Set @EventId = (Select FE.Id From [Stop] Join FuelingEvent as FE on FuelingEvent.StopId = [Stop].Id AND DatePart(dayofyear, FE.ScheduledTime) = DatePart(dayofyear, GetDate()) AND DatePart(year, FE.ScheduledTime) = DatePart(year, GetDate()) Where [Stop].Id = @StopId Update Tank Set FuelLevel = @CurrentFuelLevel Where Tank.Id = @TankId Update FuelingEvent Set ActualNeed = @OldFuelLevel - @CurrentFuelLevel, DeliveryTime = GetDate() Where FuelingEvent.Id = @EventId"; var tankUpdate = connection.QueryFirst <int>(queryString, new { tankId, stopId }); } }
public Truck UpdateTruckStatus(TruckUpdateRequest request) { using (SqlConnection connection = new SqlConnection(_connectionString)) { var truckId = request.TruckId; var currentStop = request.StopId; UpdateFueling(connection, request); var truckString = @"Select OperatingRegionId From Truck Where Truck.Id = @TruckId"; var regionId = connection.QueryFirst <int>(truckString, new { truckId }); var fuelingEvents = new DispatchConnection(_dbConfig).GetTodaysPendingFuelingEvents(regionId); /* Calls an external mapping API using HttpClient, gets travel times to each stop in region from current location, then runs through * routing process as described in information doc to determine next stop for truck and then returns that stop. */ var stopId = RouteTruck(fuelingEvents, currentStop); UpdateRoute(connection, truckId, stopId); var truckOrdersQueryString = @"Select [Stop].Location From Truck Join [Stop] on [Stop].Id = Truck.StopId Where Truck.Id = @TruckId"; var truckOrders = connection.QueryFirst <Truck>(truckOrdersQueryString, new { truckId }); return(truckOrders); } throw new Exception("Could not update truck status"); }
[HttpPut] public ActionResult UpdateTruckStatus(TruckUpdateRequest request) { var truckOrders = _connection.UpdateTruckStatus(request); return(Accepted(truckOrders)); }