public IActionResult Index(SortState sortState = SortState.ShowNameAsc, int page = 1)
        {
            TimetablesFilterViewModel filter = HttpContext.Session.Get <TimetablesFilterViewModel>(filterKey);

            if (filter == null)
            {
                filter = new TimetablesFilterViewModel {
                    DayOfWeek = 0, Month = 0, Year = 0, StaffName = string.Empty, ShowName = string.Empty
                };
                HttpContext.Session.Set(filterKey, filter);
            }

            string modelKey = $"{typeof(Timetable).Name}-{page}-{sortState}-{filter.DayOfWeek}-{filter.Month}-{filter.Year}-{filter.StaffName}-{filter.ShowName}";

            if (!cache.TryGetValue(modelKey, out TimetablesViewModel model))
            {
                model = new TimetablesViewModel();

                IQueryable <Timetable> timetables = GetSortedEntities(sortState, filter);

                int count    = timetables.Count();
                int pageSize = 10;
                model.PageViewModel = new PageViewModel(page, count, pageSize);

                model.Entities                  = count == 0 ? new List <Timetable>() : timetables.Skip((model.PageViewModel.CurrentPage - 1) * pageSize).Take(pageSize).ToList();
                model.SortViewModel             = new SortViewModel(sortState);
                model.TimetablesFilterViewModel = filter;

                cache.Set(modelKey, model);
            }

            return(View(model));
        }
        public IActionResult Index(TimetablesViewModel filterModel, string showName)
        {
            TimetablesFilterViewModel filter;

            if (!string.IsNullOrEmpty(showName))
            {
                filter = new TimetablesFilterViewModel {
                    ShowName = showName
                };
                HttpContext.Session.Set(filterKey, filter);

                return(RedirectToAction("Index", new { page = 1 }));
            }

            filter = HttpContext.Session.Get <TimetablesFilterViewModel>(filterKey);
            if (filter != null)
            {
                filter.DayOfWeek = filterModel.TimetablesFilterViewModel.DayOfWeek;
                filter.Month     = filterModel.TimetablesFilterViewModel.Month;
                filter.Year      = filterModel.TimetablesFilterViewModel.Year;
                filter.StaffName = filterModel.TimetablesFilterViewModel.StaffName;
                filter.ShowName  = filterModel.TimetablesFilterViewModel.ShowName;

                HttpContext.Session.Remove(filterKey);
                HttpContext.Session.Set(filterKey, filter);
            }

            return(RedirectToAction("Index", new { page = 1 }));
        }
        private IQueryable <Timetable> GetSortedEntities(SortState sortState, TimetablesFilterViewModel filterModel)
        {
            IQueryable <Timetable> timetables = db.Timetables.Include(t => t.Show).Include(t => t.Staff).AsQueryable();

            switch (sortState)
            {
            case SortState.TimetableDayOfWeekAsc:
                timetables = timetables.OrderBy(t => t.DayOfWeek);
                break;

            case SortState.TimetableDayOfWeekDesc:
                timetables = timetables.OrderByDescending(t => t.DayOfWeek);
                break;

            case SortState.TimetableMonthAsc:
                timetables = timetables.OrderBy(t => t.Month);
                break;

            case SortState.TimetableMonthDesc:
                timetables = timetables.OrderByDescending(t => t.Month);
                break;

            case SortState.TimetableYearAsc:
                timetables = timetables.OrderBy(t => t.Year);
                break;

            case SortState.TimetableYearDesc:
                timetables = timetables.OrderByDescending(t => t.Year);
                break;

            case SortState.ShowNameAsc:
                timetables = timetables.OrderBy(t => t.Show.Name);
                break;

            case SortState.ShowNameDesc:
                timetables = timetables.OrderByDescending(t => t.Show.Name);
                break;

            case SortState.TimetableStartTimeAsc:
                timetables = timetables.OrderBy(t => t.StartTime);
                break;

            case SortState.TimetableStartTimeDesc:
                timetables = timetables.OrderByDescending(t => t.StartTime);
                break;

            case SortState.TimetablEndTimeAsc:
                timetables = timetables.OrderBy(t => t.EndTime);
                break;

            case SortState.TimetablEndTimeDesc:
                timetables = timetables.OrderByDescending(t => t.EndTime);
                break;

            case SortState.StaffFullNameAsc:
                timetables = timetables.OrderBy(t => t.Staff.FullName);
                break;

            case SortState.StaffFullNameDesc:
                timetables = timetables.OrderByDescending(t => t.Staff.FullName);
                break;
            }

            if (filterModel.DayOfWeek != 0)
            {
                timetables = timetables.Where(t => t.DayOfWeek == filterModel.DayOfWeek).AsQueryable();
            }
            if (filterModel.Month != 0)
            {
                timetables = timetables.Where(t => t.Month == filterModel.Month).AsQueryable();
            }
            if (filterModel.Year != 0)
            {
                timetables = timetables.Where(t => t.Year == filterModel.Year).AsQueryable();
            }
            if (!string.IsNullOrEmpty(filterModel.StaffName))
            {
                timetables = timetables.Where(t => t.Staff.FullName == filterModel.StaffName).AsQueryable();
            }
            if (!string.IsNullOrEmpty(filterModel.ShowName))
            {
                timetables = timetables.Where(t => t.Show.Name == filterModel.ShowName).AsQueryable();
            }

            return(timetables);
        }