예제 #1
0
        public async Task <IActionResult> New(RouteViewModel routeViewModel)
        {
            // If model is empty display validation Error
            if (!ModelState.IsValid)
            {
                return(View());
            }

            // Get route info from API
            var routeResult = await routeService.GetRouteInfoFromGoogle(
                routeViewModel.InitialAddress,
                routeViewModel.DestinationAddress);

            if (!routeResult.Success)
            {
                ViewBag.ErrorMessage = routeResult.ErrorMessage;
                return(View());
            }

            // Get coordinates based on address
            var initialAddressCoordResult = await routeService.GetCoordinateFromAddress(routeViewModel.InitialAddress);

            if (!initialAddressCoordResult.Success)
            {
                ViewBag.ErrorMessage = initialAddressCoordResult.ErrorMessage;
                return(View());
            }

            var destinationAddressCoordResult = await routeService.GetCoordinateFromAddress(routeViewModel.DestinationAddress);

            if (!destinationAddressCoordResult.Success)
            {
                ViewBag.ErrorMessage = destinationAddressCoordResult.ErrorMessage;
                return(View());
            }

            // Get Fuel consumption
            double routeFuelConsumption = routeCalculator.GetRouteFuelConsumption(routeViewModel.FuelConsumptionPer100, routeResult.TotalDistance.Value);

            // Save route info to db
            var routeToSave = new RouteInfo()
            {
                InitialAddress           = routeViewModel.InitialAddress,
                InitialAddressCoords     = Formater.GetLatLngCoords(initialAddressCoordResult),
                DestinationAddress       = routeViewModel.DestinationAddress,
                DestinationAddressCoords = Formater.GetLatLngCoords(destinationAddressCoordResult),
                FuelConsumptionPer100    = routeViewModel.FuelConsumptionPer100,
                RouteFuelConsumption     = routeFuelConsumption.ToString(),
                TotalDistance            = routeResult.TotalDistance.Text,
                TotalDuration            = routeResult.TotalDuration.Text
            };

            context.RouteInfos.Add(routeToSave);
            await context.SaveChangesAsync();

            // Go to route detail
            return(RedirectToAction(actionName: "Detail", controllerName: "Route", routeValues: new { routeId = routeToSave.Id }));
        }
예제 #2
0
        public void ShouldCalulateCorrectFuelConsumption(double consumptionPer100, double distance, double correctResult)
        {
            var result = routeCalculator.GetRouteFuelConsumption(consumptionPer100, distance);

            Assert.AreEqual(result, correctResult);
        }