Exemple #1
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);
        }
Exemple #2
0
        public async Task <IActionResult> Add(AddModelViewModel model)
        {
            if (ModelState.IsValid)
            {
                Model aircraftModel = await _models.AddModelAsync(model.Name, model.ManufacturerId);

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

            List <Manufacturer> manufacturers = await _manufacturers.GetManufacturersAsync();

            model.SetManufacturers(manufacturers);

            return(View(model));
        }