public async Task <IActionResult> Copy([Bind("TourID,Name,TourTypeID,MinDuration")] Tour tour, int?ETour)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tour);
                await _context.SaveChangesAsync(); //save tour here

                var e   = _context.Tours.Find(ETour);
                var lcs = _context.LocationSets.Where(e => e.TourID == e.TourID).ToList(); //find the target location sets
                foreach (var lc in lcs)                                                    //for each location in list
                {
                    var locationTour = new Location_Tour
                    {
                        LocationID = lc.Location.LocationID,
                        TourID     = tour.TourID
                    };
                    _context.Add(locationTour);
                    tour.Location_Tour.Add(locationTour);
                }

                tour.MinDuration = e.MinDuration;
                _context.Update(tour);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["TourTypeID"] = new SelectList(_context.TourTypes, "TourTypeID", "Label", tour.TourTypeID);
            return(View(tour));
        }
Example #2
0
        public void AddTours()
        {
            using var context = new TourContext(serviceProvider.GetRequiredService <DbContextOptions <TourContext> >());

            Console.WriteLine("Enter tour name: ");
            string name = Console.ReadLine();

            Console.WriteLine("How many locations you want to add into the tour:");
            int count = 0;

            try
            {
                count = Convert.ToInt32(Console.ReadLine());
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                count = Convert.ToInt32(Console.ReadLine());
            }

            List <Location> locations = new List <Location>();

            for (int i = 0; i < count; i++)
            {
                Console.WriteLine($"Add the {i + 1} location's Id");
                int id = Convert.ToInt32(Console.ReadLine());
                locations.Add(context.Locations.Find(id));
            }

            Tour tour = new Tour
            {
                Name       = name,
                TourTypeID = 1//seed item for console app
            };

            List <Location_Tour> lcs = new List <Location_Tour>();

            foreach (Location location in locations)
            {
                var Location_Tour = new Location_Tour
                {
                    TourID     = tour.TourID,
                    LocationID = location.LocationID
                };
                context.LocationSets.Add(Location_Tour);
                lcs.Add(Location_Tour);
            }
            tour.Location_Tour = lcs;
            tour.MinDuration   = tour.caculateMinDuration();

            context.Tours.Add(tour);
            context.SaveChanges();
        }
        public async Task <IActionResult> Create([Bind("Location_TourID,TourID,LocationID")] Location_Tour location_Tour)
        {
            if (ModelState.IsValid)
            {
                location_Tour.Location = _context.Locations.Find(location_Tour.LocationID);
                _context.Add(location_Tour);
                var tour = _context.Tours.Find(location_Tour.TourID);
                tour.Location_Tour.Add(location_Tour);
                tour.MinDuration += location_Tour.Location.MinTime;
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index", "Tours"));
            }
            ViewData["LocationID"] = new SelectList(_context.Locations, "LocationID", "Name", location_Tour.LocationID);
            ViewData["TourID"]     = new SelectList(_context.Tours, "TourID", "Name", location_Tour.TourID);
            return(View(location_Tour));
        }