예제 #1
0
 public Airport(AirportResponse airportResponse)
 {
     Name      = airportResponse.Name;
     Code      = airportResponse.Code;
     Latitude  = double.Parse(airportResponse.Latitude);
     Longitude = double.Parse(airportResponse.Longitude);
 }
예제 #2
0
        public async Task <AirportResponse> GetAllAirports(string iso = null)
        {
            AirportResponse airportResponse = new AirportResponse();
            IEnumerable <AirportData.Models.AirportDetails> airports;

            if (_cache.IsCacheExists(cacheKey))
            {
                airportResponse.SourceFrom = "from-Cache";
                airports = _cache.Get <IEnumerable <AirportData.Models.AirportDetails> >(cacheKey);
            }
            else
            {
                airportResponse.SourceFrom = "from-Database";
                airports = await _airportRepository.GetAsync();

                _cache.Put(cacheKey, airports, CacheExpiryTime.FiveMinutes);
            }


            if (string.IsNullOrEmpty(iso))
            {
                airportResponse.Airports = airports;
            }
            else
            {
                airportResponse.Airports = airports.Where(x => x.Iso == iso).ToList();
            }

            return(airportResponse);
        }
예제 #3
0
        public async Task GetAirport_ValidIATACode_ReturnsAirport()
        {
            //arrange
            Mock <IHttpService> httpServiceMock = new Mock <IHttpService>();
            var    settingsMock = new Mock <ITeleportServicesSettings>();
            string url          = "https://example/";

            var getRequestResult = new AirportResponse
            {
                Location = new Location
                {
                    Latitude  = 5,
                    Longitude = 5
                }
            };

            httpServiceMock.SetupSequence(x => x.GetRequestAsync <AirportResponse>(It.IsAny <string>())).ReturnsAsync <AirportResponse>(getRequestResult);
            settingsMock.SetupGet <string>(x => x.CTeleportAiportDataUrl).Returns(url);

            AirportService airportService = new AirportService(httpServiceMock.Object, settingsMock.Object);

            //act
            AirportResponse airportResponse = await airportService.GetAirport("AMS");

            //assert
            Assert.Same(airportResponse, getRequestResult);
        }
예제 #4
0
        public async Task <double> ExecuteAsync(GetAirportDistanceRequest input)
        {
            AirportResponse airport1 = await airportService.GetAirport(input.IATACode1);

            AirportResponse airport2 = await airportService.GetAirport(input.IATACode2);

            return(airportService.CalculateDistance(airport1, airport2));
        }
예제 #5
0
        public async Task GetLocation()
        {
            string          url             = "https://places-dev.cteleport.com/airports/";
            string          IATACode        = "GYD";
            AirportService  airportService  = new AirportService();
            AirportResponse airportResponse = await airportService.GetAirport(url, IATACode);

            Assert.Equal(50.05039D, airportResponse.Location.Longitude, 10);
        }
예제 #6
0
        public void Fill(AirportResponse airport1, AirportResponse airport2, double distanceInMeters)
        {
            ContainsData = true;
            Airports     = new[]
            {
                airport1,
                airport2
            };

            Distance = Math.Round(distanceInMeters / 1000);
        }
예제 #7
0
        public async Task GetRequestAsync_ValidUrl_ReturnsData()
        {
            //arrange
            HttpService httpService = new HttpService();
            string      url         = "https://places-dev.cteleport.com/airports/AMS";

            //act
            AirportResponse result = await httpService.GetRequestAsync <AirportResponse>(url);

            //assert
            Assert.NotNull(result);
        }
예제 #8
0
        /// <summary>
        /// Returns distance between airports in sea miles
        /// </summary>
        /// <param name="airport"></param>
        /// <param name="otherAirport"></param>
        /// <returns></returns>
        public double GetDistance(AirportResponse airport, AirportResponse otherAirport)
        {
            if (double.IsNaN(airport.Location.Latitude) || double.IsNaN(airport.Location.Longitude) || double.IsNaN(otherAirport.Location.Latitude) ||
                double.IsNaN(otherAirport.Location.Longitude))
            {
                throw new ArgumentException("Argument latitude or longitude is not a number");
            }

            var d1   = airport.Location.Latitude * (Math.PI / 180.0);
            var num1 = airport.Location.Longitude * (Math.PI / 180.0);
            var d2   = otherAirport.Location.Latitude * (Math.PI / 180.0);
            var num2 = otherAirport.Location.Longitude * (Math.PI / 180.0) - num1;
            var d3   = Math.Pow(Math.Sin((d2 - d1) / 2.0), 2.0) +
                       Math.Cos(d1) * Math.Cos(d2) * Math.Pow(Math.Sin(num2 / 2.0), 2.0);

            return(6376500.0 * (2.0 * Math.Atan2(Math.Sqrt(d3), Math.Sqrt(1.0 - d3))) / 1852);
        }
예제 #9
0
        /// <summary>
        /// Returns distance between airports in sea miles
        /// </summary>
        /// <param name="airport"></param>
        /// <param name="otherAirport"></param>
        /// <returns></returns>
        public double CalculateDistance(AirportResponse airport, AirportResponse otherAirport)
        {
            if (airport == null)
            {
                throw new ArgumentNullException(nameof(airport));
            }
            if (otherAirport == null)
            {
                throw new ArgumentNullException(nameof(otherAirport));
            }
            if (double.IsNaN(airport.Location.Latitude) || double.IsNaN(airport.Location.Longitude) || double.IsNaN(otherAirport.Location.Latitude) ||
                double.IsNaN(otherAirport.Location.Longitude))
            {
                throw new ArgumentException("Argument latitude or longitude is not a number");
            }

            GeoCoordinate geoCoordinate1 = new GeoCoordinate(airport.Location.Latitude, airport.Location.Longitude);
            GeoCoordinate geoCoordinate2 = new GeoCoordinate(otherAirport.Location.Latitude, otherAirport.Location.Longitude);

            double distanceInMeters = geoCoordinate1.GetDistanceTo(geoCoordinate2);

            return(distanceInMeters / 1609.34D); // 1852; sea mile
        }
예제 #10
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);
        }
예제 #11
0
        public IActionResult MeasureDistanceInMiles(string from, string to)
        {
            if (!Validation.IsAirportCodeValid(from))
            {
                return(this.BadRequest(AppConfig.NOT_VALID_AIRPORT_FROM));
            }

            if (!Validation.IsAirportCodeValid(to))
            {
                return(this.BadRequest(AppConfig.NOT_VALID_AIRPORT_TO));
            }

            var airportFrom = _airportsService.GetAirportLocation(from.ToUpper());

            if (airportFrom == null)
            {
                return(this.BadRequest(AppConfig.NOT_VALID_AIRPORT_FROM));
            }

            var airportTo = _airportsService.GetAirportLocation(to.ToUpper());

            if (airportTo == null)
            {
                return(this.BadRequest(AppConfig.NOT_VALID_AIRPORT_TO));
            }

            double miles = _airportsService.GetDistanceInMiles(airportFrom, airportTo);

            var response = new AirportResponse()
            {
                UnitValue = miles,
                UnitName  = AppConfig.MILES_UNIT
            };

            return(this.Ok(response));
        }