public async Task <IActionResult> ListByRegistration(string registration)
        {
            List <Aircraft> viewModel = null;
            Aircraft        aircraft  = await _aircraft.GetAircraftByRegistrationAsync(registration);

            if (aircraft != null)
            {
                viewModel = new List <Aircraft> {
                    aircraft
                };
            }
            return(PartialView("List", viewModel));
        }
        public async Task <IActionResult> Index(SightingSearchByAircraftViewModel model)
        {
            if (ModelState.IsValid)
            {
                int page = model.PageNumber;
                switch (model.Action)
                {
                case ControllerActions.ActionPreviousPage:
                    page -= 1;
                    break;

                case ControllerActions.ActionNextPage:
                    page += 1;
                    break;

                case ControllerActions.ActionSearch:
                    page = 1;
                    break;

                default:
                    break;
                }

                // Need to clear model state here or the page number that was posted
                // is returned and page navigation doesn't work correctly. So, capture
                // and amend the page number, above, then apply it, below
                ModelState.Clear();

                List <Sighting> sightings = null;

                try
                {
                    // Retrieve the aircraft with the specified registration number
                    // then, if we have a valid aircraft, retrieve its sightings
                    Aircraft aircraft = await _aircraft.GetAircraftByRegistrationAsync(model.Registration);

                    if (aircraft != null)
                    {
                        sightings = await _client.GetSightingsByAircraft(aircraft.Id, page, _settings.Value.SearchPageSize);
                    }
                }
                catch
                {
                }

                // Expose the sightings to the View
                model.SetSightings(sightings, page, _settings.Value.SearchPageSize);
            }

            return(View(model));
        }
Beispiel #3
0
        /// <summary>
        /// Retrieve or constuct the flight details model
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public async Task <AircraftDetailsViewModel> GetAircraftDetailsModelAsync(string userName)
        {
            // Retrieve the model from the cache
            string key = GetCacheKey(AircraftDetailsKeyPrefix, userName);
            AircraftDetailsViewModel model = _cache.Get <AircraftDetailsViewModel>(key);

            if (model == null)
            {
                // Not cached, so create a new one, using the cached sighting details
                // model to supply the aircraft registration
                key = GetCacheKey(SightingDetailsKeyPrefix, userName);
                SightingDetailsViewModel sighting = _cache.Get <SightingDetailsViewModel>(key);
                model = new AircraftDetailsViewModel {
                    Registration = sighting.Registration
                };
            }

            // Set the list of available manufacturers
            List <Manufacturer> manufacturers = await GetManufacturersAsync();

            model.SetManufacturers(manufacturers);

            // See if this is an existing aircraft
            Aircraft aircraft = await _aircraft.GetAircraftByRegistrationAsync(model.Registration);

            if (aircraft != null)
            {
                // It it, so assign the aircraft properties
                model.AircraftId     = aircraft.Id;
                model.SerialNumber   = aircraft.SerialNumber;
                model.ManufacturerId = aircraft.Model.ManufacturerId;
                model.ModelId        = aircraft.ModelId;
                model.Age            = DateTime.Now.Year - aircraft.Manufactured;

                // Load the models for the aircraft's manufacturer
                List <Model> models = await GetModelsAsync(model.ManufacturerId ?? 0);

                model.SetModels(models);
            }

            return(model);
        }