Esempio n. 1
0
        public PagedList <UserAccount> GetUserAccounts(UserAccountResourceParameters userAccountResourseParameters)
        {
            if (userAccountResourseParameters == null)
            {
                throw new ArgumentNullException(nameof(userAccountResourseParameters));
            }

            var collection = _context.UserAccounts as IQueryable <UserAccount>;

            collection = collection.Where(a => a.CompanyId == userAccountResourseParameters.CompanyId);

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

            if (!string.IsNullOrWhiteSpace(userAccountResourseParameters.OrderBy))
            {
                var userAccountPropertyMappingDictionary = _propertyMappingService.GetUserAccountPropertyMapping <UserAccountDto, UserAccount>();

                collection = collection.ApplySort(userAccountResourseParameters.OrderBy, userAccountPropertyMappingDictionary);
            }

            //Paging.... happens LAST
            return(PagedList <UserAccount> .Create(collection,
                                                   userAccountResourseParameters.PageNumber,
                                                   userAccountResourseParameters.PageSize));
        }
Esempio n. 2
0
        private IEnumerable <LinkDto> CreateLinksForUserAccount(
            UserAccountResourceParameters userAccountResourceParameters,
            bool hasNext, bool hasPrevious)
        {
            var links = new List <LinkDto>();

            // self
            links.Add(
                new LinkDto(CreateUserAccountResourceUri(
                                userAccountResourceParameters, ResourceUriType.Current)
                            , "self", "GET"));

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

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

            return(links);
        }
Esempio n. 3
0
        public IActionResult GetUserAccounts(Guid companyId,
                                             [FromQuery] UserAccountResourceParameters userAccountResourceParameters)
        {
            if (!_propertyMappingService.ValidUserAccountMappingExistsFor <UserAccountDto, Entities.UserAccount>
                    (userAccountResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            userAccountResourceParameters.CompanyId = companyId;
            var userAccountFromRepo = _userAccountRepository.GetUserAccounts(userAccountResourceParameters);

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

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

            var links = CreateLinksForUserAccount(userAccountResourceParameters,
                                                  userAccountFromRepo.HasNext,
                                                  userAccountFromRepo.HasPrevious);

            var shapedUserAccounts = _mapper.Map <IEnumerable <UserAccountDto> >(userAccountFromRepo)
                                     .ShapeData(userAccountResourceParameters.Fields);

            var shapedUserAccountsWithLinks = shapedUserAccounts.Select(userAccount =>
            {
                var userAccountAsDictionary = userAccount as IDictionary <string, object>;
                var userAccountLinks        = CreateLinksForUserAccount(userAccountResourceParameters.CompanyId.ToString(), userAccountAsDictionary["Id"].ToString(), null);
                userAccountAsDictionary.Add("links", userAccountLinks);
                return(userAccountAsDictionary);
            });

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

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

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

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