public async Task <IActionResult> PlannedTripEdit(int id, EditPlannedTripViewModel viewModel) { ModelState.Remove("Trip.User"); ModelState.Remove("Trip.UserId"); //ApplicationUser user = await GetCurrentUserAsync(); //viewModel.Trip.UserId = user.Id; if (ModelState.IsValid) { try { Trip trip = await _context.Trip .Include(t => t.TripTravelTypes) .Include(t => t.TripVisitLocations) .SingleOrDefaultAsync(t => t.TripId == id); //This checks if there are any joiner tables of this kind for this trip, //then it foreaches over the joiner table and delets each one from the db //this deletes all TripTravelTypes the joiner tables if (trip.TripTravelTypes.Count > 0) { foreach (TripTravelType travelType in trip.TripTravelTypes) { //this says for each one of the joiner tables put it in the _context bag to get deleted on _context.SaveChangesAsync _context.Remove(travelType); } } //this builds up TripTravelType tables for each TravelType thats selected //checks to see if there are selectedTravelTypeIds to loop over if (viewModel.SelectedTravelTypeIds != null) { //makes joiner table for TripTravelType foreach (int TypeId in viewModel.SelectedTravelTypeIds) { TripTravelType newTripTT = new TripTravelType() { //pulls tripid out of context bag TripId = trip.TripId, TravelTypeId = TypeId }; _context.Add(newTripTT); } } // This deletes all the TripVisitLocations joiner tables if (trip.TripVisitLocations.Count > 0) { foreach (TripVisitLocation location in trip.TripVisitLocations) { _context.Remove(location); } } //this builds up the TripVisitLocation for food and adds it to the db context if (viewModel.NewFoodLocations.Count > 0) { foreach (TripVisitLocation location in viewModel.NewFoodLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = trip.TripId, LocationTypeId = 1, Name = location.Name, Description = location.Description, IsCompleted = false }; _context.Add(newTripVL); } } //this builds up the TripVisitLocation for places and adds it to the db context if (viewModel.NewVisitLocations.Count > 0) { foreach (TripVisitLocation location in viewModel.NewVisitLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = trip.TripId, LocationTypeId = 2, Name = location.Name, Description = location.Description, IsCompleted = false }; _context.Add(newTripVL); } } trip.Location = viewModel.Trip.Location; trip.Accommodation = viewModel.Trip.Accommodation; trip.Budget = viewModel.Trip.Budget; trip.ContinentId = viewModel.Trip.ContinentId; trip.IsPreTrip = true; trip.Title = viewModel.Trip.Title; trip.TripDates = viewModel.Trip.TripDates; _context.Update(trip); await _context.SaveChangesAsync(); return(RedirectToAction("PlannedTripDetails", "Trips", new { id = trip.TripId })); } catch (DbUpdateConcurrencyException) { if (!TripExists(id)) { return(NotFound()); } else { throw; } } } return(View(viewModel)); }
public async Task <IActionResult> FinishedTripEdit(int id, EditFinishedTripViewModel viewModel) { //ModelState.Remove("Trip.User"); //ModelState.Remove("Trip.UserId"); List <TripVisitLocation> FoodLocations = new List <TripVisitLocation>(); List <TripVisitLocation> VisitLocations = new List <TripVisitLocation>(); for (var i = 0; i < viewModel.NewFoodLocations.Count; i++) { if (viewModel.NewFoodLocations[i].Name != null) { FoodLocations.Add(viewModel.NewFoodLocations[i]); } } for (var i = 0; i < viewModel.NewVisitLocations.Count; i++) { if (viewModel.NewVisitLocations[i].Name != null) { VisitLocations.Add(viewModel.NewVisitLocations[i]); } } viewModel.NewFoodLocations = FoodLocations; viewModel.NewVisitLocations = VisitLocations; Trip trip = await _context.Trip .Include(t => t.TripTravelTypes) .Include(t => t.TripVisitLocations) .Include(t => t.TripRetros) .SingleOrDefaultAsync(t => t.TripId == id); //This checks if there are any joiner tables of this kind for this trip, //then it foreaches over the joiner table and delets each one from the db //this deletes all TripTravelTypes the joiner tables if (trip.TripTravelTypes.Count > 0) { foreach (TripTravelType travelType in trip.TripTravelTypes) { //this says for each one of the joiner tables put it in the _context bag to get deleted on _context.SaveChangesAsync _context.Remove(travelType); } } //this builds up TripTravelType tables for each TravelType thats selected //checks to see if there are selectedTravelTypeIds to loop over if (viewModel.SelectedTravelTypeIds != null) { //makes joiner table for TripTravelType foreach (int TypeId in viewModel.SelectedTravelTypeIds) { TripTravelType newTripTT = new TripTravelType() { //pulls tripid out of context bag TripId = trip.TripId, TravelTypeId = TypeId }; _context.Add(newTripTT); } } // This deletes all the TripVisitLocations joiner tables if (trip.TripVisitLocations.Count > 0) { foreach (TripVisitLocation location in trip.TripVisitLocations) { _context.Remove(location); } } //this builds up the TripVisitLocation for food and adds it to the db context if (viewModel.NewVisitLocations.Count > 0) { foreach (TripVisitLocation location in viewModel.NewVisitLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = trip.TripId, LocationTypeId = 2, Name = location.Name, Description = location.Description, IsCompleted = location.IsCompleted }; _context.Add(newTripVL); } } //this builds up the TripVisitLocation for food and adds it to the db context if (viewModel.NewFoodLocations.Count > 0) { foreach (TripVisitLocation location in viewModel.NewFoodLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = trip.TripId, LocationTypeId = 1, Name = location.Name, Description = location.Description, IsCompleted = location.IsCompleted }; _context.Add(newTripVL); } } //this gets the DoAgain retro TripRetro doAgainTripRetro = await _context.TripRetro .Where(tr => tr.TripId == id && tr.RetroTypeId == 1).FirstOrDefaultAsync(); //This updates the DoAgain retro doAgainTripRetro.Description = viewModel.DoAgainRetro.Description; _context.Update(doAgainTripRetro); TripRetro doDifferentTripRetro = await _context.TripRetro .Where(tr => tr.TripId == id && tr.RetroTypeId == 2).FirstOrDefaultAsync(); doDifferentTripRetro.Description = viewModel.DoDifferentRetro.Description; _context.Update(doDifferentTripRetro); trip.Location = viewModel.Trip.Location; trip.Accommodation = viewModel.Trip.Accommodation; trip.Budget = viewModel.Trip.Budget; trip.ContinentId = viewModel.Trip.ContinentId; trip.IsPreTrip = false; trip.Title = viewModel.Trip.Title; trip.TripDates = viewModel.Trip.TripDates; trip.ImagePath = viewModel.Trip.ImagePath; _context.Update(trip); await _context.SaveChangesAsync(); return(RedirectToAction("Details", "Trips", new { id = trip.TripId })); }
public async Task <IActionResult> Create(CreateTripViewModel viewmodel) { ModelState.Remove("Trip.User"); ModelState.Remove("Trip.UserId"); ApplicationUser user = await GetCurrentUserAsync(); viewmodel.Trip.UserId = user.Id; viewmodel.Trip.IsPreTrip = true; if (ModelState.IsValid) { _context.Add(viewmodel.Trip); //checks to see if there are selectedTravelTypeIds to loop over if (viewmodel.SelectedTravelTypeIds != null) { //makes joiner table for TripTravelType foreach (int TypeId in viewmodel.SelectedTravelTypeIds) { TripTravelType newTripTT = new TripTravelType() { //pulls tripid out of context bag TripId = viewmodel.Trip.TripId, TravelTypeId = TypeId }; _context.Add(newTripTT); } } //this runs though all the inputed food places and makes a joiner table for it if (viewmodel.EnteredTripFoodLocations != null) { foreach (TripVisitLocation foodL in viewmodel.EnteredTripFoodLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = viewmodel.Trip.TripId, LocationTypeId = 1, Name = foodL.Name, Description = foodL.Description, IsCompleted = false }; _context.Add(newTripVL); } } //this runs though all the inputed food places and makes a joiner table for it if (viewmodel.EnteredTripVisitLocations != null) { foreach (TripVisitLocation placeL in viewmodel.EnteredTripVisitLocations) { TripVisitLocation newTripVL = new TripVisitLocation() { TripId = viewmodel.Trip.TripId, LocationTypeId = 2, Name = placeL.Name, Description = placeL.Description, IsCompleted = false }; _context.Add(newTripVL); } } await _context.SaveChangesAsync(); return(RedirectToAction("PlannedTrips", "Trips")); } return(View(viewmodel)); }