public async Task <ActionResult> GetDistance(DistanceVM distanceVM) { try { if (ModelState.IsValid) { return(await Task.FromResult(RedirectToAction(nameof(Index), new { distance = distanceVM.MGLT }))); } return(await Task.FromResult(View())); } catch (ArgumentNullException e) { ModelState.AddModelError(nameof(distanceVM.MGLT), e.Message); return(await Task.FromResult(View(distanceVM))); } catch (ArgumentException e) { ModelState.AddModelError(nameof(distanceVM.MGLT), e.Message); return(await Task.FromResult(View(distanceVM))); } catch (Exception) { return(await Task.FromResult(View())); } }
private async Task <DistanceVM> GetOrGenerateDistance(int locationId1, int locationId2) { var location1 = ServiceFactory.GeolocationManagement.GetLocation(locationId1); var location2 = ServiceFactory.GeolocationManagement.GetLocation(locationId2); if (!location1.Success) { throw new ArgumentException($"An error occured with location1 `{location1}`. Message: {location1.Message}"); } if (!location2.Success) { throw new ArgumentException($"An error occured with location2 `{location2}`. Message: {location2.Message}"); } var point1 = new LocationPoint { Latitude = location1.Data.Latitude, Longitude = location1.Data.Longitude }; var point2 = new LocationPoint { Latitude = location2.Data.Latitude, Longitude = location2.Data.Longitude }; var calculateRouteResult = await ServiceFactory.TomTom.CalculateRoute(point1, point2); if (!calculateRouteResult.Success) { throw new ArgumentException($"An error occured with calculateRouteResult " + $"`{calculateRouteResult}`. Message: {calculateRouteResult.Message}"); } var leg = calculateRouteResult.Data.Routes.FirstOrDefault()?.Legs.FirstOrDefault(); var distance = new DistanceVM { TomTomInfo = leg == null ? string.Empty : Newtonsoft.Json.JsonConvert.SerializeObject(leg), Location1 = location1.Data.Id, Location2 = location2.Data.Id }; ServiceFactory.RouteManagement.MergeDistance(distance); var distanceResult = ServiceFactory.RouteManagement.GetDistance(distance.Location1, distance.Location2); if (!distanceResult.Success) { throw new ArgumentException($"An error occured with distanceResult" + $"`{distanceResult}` after merging data in database. Message: {distanceResult.Message}"); } return(distanceResult.Data); }
public BaseResult MergeDistance(DistanceVM distanceVM) { var result = new BaseResult(); try { var distance = Mapper.Map <Distance>(distanceVM); UnitOfWork.DistanceDao.Merge(distance); result.Success = true; result.Message = GeneralSuccessMessage; } catch (Exception ex) { result.Success = false; result.Message = GeneralErrorMessage; } return(result); }