Example #1
0
        public IActionResult GetTopics(TopicsResourceParameters topicsResourceParameters)
        {
            var topicEntities = noterRepository.GetTopics(topicsResourceParameters);

            var previousPageLink = topicEntities.HasPrevious ?
                                   CreateTopicsResourceUri(topicsResourceParameters, ResourceUriType.PreviousPage)
                : null;

            var nextPageLink = topicEntities.HasNext ?
                               CreateTopicsResourceUri(topicsResourceParameters, ResourceUriType.NextPage)
                : null;

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

            Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(paginationMetadata));

            var topicDtos = Mapper.Map <IEnumerable <TopicDto> >(topicEntities);

            return(Ok(topicDtos));
        }
Example #2
0
        private string CreateTopicsResourceUri(TopicsResourceParameters topicsResourceParameters, ResourceUriType resourceUriType)
        {
            switch (resourceUriType)
            {
            case ResourceUriType.PreviousPage: return(urlHelper.Link("GetTopics",
                                                                     new
                {
                    searchTitle = topicsResourceParameters.SearchTitle,
                    pageNumber = topicsResourceParameters.PageNumber - 1,
                    pageSize = topicsResourceParameters.PageSize
                }));

            case ResourceUriType.NextPage: return(urlHelper.Link("GetTopics",
                                                                 new
                {
                    searchTitle = topicsResourceParameters.SearchTitle,
                    pageNumber = topicsResourceParameters.PageNumber + 1,
                    pageSize = topicsResourceParameters.PageSize
                }));

            default:
                return(urlHelper.Link("GetTopics",
                                      new
                {
                    searchTitle = topicsResourceParameters.SearchTitle,
                    pageNumber = topicsResourceParameters.PageNumber,
                    pageSize = topicsResourceParameters.PageSize
                }));
            }
        }
Example #3
0
        public PagedList <Topic> GetTopics(TopicsResourceParameters topicsResourceParameters)
        {
            IQueryable <Topic> query;

            if (!string.IsNullOrEmpty(topicsResourceParameters.SearchTitle))
            {
                query = context.Topics
                        .Where(e => e.IsPage == false && e.Title.Contains(topicsResourceParameters.SearchTitle))
                        .Include(e => e.Commentaries);
            }
            else
            {
                query = context.Topics
                        .Where(e => e.IsPage == false)
                        .Include(e => e.Commentaries);
            }

            return(PagedList <Topic> .Create(query, topicsResourceParameters.PageNumber, topicsResourceParameters.PageSize));
        }