Exemple #1
0
        private IEnumerable <LinkDto> CreateLinksForSite(
            SiteResourceParameters siteResourceParameters,
            bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>();

            // self
            links.Add(
                new LinkDto(CreateSitesResourceUri(
                                siteResourceParameters, ResourceUriType.Current)
                            , "self", "GET"));

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

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

            return(links);
        }
Exemple #2
0
        public IActionResult GetSites(Guid companyId, Guid siteTypeId,
                                      [FromQuery] SiteResourceParameters siteResourceParameters)
        {
            if (!_propertyMappingService.ValidSiteMappingExistsFor <SiteDto, Entities.Site>
                    (siteResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            siteResourceParameters.CompanyId = companyId;

            var sitesFromRepo = _siteRepository.GetSites(siteResourceParameters);

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

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

            var links = CreateLinksForSite(siteResourceParameters,
                                           sitesFromRepo.HasNext,
                                           sitesFromRepo.HasPrevious);

            var shapedSites = _mapper.Map <IEnumerable <SiteDto> >(sitesFromRepo)
                              .ShapeData(siteResourceParameters.Fields);

            var shapedSitesWithLinks = shapedSites.Select(sites =>
            {
                var siteAsDictionary = sites as IDictionary <string, object>;
                var siteLinks        = CreateLinksForSite(companyId.ToString(), siteTypeId.ToString(), (string)siteAsDictionary["Id"], null);
                siteAsDictionary.Add("links", siteLinks);
                return(siteAsDictionary);
            });

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

            return(Ok(linkedCollectionResource));
        }
Exemple #3
0
        private string CreateSitesResourceUri(
            SiteResourceParameters siteResourceParameters,
            ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link("GetSites",
                                new
                {
                    fields = siteResourceParameters.Fields,
                    orderBy = siteResourceParameters.OrderBy,
                    pageNumber = siteResourceParameters.PageNumber - 1,
                    pageSize = siteResourceParameters.PageSize,
                    searchQuery = siteResourceParameters.SearchQuery
                }));

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

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