public IActionResult FindNearest(string cities)
        {
            string host = "https://homework.appulate.com/api";
            var    s    = new AirportService(host);

            string[] arr = cities.Split(",");

            try {
                var list = s.GetAirports(arr).Result;
                if (list.Count == 0)
                {
                    return(Ok("There are no airports found for cities"));
                }
                else
                {
                    List <AirportPair> pairs    = s.CalculateDistance(list);
                    List <AirportPair> shortest = new List <AirportPair>();
                    foreach (AirportPair ap in pairs)
                    {
                        var p = shortest.Where(a => a.IsSameRoute(ap)).SingleOrDefault();
                        if (p == null)
                        {
                            shortest.Add(ap);
                        }
                        else if (ap.Distance < p.Distance)
                        {
                            shortest[shortest.FindIndex(a => a.IsSameRoute(p))] = ap;
                        }
                    }

                    if (shortest.Count == 0)
                    {
                        return(Ok("Can't build pair of cities"));
                    }
                    else
                    {
                        return(Ok(shortest));
                    }
                }
            } catch (Exception ex) {
                return(StatusCode(500, ex.Message));
            } finally {
                s.Dispose();
            }
        }
Пример #2
0
        public async Task CalculateDistance_SameLocations_ReturnsZero()
        {
            //arrange
            Mock <IHttpService> httpServiceMock = new Mock <IHttpService>();
            var             settingsMock        = new Mock <ITeleportServicesSettings>();
            AirportService  airportService      = new AirportService(httpServiceMock.Object, settingsMock.Object);
            AirportResponse airportResponse     = new AirportResponse {
                Location = new Location {
                    Latitude = 1, Longitude = 1
                }
            };
            AirportResponse airportResponse2 = new AirportResponse {
                Location = new Location {
                    Latitude = 1, Longitude = 1
                }
            };

            //act
            double result = airportService.CalculateDistance(airportResponse, airportResponse2);

            //assert
            Assert.Equal(0, result, 2);
        }