public PagedList <Terminal> GetTerminals(TerminalResourceParameters terminalResourseParameters)
        {
            if (terminalResourseParameters == null)
            {
                throw new ArgumentNullException(nameof(terminalResourseParameters));
            }

            if (terminalResourseParameters.SiteId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(terminalResourseParameters));
            }

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

            collection = collection.Where(a => a.SiteId == terminalResourseParameters.SiteId);

            if (!string.IsNullOrWhiteSpace(terminalResourseParameters.SearchQuery))
            {
                var searchQuery = terminalResourseParameters.SearchQuery.Trim();
                collection = collection.Where(a => a.TerminalName.Contains(searchQuery));
            }

            if (!string.IsNullOrWhiteSpace(terminalResourseParameters.OrderBy))
            {
                var terminalPropertyMappingDictionary = _propertyMappingService.GetTerminalPropertyMapping <TerminalDto, Terminal>();

                collection = collection.ApplySort(terminalResourseParameters.OrderBy, terminalPropertyMappingDictionary);
            }

            //Paging.... happens LAST
            return(PagedList <Terminal> .Create(collection,
                                                terminalResourseParameters.PageNumber,
                                                terminalResourseParameters.PageSize));
        }
Esempio n. 2
0
        private IEnumerable <LinkDto> CreateLinksForTerminal(
            TerminalResourceParameters terminalResourceParameters,
            bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>();

            // self
            links.Add(
                new LinkDto(CreateTerminalResourceUri(
                                terminalResourceParameters, ResourceUriType.Current)
                            , "self", "GET"));

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

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

            return(links);
        }
Esempio n. 3
0
        public IActionResult GetTerminals(Guid siteId,
                                          [FromQuery] TerminalResourceParameters terminalResourceParameters)
        {
            if (siteId == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(siteId));
            }

            if (!_propertyMappingService.ValidTerminalMappingExistsFor <TerminalDto, Entities.Terminal>
                    (terminalResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            terminalResourceParameters.SiteId = siteId;

            var terminalFromRepo = _terminalRepository.GetTerminals(terminalResourceParameters);

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

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

            var links = CreateLinksForTerminal(terminalResourceParameters,
                                               terminalFromRepo.HasNext,
                                               terminalFromRepo.HasPrevious);

            var shapedTerminals = _mapper.Map <IEnumerable <TerminalDto> >(terminalFromRepo)
                                  .ShapeData(terminalResourceParameters.Fields);

            var shapedTerminalWithLinks = shapedTerminals.Select(terminal =>
            {
                var terminalAsDictionary = terminal as IDictionary <string, object>;
                var terminalLinks        = CreateLinksForTerminal(siteId.ToString(), (string)terminalAsDictionary["Id"], null);
                terminalAsDictionary.Add("links", terminalLinks);
                return(terminalAsDictionary);
            });

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

            return(Ok(linkedCollectionResource));
        }
Esempio n. 4
0
        private string CreateTerminalResourceUri(
            TerminalResourceParameters terminalResourceParameters,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetTerminal",
                                new
                {
                    fields = terminalResourceParameters.Fields,
                    orderBy = terminalResourceParameters.OrderBy,
                    pageNumber = terminalResourceParameters.PageNumber - 1,
                    pageSize = terminalResourceParameters.PageSize,
                    searchQuery = terminalResourceParameters.SearchQuery
                }));

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

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