Esempio n. 1
0
        public IActionResult GetAllPost(StudentResourceParameters studentResourceParameters)
        {
            var listOfPost = _unitOfWork.StudentRepository.GetAllStudents(studentResourceParameters);
            var listOfDtos = Mapper.Map <List <StudentDTO> >(listOfPost);

            var previousPageLink = listOfPost.HasPrevious
                ? CreateStudentResourceUri(studentResourceParameters,
                                           ResourceUriType.PreviousPage)
                : null;

            var nextPageLink = listOfPost.HasNext
                ? CreateStudentResourceUri(studentResourceParameters,
                                           ResourceUriType.NextPage)
                : null;

            var paginationMetadata = new PaginationHeader
            {
                PreviousPageLink = previousPageLink,
                NextPageLink     = nextPageLink,
                TotalCount       = listOfPost.TotalCount,
                PageSize         = listOfPost.PageSize,
                CurrentPage      = listOfPost.CurrentPage,
                TotalPages       = listOfPost.TotalPages
            };

            Response.Headers.Add("X-Pagination",
                                 Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));
            return(Ok(listOfDtos));
        }
        public async Task <ActionResult> GetStudents([FromQuery] StudentResourceParameters pagedResourceParameters, CancellationToken ct)
        {
            var studentDtos = await _studentService.GetStudentsAsync(pagedResourceParameters, ct);

            var previousPageLink = studentDtos.HasPrevious ? CreateDataflowsResourceUri(pagedResourceParameters, Enums.ResourceUriType.PreviousPage) : null;
            var nextPageLink     = studentDtos.HasNext ? CreateDataflowsResourceUri(pagedResourceParameters, Enums.ResourceUriType.NextPage) : null;

            var paginationMetadata = new
            {
                totalCount  = studentDtos.TotalCount,
                pageSize    = studentDtos.PageSize,
                currentPage = studentDtos.CurrentPage,
                totalPages  = studentDtos.TotalPages,
                previousPageLink,
                nextPageLink
            };

            if (studentDtos == null)
            {
                return(NotFound());
            }

            Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata));
            return(Ok(studentDtos));
        }
Esempio n. 3
0
        private string CreateStudentResourceUri(
            StudentResourceParameters authorsResourceParameters,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(_urlHelper.Link("GetBooks",
                                       new
                {
                    pageNumber = authorsResourceParameters.PageNumber - 1,
                    pageSize = authorsResourceParameters.PageSize
                }));

            case ResourceUriType.NextPage:
                return(_urlHelper.Link("GetBooks",
                                       new
                {
                    pageNumber = authorsResourceParameters.PageNumber + 1,
                    pageSize = authorsResourceParameters.PageSize
                }));

            case ResourceUriType.Current:
            default:
                return(_urlHelper.Link("GetBooks",
                                       new
                {
                    pageNumber = authorsResourceParameters.PageNumber,
                    pageSize = authorsResourceParameters.PageSize
                }));
            }
        }
        private string CreateDataflowsResourceUri(StudentResourceParameters pagedResourceParameters, Enums.ResourceUriType type)
        {
            switch (type)
            {
            case Enums.ResourceUriType.PreviousPage:
                return(_linkGenerator.GetUriByName(this.HttpContext, "GetStudents",
                                                   new
                {
                    status = pagedResourceParameters.Status,
                    searchQuery = pagedResourceParameters.SearchQuery,
                    pageNumber = pagedResourceParameters.PageNumber - 1,
                    pageSize = pagedResourceParameters.PageSize
                }));        // TODO get the aboslute path

            case Enums.ResourceUriType.NextPage:
                return(_linkGenerator.GetUriByName(this.HttpContext, "GetStudents",
                                                   new
                {
                    status = pagedResourceParameters.Status,
                    searchQuery = pagedResourceParameters.SearchQuery,
                    pageNumber = pagedResourceParameters.PageNumber + 1,
                    pageSize = pagedResourceParameters.PageSize
                }));

            default:
                return(_linkGenerator.GetUriByName(this.HttpContext, "GetStudents",
                                                   new
                {
                    status = pagedResourceParameters.Status,
                    searchQuery = pagedResourceParameters.SearchQuery,
                    pageNumber = pagedResourceParameters.PageNumber,
                    pageSize = pagedResourceParameters.PageSize
                }));
            }
        }
Esempio n. 5
0
        public async Task <PagedList <StudentDto> > GetStudentsAsync(StudentResourceParameters resourceParameters, CancellationToken ct)
        {
            var studentEntities = _uow.Students.GetAll();

            /*if (!string.IsNullOrEmpty(resourceParameters.Status))
             * {
             *  if (Enum.TryParse(resourceParameters.Status, true, out DataflowStatus status))
             *  {
             *      studentEntities = studentEntities.Where(a => a.Status == status);
             *  }
             * }*/

            if (!string.IsNullOrEmpty(resourceParameters.SearchQuery))
            {
                var searchQueryForWhereClause = resourceParameters.SearchQuery.Trim().ToLowerInvariant();
                studentEntities = studentEntities
                                  .Where(a => a.Code.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                         a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                         a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause));
            }

            var pagedListEntities = await PagedList <Student> .CreateAsync(studentEntities,
                                                                           resourceParameters.PageNumber, resourceParameters.PageSize, ct);

            var result = _mapper.Map <PagedList <StudentDto> >(pagedListEntities);

            result.TotalCount  = pagedListEntities.TotalCount;
            result.TotalPages  = pagedListEntities.TotalPages;
            result.CurrentPage = pagedListEntities.CurrentPage;
            result.PageSize    = pagedListEntities.PageSize;

            return(result);
        }
Esempio n. 6
0
        public void ShouldNotNull()
        {
            var resource = new StudentResourceParameters();

            resource.PageSize = 20;

            var controller = new StudentsController(_unitOfWork, _urlHelper)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = httpContext
                }
            };

            var sut = controller.GetAllPost(resource);

            var result = Assert.IsType <OkObjectResult>(sut);

            Assert.IsType <List <StudentDTO> >(result.Value);

            Assert.NotNull(sut);
        }
Esempio n. 7
0
        public void ShouldReturnCorrectSize()
        {
            var resource = new StudentResourceParameters();

            resource.PageSize   = 15;
            resource.PageNumber = 2;

            var controller = new StudentsController(_unitOfWork, _urlHelper)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = httpContext
                }
            };
            var sut = controller.GetAllPost(resource);

            var okResult     = Assert.IsType <OkObjectResult>(sut);
            var returnObject = Assert.IsType <List <StudentDTO> >(okResult.Value);

            Assert.Equal(StudentResourceParameters.maxPageSize, returnObject.Count);
            Assert.True(!returnObject.Contains(Mapper.Map <StudentDTO>(_randomStudent[1])));
            AssertObjects.PropertyValuesAreEquals(returnObject[0], Mapper.Map <StudentDTO>(_randomStudent[10]));
        }
Esempio n. 8
0
        public void ShouldReturnCorrectItem()
        {
            var resource = new StudentResourceParameters();

            resource.PageSize   = 15;
            resource.PageNumber = 1;
            resource.Name       = "N";


            var controller = new StudentsController(_unitOfWork, _urlHelper)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = httpContext
                }
            };
            var sut = controller.GetAllPost(resource);

            var okResult     = Assert.IsType <OkObjectResult>(sut);
            var returnObject = Assert.IsType <List <StudentDTO> >(okResult.Value);

            Assert.True(returnObject.All(x => x.Name.ToUpperInvariant().Contains(resource.Name.ToUpperInvariant())));
        }