Пример #1
0
        public async Task <IActionResult> Get()
        {
            var pagination = Request.Headers["Pagination"];

            if (!string.IsNullOrEmpty(pagination))
            {
                string[] vals = pagination.ToString().Split(',');
                int.TryParse(vals[0], out page);
                int.TryParse(vals[1], out pageSize);
            }

            int currentPage     = page;
            int currentPageSize = pageSize;
            var totalCourses    = await _coursesRepository.CountAsync();

            var totalPages = (int)Math.Ceiling((double)totalCourses / pageSize);

            IEnumerable <Course> courses = _coursesRepository
                                           .AllIncluding(c => c.Department)
                                           .OrderBy(c => c.CourseID)
                                           .Skip((currentPage - 1) * currentPageSize)
                                           .Take(currentPageSize)
                                           .ToList();

            Response.AddPagination(page, pageSize, totalCourses, totalPages);

            IEnumerable <CourseViewModel> coursesVM = Mapper.Map <IEnumerable <Course>, IEnumerable <CourseViewModel> >(courses);

            return(new OkObjectResult(coursesVM));
        }