Example #1
0
        /// <summary>
        /// Either retrieve an existing flight or create a new one
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private async Task <Flight> RetrieveOrCreateFlight(string userName)
        {
            Flight flight = null;

            // Retrieve the flight details from the cache
            string key = GetCacheKey(FlightDetailsKeyPrefix, userName);
            FlightDetailsViewModel details = _cache.Get <FlightDetailsViewModel>(key);

            if (details != null)
            {
                // If this is a sighting for an existing flight, just return it.
                // Otherwise, we need to create a new flight
                if (details.FlightId > 0)
                {
                    flight = await _flights.GetFlightByIdAsync(details.FlightId);
                }
                else
                {
                    if (details.AirlineId == 0)
                    {
                        // If there's no airline selected, we're creating a new one
                        Airline airline = await _airlines.AddAirlineAsync(details.NewAirline);

                        details.AirlineId = airline.Id;
                    }

                    // Create the flight
                    flight = await _flights.AddFlightAsync(details.FlightNumber, details.Embarkation, details.Destination, details.AirlineId);
                }
            }

            return(flight);
        }
        public async Task <IActionResult> Add(AddAirlineViewModel model)
        {
            if (ModelState.IsValid)
            {
                Airline airline = await _client.AddAirlineAsync(model.Name);

                ModelState.Clear();
                model.Clear();
                model.Message = $"Airline '{airline.Name}' added successfully";
            }

            return(View(model));
        }