public bool CreateCustomRoute(CustomRouteRequest route, out IList <RouteResponse> result) { var routes = _context.GradedRoutesWithIncludedRelatedData(); var places = FindPlacesFromRoutes(routes); PlaceResponse start = route.Start; PlaceResponse end = route.End; double distanceFromStart = FindClosestPlace(start, places, out Miejsce closestToStart); double distanceFromEnd = FindClosestPlace(end, places, out Miejsce closestToEnd); if (distanceFromStart <= MAXIMUM_DISTANCE && distanceFromEnd <= MAXIMUM_DISTANCE) { var mountainGroup = FindMountainGroup(closestToStart); var mountainGroupName = _context.GrupaGorska.First(m => m.Id == mountainGroup).Nazwa; routes = routes.Where(r => r.GrupaGorskaId == mountainGroup).ToList(); places = FindPlacesFromRoutes(routes); //create graph from locations in a mountain group var graph = new BidirectionalGraph <PlaceVertex, Edge <PlaceVertex> >(); var edgeCostDictionary = new Dictionary <Edge <PlaceVertex>, int>(); PopulateGraph(graph, edgeCostDictionary, places, routes); //find path between start location and end location using A* Algorithm AStarShortestPathAlgorithm <PlaceVertex, Edge <PlaceVertex> > astar = new AStarShortestPathAlgorithm <PlaceVertex, Edge <PlaceVertex> >(graph, x => edgeCostDictionary[x], x => 0); var startVertex = new PlaceVertex(closestToStart.Id); var endVertex = new PlaceVertex(closestToEnd.Id); if (astar.ComputeDistanceBetween(startVertex, endVertex, out var path)) { result = new List <RouteResponse>(); if (distanceFromStart >= MINIMUM_DISTANCE) { result.Add(RouteResponse.CreateCustomRoute(start, PlaceResponse.BuildFromModel(closestToStart), mountainGroupName, distanceFromStart)); } result = result.Concat(CreateResponseListFromPath(path)).ToList(); if (distanceFromEnd >= MINIMUM_DISTANCE) { result.Add(RouteResponse.CreateCustomRoute(PlaceResponse.BuildFromModel(closestToEnd), end, mountainGroupName, distanceFromEnd)); } return(true); } else { result = new List <RouteResponse>(); return(false); } } else { result = new List <RouteResponse>(); return(false); } }
public IActionResult CreateCustomRoute(CustomRouteRequest route) { if (_apiCustomRouteService.CreateCustomRoute(route, out var customRoute)) { return(Ok(customRoute)); } else { return(BadRequest()); } }