コード例 #1
0
        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"));
        }
コード例 #2
0
        // ------------------------------------------------------------------------END OF CREATE NEW TRIP

        //--------------------------------------------------------------------------START FINISH TRIP CREATE

        //public async Task<FinishTripViewModel> FinishTripView(int id)
        //{
        //    Trip trip = await _context.Trip
        //           .Include(t => t.Continent)
        //           .Include(t => t.TripTravelTypes)
        //           .Include(t => t.TripVisitLocations)
        //           .FirstOrDefaultAsync(t => t.TripId == id);

        //    List<TravelType> travelTypes = await _context.TripTravelType
        //        .Include(t => t.TravelType)
        //        .Where(t => t.TripId == trip.TripId)
        //        .Select(t => t.TravelType)
        //        .ToListAsync();

        //    FinishTripViewModel viewmodel = new FinishTripViewModel
        //    {
        //        Trip = trip,
        //        TravelTypes = travelTypes
        //    };


        //    //this builds up the foodlocations in checkbox form so the user can select which ones they made it too
        //    viewmodel.FoodLocations = trip.TripVisitLocations.Where(tvl => tvl.LocationTypeId == 1)
        //        //.AsEnumerable()
        //        .Select(li => new SelectListItem
        //        {
        //            Text = li.Name,
        //            Value = li.TripVisitLocationId.ToString()
        //        }).ToList();
        //    ;

        //    //this builds up the foodlocations in checkbox form so the user can select which ones they made it too
        //    viewmodel.PlaceLocations = trip.TripVisitLocations.Where(tvl => tvl.LocationTypeId == 2)
        //        .AsEnumerable()
        //        .Select(li => new SelectListItem
        //        {
        //            Text = li.Name,
        //            Value = li.TripVisitLocationId.ToString()
        //        }).ToList();
        //    ;

        //    ViewData["scripts"] = new List<string>() {
        //        "FinishTrip"
        //    };

        //    return viewmodel;
        //}

        public async Task <IActionResult> FinishTrip(int id)
        {
            Trip trip = await _context.Trip
                        .Include(t => t.Continent)
                        .Include(t => t.TripTravelTypes)
                        .ThenInclude(tt => tt.TravelType)
                        .Include(t => t.TripVisitLocations)
                        .FirstOrDefaultAsync(t => t.TripId == id);

            //List<TravelType> travelTypes = await _context.TripTravelType
            //    .Include(t => t.TravelType)
            //    .Where(t => t.TripId == trip.TripId)
            //    .Select(t => t.TravelType)
            //    .ToListAsync();


            FinishTripViewModel viewmodel = new FinishTripViewModel
            {
                Trip = trip,
                //TravelTypes = travelTypes,
                AllLocations = trip.TripVisitLocations.ToList(),
            };


            //this builds up the foodlocations in checkbox form so the user can select which ones they made it too
            viewmodel.FoodLocations = trip.TripVisitLocations.Where(tvl => tvl.LocationTypeId == 1)
                                      //.AsEnumerable()
                                      .Select(li => new SelectListItem
            {
                Text     = li.Name,
                Value    = li.TripVisitLocationId.ToString(),
                Selected = false
            }).ToList();
            ;

            //this builds up the foodlocations in checkbox form so the user can select which ones they made it too
            viewmodel.PlaceLocations = trip.TripVisitLocations.Where(tvl => tvl.LocationTypeId == 2)
                                       .AsEnumerable()
                                       .Select(li => new SelectListItem
            {
                Text     = li.Name,
                Value    = li.TripVisitLocationId.ToString(),
                Selected = false
            }).ToList();
            ;

            List <TravelType> AllTravelTypes = await _context.TravelType.ToListAsync();


            List <TravelType> PrevSelectedTravelTypes = trip.TripTravelTypes.Select(ttt => ttt.TravelType).ToList();

            viewmodel.TravelTypes = PrevSelectedTravelTypes;

            //makes an empty list to hold selectListItems
            List <SelectListItem> DisplayTripTravelTypes = new List <SelectListItem>();


            foreach (TravelType TravelType in AllTravelTypes)
            {
                bool newList = PrevSelectedTravelTypes.Any(item => item.TravelTypeId == TravelType.TravelTypeId);

                DisplayTripTravelTypes.Add(new SelectListItem
                {
                    Text     = TravelType.Type,
                    Value    = TravelType.TravelTypeId.ToString(),
                    Selected = newList
                });
            }

            viewmodel.AllTravelTypes = DisplayTripTravelTypes;


            ViewData["scripts"] = new List <string>()
            {
                "FinishTrip"
            };

            return(View(viewmodel));
        }