public async Task <IActionResult> Index(AircraftDetailsViewModel model) { IActionResult result = null; if (ModelState.IsValid && (model.Action == ControllerActions.ActionNextPage)) { _wizard.CacheAircraftDetailsModel(model, User.Identity.Name); result = RedirectToAction("Index", "ConfirmDetails"); } else if (model.Action == ControllerActions.ActionPreviousPage) { _wizard.ClearCachedAircraftDetailsModel(User.Identity.Name); result = RedirectToAction("Index", "FlightDetails"); } else { // Set the list of available manufacturers List <Manufacturer> manufacturers = await _wizard.GetManufacturersAsync(); model.SetManufacturers(manufacturers); // Load the models for the aircraft's manufacturer if (model.ManufacturerId > 0) { List <Model> models = await _wizard.GetModelsAsync(model.ManufacturerId ?? 0); model.SetModels(models); } result = View(model); } return(result); }
public async Task <IActionResult> Models(int manufacturerId) { AircraftDetailsViewModel model = new AircraftDetailsViewModel(); List <Model> aircraftModels = await _wizard.GetModelsAsync(manufacturerId); model.SetModels(aircraftModels); return(PartialView(model)); }
/// <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); }
/// <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); }
/// <summary> /// GET: /Aircraft/Details/5 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Details(int id) { Aircraft aircraft = _dataService.GetAircraftById(id); if (aircraft == null) { throw new HttpException(404, "Aircraft not found"); } AircraftImageListViewModel imageModel = new AircraftImageListViewModel(); imageModel.ImageList = aircraft.Images; AircraftDetailsViewModel model = new AircraftDetailsViewModel() { Aircraft = aircraft, AircraftImages = imageModel }; return(View(model)); }
/// <summary> /// Cache the aircraft details view model /// </summary> /// <param name="userName"></param> /// <param name="model"></param> public void CacheAircraftDetailsModel(AircraftDetailsViewModel model, string userName) { string key = GetCacheKey(AircraftDetailsKeyPrefix, userName); _cache.Set <AircraftDetailsViewModel>(key, model, _settings.Value.CacheLifetimeSeconds); }
/// <summary> /// Construct the model to confirm sighting details /// </summary> /// <param name="userName"></param> /// <returns></returns> public async Task <ConfirmDetailsViewModel> GetConfirmDetailsModelAsync(string userName) { // Get the sighting, flight details and aircraft models and map them // into the confirm details model ConfirmDetailsViewModel model = new ConfirmDetailsViewModel(); string key = GetCacheKey(SightingDetailsKeyPrefix, userName); SightingDetailsViewModel sighting = _cache.Get <SightingDetailsViewModel>(key); _mapper.Map <SightingDetailsViewModel, ConfirmDetailsViewModel>(sighting, model); key = GetCacheKey(FlightDetailsKeyPrefix, userName); FlightDetailsViewModel flight = _cache.Get <FlightDetailsViewModel>(key); _mapper.Map <FlightDetailsViewModel, ConfirmDetailsViewModel>(flight, model); key = GetCacheKey(AircraftDetailsKeyPrefix, userName); AircraftDetailsViewModel aircraft = _cache.Get <AircraftDetailsViewModel>(key); _mapper.Map <AircraftDetailsViewModel, ConfirmDetailsViewModel>(aircraft, model); // For the location, if we have a new location specified then use that as the // location. Otherwise, look up the location by its ID if (sighting.LocationId == 0) { model.Location = sighting.NewLocation; } else { Location location = await _locations.GetLocationAsync(sighting.LocationId); model.Location = location.Name; } // Repeat the above logic for the airline if (flight.AirlineId == 0) { model.Airline = flight.NewAirline; } else { Airline airline = await _airlines.GetAirlineAsync(flight.AirlineId); model.Airline = airline.Name; } // Repeat the above logic for the manufacturer if ((aircraft.ManufacturerId ?? 0) == 0) { model.Manufacturer = aircraft.NewManufacturer; } else { int manufacturerId = aircraft.ManufacturerId ?? 0; Manufacturer manufacturer = await _manufacturers.GetManufacturerAsync(manufacturerId); model.Manufacturer = manufacturer.Name; } // Repeat the above logic for the model if ((aircraft.ModelId ?? 0) == 0) { model.Model = aircraft.NewModel; } else { int modelId = aircraft.ModelId ?? 0; Model aircraftModel = await _models.GetModelAsync(modelId); model.Model = aircraftModel.Name; } return(model); }
public async Task <IActionResult> Index() { AircraftDetailsViewModel model = await _wizard.GetAircraftDetailsModelAsync(User.Identity.Name); return(View(model)); }