/// <summary>
        ///     The entry point for the Home page UI.
        ///     <return>
        ///         The Task corresponding to the ViewResult for the Home page UI.
        ///     </return>
        ///     <remarks>
        ///         Asynchronous because it makes an HTTP Get request via an HttpClient instance to get the list of
        ///         valid airport names.
        ///     </remarks>
        /// </summary>
        public async Task <IActionResult> Index()
        {
            try
            {
                _logger.LogInformation("WebApp requesting Airport data from WebAPI");

                // Get valid airport names
                var airports = await GetAirports();

                _logger.LogInformation($"WebAPI responded with data for {airports.Count} airports.");

                // Initialize the Home page view model with the valid airports
                var viewModel = new HomeSearchViewModel
                {
                    Airports = airports
                };

                return(View(viewModel));
            }
            catch (HttpRequestException)    // This would indicate a failure such as internet loss
            {
                _logger.LogError("WebApp was unable to get airport data from WebAPI");

                // Indicate that the service is unavailable
                var viewModel = new HttpErrorPageViewModel(System.Net.HttpStatusCode.ServiceUnavailable);
                return(View("HttpErrorPage", viewModel));
            }
        }
        public async Task <IActionResult> Search(HomeSearchViewModel searchForm)
        {
            _logger.LogInformation($"Received form from the user:"******"Destination Airport: {searchForm.DestinationAirport}" + Environment.NewLine +
                                   $"Origin Airport: {searchForm.OriginAirport}" + Environment.NewLine +
                                   $"Return Date: {searchForm.ReturnDate}" + Environment.NewLine +
                                   $"Takeoff Date: {searchForm.TakeOffDate}" + Environment.NewLine +
                                   $"Round Trip: {searchForm.IsRoundTrip}");

            _logger.LogInformation("WebApp requesting Airport data from WebAPI");

            // Get the list of airports for validation
            var airports = await GetAirports();

            _logger.LogInformation($"WebAPI responded with data for {airports.Count} airports.");

            // Make sure the Origin Airport name is valid
            if (!airports.Select(a => a.AirportName).Contains(searchForm.OriginAirport))
            {
                ModelState.AddModelError("OriginAirport", "Invalid airport name");
            }

            // Make sure the Destination Airport name is valid
            if (!airports.Select(a => a.AirportName).Contains(searchForm.DestinationAirport))
            {
                ModelState.AddModelError("DestinationAirport", "Invalid airport name");
            }

            // Perform other validation. See the data annotations on the HomeSearchViewModel class for more.
            // If the validation fails, return ViewResult for the Home page with error information
            if (!ModelState.IsValid)
            {
                _logger.LogWarning("Form validation failed.");

                searchForm.Airports = airports;
                return(View("Index", searchForm));
            }

            // Send the appropriate HTTP Get request or return to the home page if the form is in an invalid state.
            if (!searchForm.IsRoundTrip)
            {
                _logger.LogInformation("WebApp querying WebAPI for one-way flight prices.");

                try
                {
                    return(await GetOneWayFlights(searchForm));
                }
                catch (FlightQuoteApiException ex)  // This indicates a failure on the WebAPI side
                {
                    var viewModel = new HttpErrorPageViewModel(ex.StatusCode);
                    return(View("HttpErrorPage", viewModel));
                }
                catch (HttpRequestException)       // This indicates a failure such as internet loss
                {
                    var viewModel = new HttpErrorPageViewModel(System.Net.HttpStatusCode.ServiceUnavailable);
                    return(View("HttpErrorPage", viewModel));
                }
            }

            _logger.LogInformation("WebApp querying WebAPI for round-trip flight prices.");

            try
            {
                // We've passed all validation and the only option remaining is roundtrip flights
                return(await GetRoundTripFlights(searchForm));
            }
            catch (FlightQuoteApiException ex)  // This indicates a failure on the WebAPI side
            {
                var viewModel = new HttpErrorPageViewModel(ex.StatusCode);
                return(View("HttpErrorPage", viewModel));
            }
            catch (HttpRequestException)       // This indicates a failure such as internet loss
            {
                var viewModel = new HttpErrorPageViewModel(System.Net.HttpStatusCode.ServiceUnavailable);
                return(View("HttpErrorPage", viewModel));
            }
        }