Ejemplo n.º 1
0
        public ActionResult Index()
        {
            var trip_list  = dbcontext.Trips.ToList();
            var model_list = new List <ViewEditTripsViewModel>();

            // get trip list with closest departure date. Doesnt actually work? When I try trip_list.OrderByDescending
            // it returns the same list as when done below. Dunno why. Solution is to take objects from last in list
            trip_list.OrderBy(item => item.DateDeparture);

            int count = trip_list.Count() - 1; // eg 12 because trip list index starts from 0 not 1 in below for()
            int to    = count - 6;             // 12 - 6 = 6

            //make sure there are trips in db
            if (count >= 0)                      // there is at least 1 trip in db
            {
                for (int i = count; i > to; i--) // im ordering from last because .OrderBy/.OrderByDescending returns the same list for some reason
                {
                    // check if there are less than 6 trips in list
                    //if (trip_list.Count < i - 1) break; This would be correct if we ordered from first, not last
                    // this would give a index out of bounds error if there are eg 4 trips (in 5th step we would be adding trip[-1]
                    // we need to check if next index will be out of bounds (if there are no more trips in db)
                    // 0 because we check if there are no trips left
                    if (0 > i + 1)
                    {
                        break;
                    }
                    model_list.Add(new ViewEditTripsViewModel {
                        TripInstance = trip_list[i]
                    });
                }
            }


            // model to return
            TripsViewModels model = new TripsViewModels();

            model.List = model_list;

            return(View(model));
        }
Ejemplo n.º 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));
        }