public IHttpActionResult DeleteStarship(int?id) { if (!id.HasValue) { throw new HttpResponseException(HttpStatusCode.BadRequest); } var starshipDb = starshipRepository.GetSingleStarship(id ?? (int)InvalidPropertyValues.undefinedValue); if (starshipDb == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } var flights = flightRepository.GetFlightsByStarship(starshipDb.StarshipId); if (flights.Any()) { return(BadRequest("The Starshio is used in a Flight!\nTo delete it contact your Database Manager.")); } starshipRepository.Remove(starshipDb); unitOfWork.Complete(); return(Ok()); }
public ActionResult Edit(int?id) { if (!id.HasValue) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var starship = starshipRepository.GetSingleStarship(id ?? (int)InvalidPropertyValues.undefinedValue); if (starship == null) { return(HttpNotFound()); } var viewModel = new StarshipFormViewModel(starship); return(View("StarshipForm", viewModel)); }
public ActionResult Save(FlightFormViewModel flightFormViewModel) { if (!ModelState.IsValid) { var lightFlightPaths = InitializeFlightPaths(); var lightStarships = InitializeStarships(); var viewModel = new ContainerFlightFormViewModel(flightFormViewModel, lightFlightPaths, lightStarships); return(View("FlightForm", viewModel)); } Flight flightToDB; if (flightFormViewModel.FlightId == 0) { var starship = starshipRepository.GetSingleStarship(flightFormViewModel.StarshipId); flightToDB = new Flight(flightFormViewModel.GetDateTime(), flightFormViewModel.BasePrice, flightFormViewModel.FlightPathId, starship, flightFormViewModel.IsVIP); flightRepository.Add(flightToDB); } else { flightToDB = flightRepository.GetSingleFlight(flightFormViewModel.FlightId); if (flightToDB == null) { return(HttpNotFound()); } var starship = starshipRepository.GetSingleStarship(flightFormViewModel.StarshipId); flightToDB.Update(flightFormViewModel.GetDateTime(), flightFormViewModel.BasePrice, null, starship, null); } unitOfWork.Complete(); InformViewsForCreateOrEdit(flightToDB, flightFormViewModel.FlightId); return(RedirectToAction("Index", "Flight")); }