private IEnumerable <LinkDto> CreateLinksForTerminalSettings(
            TerminalSettingsParameters terminalSettingsParameters,
            bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>();

            // self
            links.Add(
                new LinkDto(CreateTerminalSettingsResourceUri(
                                terminalSettingsParameters, ResourceUriType.Current)
                            , "self", "GET"));

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

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

            return(links);
        }
        public PagedList <TerminalSettings> GetTerminalSettings(TerminalSettingsParameters terminalSettingsParameters)
        {
            if (terminalSettingsParameters == null)
            {
                throw new ArgumentNullException(nameof(terminalSettingsParameters));
            }

            if (terminalSettingsParameters.TerminalId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(terminalSettingsParameters));
            }

            var collection = _context.TerminalSettings as IQueryable <TerminalSettings>;

            collection = collection.Where(a => a.TerminalId == terminalSettingsParameters.TerminalId);
            if (!string.IsNullOrWhiteSpace(terminalSettingsParameters.OrderBy))
            {
                var terminalSettingsPropertyMappingDictionary = _propertyMappingService.GetTerminalSettingsPropertyMapping <TerminalSettingsDto, TerminalSettings>();

                collection = collection.ApplySort(terminalSettingsParameters.OrderBy, terminalSettingsPropertyMappingDictionary);
            }

            //Paging.... happens LAST
            return(PagedList <TerminalSettings> .Create(collection,
                                                        terminalSettingsParameters.PageNumber,
                                                        terminalSettingsParameters.PageSize));
        }
        public IActionResult GetTerminalSettings(Guid terminalId,
                                                 [FromQuery] TerminalSettingsParameters terminalSettingsParameters)
        {
            terminalSettingsParameters.TerminalId = terminalId;

            if (!_propertyMappingService.ValidTerminalSettingsMappingExistsFor <TerminalSettingsDto, Entities.TerminalSettings>
                    (terminalSettingsParameters.OrderBy))
            {
                return(BadRequest());
            }

            var terminalSettingsFromRepo = _terminalSettingsRepository.GetTerminalSettings(terminalSettingsParameters);

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

            Response.Headers.Add("X-Pagination",
                                 JsonSerializer.Serialize(paginationMetadata));

            var links = CreateLinksForTerminalSettings(terminalSettingsParameters,
                                                       terminalSettingsFromRepo.HasNext,
                                                       terminalSettingsFromRepo.HasPrevious);

            var shapedSites = _mapper.Map <IEnumerable <TerminalSettingsDto> >(terminalSettingsFromRepo)
                              .ShapeData(terminalSettingsParameters.Fields);

            var shapedTerminalWithLinks = shapedSites.Select(terminalSettings =>
            {
                var terminalSettingsAsDictionary = terminalSettings as IDictionary <string, object>;
                var terminalSettingsLinks        = CreateLinksForTerminalSettings(terminalId.ToString(), (string)terminalSettingsAsDictionary["Id"], null);
                terminalSettingsAsDictionary.Add("links", terminalSettingsLinks);
                return(terminalSettingsAsDictionary);
            });

            var linkedCollectionResource = new
            {
                value = shapedTerminalWithLinks,
                links
            };

            return(Ok(linkedCollectionResource));
        }
        private string CreateTerminalSettingsResourceUri(
            TerminalSettingsParameters terminalSettingsParameters,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetTerminalSettings",
                                new
                {
                    fields = terminalSettingsParameters.Fields,
                    orderBy = terminalSettingsParameters.OrderBy,
                    pageNumber = terminalSettingsParameters.PageNumber - 1,
                    pageSize = terminalSettingsParameters.PageSize,
                    searchQuery = terminalSettingsParameters.SearchQuery
                }));

            case ResourceUriType.NextPage:
                return(Url.Link("GetTerminalSettings",
                                new
                {
                    fields = terminalSettingsParameters.Fields,
                    orderBy = terminalSettingsParameters.OrderBy,
                    pageNumber = terminalSettingsParameters.PageNumber + 1,
                    pageSize = terminalSettingsParameters.PageSize,
                    searchQuery = terminalSettingsParameters.SearchQuery
                }));

            case ResourceUriType.Current:
            default:
                return(Url.Link("GetTerminalSettings",
                                new
                {
                    fields = terminalSettingsParameters.Fields,
                    orderBy = terminalSettingsParameters.OrderBy,
                    pageNumber = terminalSettingsParameters.PageNumber,
                    pageSize = terminalSettingsParameters.PageSize,
                    searchQuery = terminalSettingsParameters.SearchQuery
                }));
            }
        }