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> FinishTrip(int id, FinishTripViewModel viewModel) { ModelState.Remove("Trip.User"); ModelState.Remove("Trip.UserId"); ModelState.Remove("Trip.TripDates"); ModelState.Remove("Trip.Location"); ModelState.Remove("Trip.Accommodation"); ModelState.Remove("Trip.Title"); ModelState.Remove("Trip.ContinentId"); if (!ModelState.IsValid) { return(View(viewModel)); } Trip trip = await _context.Trip .Include(t => t.TripTravelTypes) .Include(t => t.TripVisitLocations) .FirstOrDefaultAsync(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) { // wipe away previous trip travel types _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() { TripId = trip.TripId, TravelTypeId = TypeId }; _context.Add(newTripTT); } } if (viewModel.SelectedLocationIds != null) { foreach (var tvl in trip.TripVisitLocations) { //this checks the selcted foodLocIds again the list of Locs to see which ones were selected with the checkboxed so it can find the ones in needs to update the status of if (viewModel.SelectedLocationIds.Any(item => item == tvl.TripVisitLocationId)) { tvl.IsCompleted = true; _context.Update(tvl); } } } if (viewModel.NewFoods != null) { foreach (TripVisitLocation foodVL in viewModel.NewFoods) { foodVL.LocationTypeId = 1; foodVL.TripId = trip.TripId; _context.Add(foodVL); } } if (viewModel.NewPlaces != null) { foreach (TripVisitLocation placeVL in viewModel.NewPlaces) { placeVL.LocationTypeId = 2; placeVL.TripId = trip.TripId; _context.Add(placeVL); } } TripRetro DoAgainRetro = new TripRetro(); DoAgainRetro.TripId = id; DoAgainRetro.RetroTypeId = 1; DoAgainRetro.Description = viewModel.DoAgain.Description; _context.Add(DoAgainRetro); TripRetro DoDifferent = new TripRetro(); DoDifferent.TripId = id; DoDifferent.RetroTypeId = 2; DoDifferent.Description = viewModel.DoDifferent.Description; _context.Add(DoDifferent); trip.IsPreTrip = false; trip.ImagePath = viewModel.Trip.ImagePath; trip.DateFinished = DateTime.Now; _context.Update(trip); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Trips")); }