public ActionResult Save(FlightPathFormViewModel flightPathFormViewModel) { bool exists = flightPathRepository.GetFlightPathByUniqueIndex(flightPathFormViewModel.DestinationId, flightPathFormViewModel.DepartureId) != null; if (exists && flightPathFormViewModel.FlightPathId == 0) //It applies only for New FlightPath not Edit { var containerViewModel = InitializeNewContainerFormViewModel(flightPathFormViewModel); ModelState.AddModelError("FlightPath.DestinationId", "This Destination is already part of this flight path"); return(View("FlightPathForm", containerViewModel)); } if (!ModelState.IsValid) { var containerViewModel = InitializeNewContainerFormViewModel(flightPathFormViewModel); return(View("FlightPathForm", containerViewModel)); } if (flightPathFormViewModel.FlightPathId == 0) { var planets = planetRepository.GetPlanetsByIdRaw(flightPathFormViewModel.DepartureId, flightPathFormViewModel.DestinationId); if (planets.Count != 2) { return(HttpNotFound()); } var flightPath = new FlightPath(planets.Single(p => p.PlanetID == flightPathFormViewModel.DepartureId), planets.Single(p => p.PlanetID == flightPathFormViewModel.DestinationId)); flightPathRepository.Add(flightPath); } unitOfWork.Complete(); return(RedirectToAction("Index", "FlightPath")); }
private ContainerFlightPathFormViewModel InitializeNewContainerFormViewModel(FlightPathFormViewModel viewModel = null) { var allPlanets = planetRepository.GetPlanetsRaw(); var availableDepartures = new List <LightPlanetViewModel>(); allPlanets.ForEach(p => availableDepartures.Add(LightPlanetViewModel.CreateFromModel(p))); var flightPathFormViewModel = viewModel ?? new FlightPathFormViewModel(); return(new ViewModels.FlightPathViewModels.ContainerFlightPathFormViewModel(flightPathFormViewModel, availableDepartures)); }