Example #1
0
        private string CreateSpeakersResourceUri(
            SpeakerResourceParameters speakersParam,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetSpeakers",
                                new
                {
                    pageNumber = speakersParam.PageNumber - 1,
                    pageSize = speakersParam.PageSize
                }));

            case ResourceUriType.NextPage:
                return(Url.Link("GetSpeakers",
                                new
                {
                    pageNumber = speakersParam.PageNumber + 1,
                    pageSize = speakersParam.PageSize
                }));

            case ResourceUriType.Current:
            default:
                return(Url.Link("GetSpeakers",
                                new
                {
                    pageNumber = speakersParam.PageNumber,
                    pageSize = speakersParam.PageSize
                }));
            }
        }
Example #2
0
        private IEnumerable <LinkDto> CreateLinksForSpeakers(
            SpeakerResourceParameters speakersParam,
            bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>();

            // self
            links.Add(
                new LinkDto(CreateSpeakersResourceUri(
                                speakersParam, ResourceUriType.Current)
                            , "self", "GET"));

            if (hasNext)
            {
                links.Add(
                    new LinkDto(CreateSpeakersResourceUri(
                                    speakersParam, ResourceUriType.NextPage),
                                "nextPage", "GET"));
            }

            if (hasPrevious)
            {
                links.Add(
                    new LinkDto(CreateSpeakersResourceUri(
                                    speakersParam, ResourceUriType.PreviousPage),
                                "previousPage", "GET"));
            }

            return(links);
        }
Example #3
0
        public PagedList <Speaker> GetSpeakers(SpeakerResourceParameters speakerResourceParameters)
        {
            if (speakerResourceParameters == null)
            {
                throw new ArgumentNullException(nameof(speakerResourceParameters));
            }

            var collection = _context.Speakers as IQueryable <Speaker>;


            return(PagedList <Speaker> .Create(collection,
                                               speakerResourceParameters.PageNumber,
                                               speakerResourceParameters.PageSize));
        }
Example #4
0
        public IActionResult GetSpeakers([FromQuery] SpeakerResourceParameters speakersParam)
        {
            var speakersFromRepo = _speakerRepository.GetSpeakers(speakersParam);
            //map from repo to DTO
            var paginationMetadata = new
            {
                totalCount  = speakersFromRepo.TotalCount,
                pageSize    = speakersFromRepo.PageSize,
                currentPage = speakersFromRepo.CurrentPage,
                totalPages  = speakersFromRepo.TotalPages
            };

            Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata));
            //response.Headers.Add("X-Paging-PageNo", pageNo.ToString());
            //response.Headers.Add("X-Paging-PageSize", pageSize.ToString());
            //response.Headers.Add("X-Paging-PageCount", pageCount.ToString());
            //response.Headers.Add("X-Paging-TotalRecordCount", total.ToString());
            var links = CreateLinksForSpeakers(speakersParam,
                                               speakersFromRepo.HasNext,
                                               speakersFromRepo.HasPrevious);

            var shapedSpeakers = _mapper.Map <IEnumerable <SpeakerDto> >(speakersFromRepo)
                                 .ShapeData();

            var speakerWithLinks = shapedSpeakers.Select(speaker =>
            {
                var speakerAsDictionary = speaker as IDictionary <string, object>;
                var speakerLinks        = CreateLinksForSpeaker((int)speakerAsDictionary["Id"]);
                speakerAsDictionary.Add("links", speakerLinks);
                return(speakerAsDictionary);
            });

            var linkedResource = new
            {
                value = speakerWithLinks,
                links
            };

            return(Ok(linkedResource));
        }