예제 #1
0
        public IActionResult GetAll(int page)
        {
            if (page <= 0)
            {
                return(Redirect("/Home/Index"));
            }

            int flightsCount = flightService.Count();

            var lastPage = flightsCount / 8 + 1; // 8 flights per page

            if (flightsCount % 8 == 0 && lastPage > 1)
            {
                lastPage--;
            }

            if (page > lastPage)
            {
                return(Redirect("/Home/Index"));
            }

            var flights = flightService.GetAll(page);

            var viewModel = new Models.ViewModels.Flight.ListingPageViewModel
            {
                CurrentPage = page,
                AllFlights  = flightsCount,
                LastPage    = lastPage,
                Flights     = new List <ListingViewModel>()
            };

            foreach (var flight in flights)
            {
                TimeSpan span = (flight.ArrivalTime - flight.TakeOffTime);

                var travelTime = String.Format("{0} days/{1} hours/{2} minutes",
                                               span.Days, span.Hours, span.Minutes, span.Seconds);

                viewModel.Flights.Add(new ListingViewModel()
                {
                    StartDestination = flight.StartDestination,
                    EndDestination   = flight.EndDestination,
                    TakeOffTime      = flight.TakeOffTime.ToString("dd/MM/yyyy hh:mm tt"),
                    Id                 = flight.FlightId,
                    TravelTime         = travelTime,
                    FreeSeatsPassanger = flight.FreeSeatsPassanger,
                    FreeSeatsBussiness = flight.FreeSeatsBussiness
                });
            }

            return(View(viewModel));
        }