public async Task <IActionResult> Edit(int id)
        {
            Aircraft aircraft = await _aircraft.GetAircraftByIdAsync(id);

            EditAircraftViewModel viewModel     = _mapper.Map <EditAircraftViewModel>(aircraft);
            List <Manufacturer>   manufacturers = await _manufacturers.GetManufacturersAsync();

            viewModel.SetManufacturers(manufacturers);
            return(View(viewModel));
        }
Example #2
0
        /// <summary>
        /// Either retrieve an existing aircraft or create a new one
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        private async Task <Aircraft> RetrieveOrCreateAircraft(string userName)
        {
            Aircraft aircraft = null;

            // Retrieve the aircraft details from the cache
            string key = GetCacheKey(AircraftDetailsKeyPrefix, userName);
            AircraftDetailsViewModel details = _cache.Get <AircraftDetailsViewModel>(key);

            if (details != null)
            {
                // If this is a sighting for an existing aircraft, just return it.
                // Otherwise, we need to create a new aircraft
                if (details.AircraftId > 0)
                {
                    aircraft = await _aircraft.GetAircraftByIdAsync(details.AircraftId ?? 0);
                }
                else
                {
                    if ((details.ManufacturerId ?? 0) == 0)
                    {
                        // With no manufacturer selected, we're creating a new manufacturer and model
                        Manufacturer manufacturer = await _manufacturers.AddManufacturerAsync(details.NewManufacturer);

                        Model model = await _models.AddModelAsync(details.NewModel, manufacturer.Id);

                        details.ModelId = model.Id;
                    }
                    else if ((details.ModelId ?? 0) == 0)
                    {
                        // With no model selected, we're creating a new model for the selected manufacturer
                        Model model = await _models.AddModelAsync(details.NewModel, details.ManufacturerId ?? 0);

                        details.ModelId = model.Id;
                    }

                    // Create the aircraft
                    int manufactured = DateTime.Now.Year - (details.Age ?? 0);
                    aircraft = await _aircraft.AddAircraftAsync(details.Registration, details.SerialNumber, manufactured, details.ModelId ?? 0);
                }
            }

            return(aircraft);
        }