コード例 #1
0
        // GET: TripLocation
        public ActionResult Index(int?thisTripId, ManageMessageId?message)
        {
            if (thisTripId == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            ViewBag.StatusMessage =
                message == ManageMessageId.EditDetailsSuccess ? "All changes have been saved."
            : message == ManageMessageId.AddEntrySuccess ? "Successfully added a new location to trip."
            : message == ManageMessageId.DeleteEntrySuccess ? "Successfully deleted a location from trip."
            : message == ManageMessageId.Error ? "An error has occured."
            : "";


            TripLocationsViewModels model = new TripLocationsViewModels();

            model.Id_Trip  = (int)thisTripId;
            model.TripName = dbcontext.Trips.Find((int)thisTripId).Name;

            var list = new List <TripLocationsInstanceViewModels>();

            // populate above list with all sublocations the current trip has assigned to it. Like in Trip/Index action we
            //need to check both ways if trip-location is assigned to each other
            foreach (var item in dbcontext.Trip_Locations.ToList())
            {
                if (item.Id_Trip == thisTripId)
                {
                    foreach (var location in dbcontext.Locations.ToList())
                    {
                        if (item.Id_Location == location.Id)
                        {
                            list.Add(new TripLocationsInstanceViewModels
                            {
                                Country         = location.Country,
                                Town            = location.Town,
                                Name            = location.Name,
                                Description     = location.Description,
                                LocationImage   = location.LocationImage,
                                Number          = item.Number,
                                RouteInstanceId = item.Id
                            });
                        }
                    }
                }
            }

            model.ListElement = list;
            return(View(model));
        }
コード例 #2
0
        // GET: Trip
        public ActionResult Index(float?PriceMax, float?PriceMin, ManageMessageId?message, int?page)
        {
            ViewBag.StatusMessage =
                message == ManageMessageId.EditDetailsSuccess ? "All changes have been saved."
            : message == ManageMessageId.CreateEntrySuccess ? "Successfully added a new trip."
            : message == ManageMessageId.DeleteEntrySuccess ? "Successfully deleted a trip."
            : message == ManageMessageId.BookEntrySuccess ? "Successfully booked a trip."
            : message == ManageMessageId.Error ? "An error has occurred."
            : message == ManageMessageId.CannotEditEntry ? "There are reservations made for this trip. Cannot edit."
            : "";
            // THIS ACTION WAS ANALYZED IN DETAIL in provided documentation (chapter 4)

            // tutorial used: https://github.com/TroyGoode/PagedList
            var pageIndex = (page ?? 1); // first page index. Must start at least at 1
            var pageSize  = 8;           // objects per page


            //setting default values of fields for search. Those options will be used to search by if empty
            TripsViewModels model = new TripsViewModels();

            if (PriceMax == null)
            {
                PriceMax = 999999;
            }
            if (PriceMin == null)
            {
                PriceMin = 0;
            }

            // variables for creating lists of returned trips
            var trip_list  = new List <Trip>();
            var model_list = new List <ViewEditTripsViewModel>();

            //****** takes trip info
            foreach (var trip in dbcontext.Trips.ToList())
            {
                trip_list.Add(trip);
            }
            //now we have a list of all trips now we add subplaces to the returned view

            foreach (var trip in trip_list)
            {
                if (trip.Price < PriceMax && trip.Price > PriceMin)
                {
                    var list = new List <TripLocationsInstanceViewModels>();
                    foreach (var item in dbcontext.Trip_Locations.ToList())
                    {
                        if (item.Id_Trip == trip.Id) // first check if [Trip] table and [TripLocation] table are connected via same ID. Below same for location
                        {
                            foreach (var location in dbcontext.Locations.ToList())
                            {
                                if (item.Id_Location == location.Id) // check the table created after removing many-many table connection
                                {
                                    list.Add(new TripLocationsInstanceViewModels
                                    {
                                        Country         = location.Country,
                                        Town            = location.Town,
                                        Name            = location.Name,
                                        Description     = location.Description,
                                        LocationImage   = location.LocationImage,
                                        Number          = item.Number,
                                        RouteInstanceId = item.Id
                                    });
                                }
                            }
                        }
                    }
                    var route = new TripLocationsViewModels();
                    if (list.Count() > 0)
                    {
                        route.ListElement = list;
                    }
                    route.Id_Trip  = trip.Id;
                    route.TripName = trip.Name;

                    model_list.Add(new ViewEditTripsViewModel {
                        TripInstance = trip, Route = route
                    });
                }
            }


            model.List = model_list;
            foreach (var trip in model.List)
            {
                //return free spots left FOR NOW STATIC VALUES. Bookings controller is not implemented
                trip.NumSpotsLeft = countFreeSpots(trip.TripInstance.Id);
                //return number of reservations
                trip.NumberOfReservations = countReservavtionsMade(trip.TripInstance.Id);
            }

            // add coach info to List
            foreach (var trip in model.List)
            {
                foreach (var item in dbcontext.Coaches.ToList())
                {
                    if (trip.TripInstance.CoachNumberId == item.Id)
                    {
                        trip.CoachBanner = item.VehScreenshot;
                        trip.CoachModel  = item.Brand + " " + item.VehModel + " - no." + item.VehicleNumber;
                    }
                }
            }


            // convert normal list to a pagedlist with given index and size
            model.List = model.List.ToPagedList(pageIndex, pageSize);

            return(View(model));
        }