public void InsertOrUpdate(Rotation rotation) { if (rotation.ID == default(int)) { // New entity context.Rotation.Add(rotation); } else { // Existing entity context.Rotation.Attach(rotation); context.Entry(rotation).State = EntityState.Modified; } }
public ActionResult Create(Rotation rotation) { if (ModelState.IsValid) { rotationRepository.InsertOrUpdate(rotation); rotationRepository.Save(); return RedirectToAction("Index"); } else { ViewBag.PossibleUser = userRepository.All; return View(); } }
public ActionResult Book(FlightsBook viewModel) { Connection conn = connectionRepository.Find(viewModel.Connection.ID); Flight bookedFlight = new Flight(); bookedFlight.AircraftTypeICAO = viewModel.AircraftTypeICAO; bookedFlight.AirlineICAO = conn.AirlineICAO; bookedFlight.BookedOn = DateTime.UtcNow; bookedFlight.DepartureAirportICAO = conn.DepartureAirportICAO; bookedFlight.DestinationAirportICAO = conn.DestinationAirportICAO; // TODO maybe change to prefix and id seperated bookedFlight.FlightNumber = conn.Name; bookedFlight.UserID = UserContext.GetCurrent().ID; flightRepository.InsertOrUpdate(bookedFlight); flightRepository.Save(); if (viewModel.Option == 1 || viewModel.Option == 2) { Rotation rotation; if(viewModel.Option == 1) { rotation = new Rotation(); rotation.Name = viewModel.NewRotationName; rotation.Flights = new List<Flight>(); rotation.UserID = UserContext.GetCurrent().ID; } else { rotation = rotationRepository.AllIncluding(m => m.Flights).Single(t => t.ID == viewModel.RotationID.Value); } rotation.Flights.Add(bookedFlight); rotationRepository.InsertOrUpdate(rotation); rotationRepository.Save(); } return RedirectToAction("BookingSaved", new { id = conn.ID }); }