public PagedList <CustomerAccount> GetCustomerAccounts(CustomerAccountResourceParameters customerResourseParameters) { if (customerResourseParameters == null) { throw new ArgumentNullException(nameof(customerResourseParameters)); } var collection = _context.CustomerAccounts as IQueryable <CustomerAccount>; if (!string.IsNullOrWhiteSpace(customerResourseParameters.SearchQuery)) { var searchQuery = customerResourseParameters.SearchQuery.Trim(); collection = collection.Where(a => a.ContactName.Contains(searchQuery)); } if (!string.IsNullOrWhiteSpace(customerResourseParameters.OrderBy)) { var customerAccountPropertyMappingDictionary = _propertyMappingService.GetCustomerAccountPropertyMapping <CustomerAccountDto, CustomerAccount>(); collection = collection.ApplySort(customerResourseParameters.OrderBy, customerAccountPropertyMappingDictionary); } //Paging.... happens LAST return(PagedList <CustomerAccount> .Create(collection, customerResourseParameters.PageNumber, customerResourseParameters.PageSize)); }
private IEnumerable <LinkDto> CreateLinksForCustomerAccount( CustomerAccountResourceParameters customerAccountResourceParameters, bool hasNext, bool hasPrevious) { var links = new List <LinkDto>(); // self links.Add( new LinkDto(CreateCustomerAccountResourceUri( customerAccountResourceParameters, ResourceUriType.Current) , "self", "GET")); if (hasNext) { links.Add( new LinkDto(CreateCustomerAccountResourceUri( customerAccountResourceParameters, ResourceUriType.NextPage), "nextPage", "GET")); } if (hasPrevious) { links.Add( new LinkDto(CreateCustomerAccountResourceUri( customerAccountResourceParameters, ResourceUriType.PreviousPage), "previousPage", "GET")); } return(links); }
public IActionResult GetCustomerAccounts(Guid companyId, [FromQuery] CustomerAccountResourceParameters customerAccountResourceParameters) { if (!_propertyMappingService.ValidCustomerAccountMappingExistsFor <CustomerAccountDto, Entities.CustomerAccount> (customerAccountResourceParameters.OrderBy)) { return(BadRequest()); } customerAccountResourceParameters.CompanyId = companyId; var customerAccountFromRepo = _customerAccountRepository.GetCustomerAccounts(customerAccountResourceParameters); var paginationMetadata = new { totalCount = customerAccountFromRepo.TotalCount, pageSize = customerAccountFromRepo.PageSize, currentPage = customerAccountFromRepo.CurrentPage, totalPages = customerAccountFromRepo.TotalPages }; Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata)); var links = CreateLinksForCustomerAccount(customerAccountResourceParameters, customerAccountFromRepo.HasNext, customerAccountFromRepo.HasPrevious); var shapedCustomerAccount = _mapper.Map <IEnumerable <CustomerAccountDto> >(customerAccountFromRepo) .ShapeData(customerAccountResourceParameters.Fields); var shapedCustomerAccountWithLinks = shapedCustomerAccount.Select(customerAccount => { var customerAccountAsDictionary = customerAccount as IDictionary <string, object>; var customerAccountLinks = CreateLinksForCustomerAccounts(companyId.ToString(), (string)customerAccountAsDictionary["Id"], null); customerAccountAsDictionary.Add("links", customerAccountLinks); return(customerAccountAsDictionary); }); var linkedCollectionResource = new { value = shapedCustomerAccountWithLinks, links }; return(Ok(linkedCollectionResource)); }
private string CreateCustomerAccountResourceUri( CustomerAccountResourceParameters customerAccountResourceParameters, ResourceUriType type) { switch (type) { case ResourceUriType.PreviousPage: return(Url.Link("GetCustomerAccounts", new { fields = customerAccountResourceParameters.Fields, orderBy = customerAccountResourceParameters.OrderBy, pageNumber = customerAccountResourceParameters.PageNumber - 1, pageSize = customerAccountResourceParameters.PageSize, mainCategory = customerAccountResourceParameters.Type, searchQuery = customerAccountResourceParameters.SearchQuery })); case ResourceUriType.NextPage: return(Url.Link("GetCustomerAccounts", new { fields = customerAccountResourceParameters.Fields, orderBy = customerAccountResourceParameters.OrderBy, pageNumber = customerAccountResourceParameters.PageNumber + 1, pageSize = customerAccountResourceParameters.PageSize, mainCategory = customerAccountResourceParameters.Type, searchQuery = customerAccountResourceParameters.SearchQuery })); case ResourceUriType.Current: default: return(Url.Link("GetCustomerAccounts", new { fields = customerAccountResourceParameters.Fields, orderBy = customerAccountResourceParameters.OrderBy, pageNumber = customerAccountResourceParameters.PageNumber, pageSize = customerAccountResourceParameters.PageSize, mainCategory = customerAccountResourceParameters.Type, searchQuery = customerAccountResourceParameters.SearchQuery })); } }