Beispiel #1
0
        public IActionResult GetCoursesForPagination(PaginationData data)
        {
            int page = data.RequestPage ?? 0;

            _pageSize = data.PageLimit ?? 4;
            var itemsToSkip =
                page == 0 ? 0 : (page - 1) * _pageSize;

            var container = new ApiContainer();


            var filter = new CourseFilter()
            {
                Category        = data.Category,
                UserSearchInput = data.UserSearchInput
            };
            var courses = _data.GetCourses(filter).OrderBy(t => t.Id).Skip(itemsToSkip).Take(_pageSize);

            if (!courses.Any())
            {
                return(NotFound());
            }

            var dataRez = new List <CourseDto>();
            var model   = courses.Select(c => _mapper.Map <Course, CourseDto>(c));

            foreach (var course in courses)
            {
                var dtoModel = _mapper.Map <Course, CourseDto>(course);
                dtoModel.StudentCount = course.Users.Count();
                dtoModel.UserRaiting  = 5;
                dtoModel.Author       =
                    string.IsNullOrEmpty(course.Author.Name) || string.IsNullOrEmpty(course.Author.Surname)
                        ? course.Author.UserName
                        : $"{course.Author.Name} {course.Author.Surname}";
                dataRez.Add(dtoModel);
            }
            container.Data        = dataRez;
            container.CurrentPage = page;
            container.TotalRecord = _data.GetCourses(filter).Count();
            container.PageLimit   = _pageSize;
            container.Fetched     = courses.Count();
            return(Ok(container));
        }