/// <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(AddFlightViewModel model) { if (ModelState.IsValid) { string number = model.FlightNumber; await _flights.AddFlightAsync(number, model.Embarkation, model.Destination, model.AirlineId); ModelState.Clear(); model.Clear(); model.Message = $"Flight '{number}' added successfully"; } List <Airline> airlines = await _airlines.GetAirlinesAsync(); model.SetAirlines(airlines); return(View(model)); }
public async Task <IActionResult> Add(AddFlightViewModel model) { IActionResult result; if (ModelState.IsValid) { DateTime start = CombineDateAndTime(model.StartDate, model.StartTime); DateTime end = CombineDateAndTime(model.EndDate, model.EndTime); Flight flight = await _flights.AddFlightAsync( model.DroneId, model.LocationId, model.OperatorId, start, end); // Redirect to the flight details/properties page result = RedirectToAction("Index", "FlightDetails", new { id = flight.Id }); } else { // If the model state isn't valid, load the lists of drones, // locations and operators and redisplay the flight logging page List <Drone> drones = await _drones.GetDronesAsync(); List <Location> locations = await _locations.GetLocationsAsync(); List <Operator> operators = await _operators.GetOperatorsAsync(); model.SetDrones(drones); model.SetLocations(locations); model.SetOperators(operators); result = View(model); } return(result); }