public IActionResult AddNewFlight(FlightViewModel model) { if (!ModelState.IsValid) { return(View("Index", CreateDefaultFlightViewModel(model.NewFlightDepartureAirport, model.NewFlightArrivalAirport, model.NewFuelConsumptionLitersPerKm, model.NewFuelConsumptionTakeoffEffortInLiters))); } if (model.NewFlightDepartureAirport == model.NewFlightArrivalAirport) { ModelState.AddModelError("NewFlightArrivalAirport", "Departure and destination airports can't be the same !"); return(View("Index", CreateDefaultFlightViewModel(model.NewFlightDepartureAirport, model.NewFlightArrivalAirport, model.NewFuelConsumptionLitersPerKm, model.NewFuelConsumptionTakeoffEffortInLiters))); } try { _flightsManager.CreateNewFlight(model.NewFlightDepartureAirport, model.NewFlightArrivalAirport, model.NewFuelConsumptionLitersPerKm, model.NewFuelConsumptionTakeoffEffortInLiters); } catch (Exception) { ModelState.AddModelError("NewFuelConsumptionTakeoffEffortInLiters", "An error occured"); return(View("Index", CreateDefaultFlightViewModel(model.NewFlightDepartureAirport, model.NewFlightArrivalAirport, model.NewFuelConsumptionLitersPerKm, model.NewFuelConsumptionTakeoffEffortInLiters))); } return(RedirectToAction(nameof(Index))); }
public void WhenCreatingNewFlight_GivenFlightParameters_ShouldCallCreateFlightCorrectly() { _databaseMock.Setup(d => d.CreateNewFlight(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <double>(), It.IsAny <double>())).Verifiable(); _flightsManager.CreateNewFlight( _testFlight.Departure.InternalName, _testFlight.Destination.InternalName, _testFlight.AircraftFuelConsumptionLitersPerKm, _testFlight.AircraftFuelConsumptionTakeoffEffort); _databaseMock.Verify(d => d.CreateNewFlight( _testFlight.Departure.InternalName, _testFlight.Destination.InternalName, _testFlight.AircraftFuelConsumptionLitersPerKm, _testFlight.AircraftFuelConsumptionTakeoffEffort), Times.Once); }