Esempio n. 1
0
        public IActionResult All(int page)
        {
            if (page <= 0)
            {
                return(Redirect("/Home/Index"));
            }

            int reservationsCount = trainService.GetCount();

            var lastPage = reservationsCount / GlobalConstants.TrainsPerPage + 1;

            if (reservationsCount % GlobalConstants.TrainsPerPage == 0 && lastPage > 1)
            {
                lastPage--;
            }

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

            var trains = trainService.GetAll(page);

            var viewModel = new ListingPageViewModel
            {
                CurrentPage      = page,
                TotalTrainsCount = trains.Count,
                LastPage         = lastPage,
                Trains           = new List <TrainViewModel>()
            };

            foreach (var train in trains)
            {
                viewModel.Trains.Add(new TrainViewModel()
                {
                    Id       = train.Id,
                    Name     = train.Name,
                    Capacity = train.Capacity,
                    Fast     = train.Fast
                });
            }

            return(View(viewModel));
        }