示例#1
0
        public async Task <IActionResult> GetAirportDistance(string firstAirport, string secondAirport)
        {
            if (string.IsNullOrEmpty(firstAirport) || string.IsNullOrWhiteSpace(secondAirport))
            {
                return(BadRequest("You need to specify both airport IATA codes in order to calculate the distance."));
            }

            var airportDistanceDto = new AirportDistanceDto
            {
                FirstAirport  = firstAirport,
                SecondAirport = secondAirport
            };

            return(Ok(await _aiportServiceAdapter.GetAiportDistanceByCode(airportDistanceDto)));
        }
示例#2
0
        public async Task <AirportResource> GetAiportDistanceByCode(AirportDistanceDto airportsInfo)
        {
            // Get Icao code in order to calculate distance between two airports
            var firstAirportIcaoCode = await GetAirportIcaoCode(airportsInfo.FirstAirport);

            var secondAirportIcaoCode = await GetAirportIcaoCode(airportsInfo.SecondAirport);

            // Calculate route and get distance in kms
            _airportService.route = $"{firstAirportIcaoCode.Icao_Code}-{ secondAirportIcaoCode.Icao_Code}";

            var distanceInKms = ((await _airportService.RouteAsync()).Totals.Distance_Km);

            // Convert distance in kms to miles
            var distanceInMiles = ConvertKilometersToMiles(distanceInKms);

            return(new AirportResource
            {
                DistanceInMiles = distanceInMiles,
                FirstAirportResource = _mapper.Map <AirportDto, AirportDetailsResource>(firstAirportIcaoCode),
                SecondAirportResource = _mapper.Map <AirportDto, AirportDetailsResource>(secondAirportIcaoCode)
            });
        }