示例#1
0
        public IActionResult Index(
            [Bind(nameof(MainViewData.Scope),
                  nameof(MainViewData.CourseToDisplay),
                  nameof(MainViewData.CourseType),
                  nameof(MainViewData.Order),
                  nameof(MainViewData.Scope))] MainViewData model, int page = 1)
        {
            var result = model;
            List <Announcement> announcements;

            if (model.Scope == Scope.Courses)
            {
                announcements = announcementService.GetAnnouncementsForUserCourses(userManager.GetUserId(User));
            }
            else
            {
                announcements = announcementService.GetGeneralAnnouncements();
            }

            result.UsersWithAnnouncements = announcementService.GetUsersWithAnnouncements();
            result.Courses = coursesService.GetUserAssignedCourses(userManager.GetUserId(User))
                             .Select(userCourse => userCourse.Course)
                             .Distinct()
                             .ToList();

            if (!string.IsNullOrEmpty(model.CourseToDisplay))
            {
                announcementService.FilterByCourseName(ref announcements, model.CourseToDisplay);
            }
            if (model.CourseType.HasValue)
            {
                announcementService.FilterByCourseType(ref announcements, model.CourseType.Value);
            }

            announcementService.SortAnnouncements(ref announcements, model.Order);

            result.Announcements = announcements;
            result.PagingInfo    = new PagingInfo
            {
                CurrentPage  = page,
                ItemsPerPage = pageSize,
                TotalItems   = result.Announcements.Count()
            };
            result.Announcements = result.Announcements.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return(View(result));
        }