Exemple #1
0
 public ActionResult <IEnumerable <CustomerAccount> > Get()
 {
     _logger.LogInformation("Get all customer accounts");
     try
     {
         var customerAccounts = _customerAccountRepository.GetCustomerAccounts();
         return(Ok(customerAccounts));
     }
     catch (Exception ex)
     {
         _logger.LogError($"Error in {nameof(Get)}. Details: {ex}");
         return(StatusCode(500));
     }
 }
        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));
        }