public ActionResult <CustomerAccountDto> CreateCustomerAccount(Guid companyId, CustomerAccountForCreationDto customerAccount)
        {
            var customerAccountEntity = _mapper.Map <Entities.CustomerAccount>(customerAccount);

            _customerAccountRepository.CreateCustomerAccount(customerAccountEntity);
            _customerAccountRepository.Save();

            var customerAccountToReturn = _mapper.Map <CustomerAccountDto>(customerAccountEntity);

            var links = CreateLinksForCustomerAccounts(companyId.ToString(), customerAccountToReturn.Id, null);

            var linkedResourceToReturn = customerAccountToReturn.ShapeData(null)
                                         as IDictionary <string, object>;

            linkedResourceToReturn.Add("links", links);

            return(CreatedAtRoute("GetCustomerAccount",
                                  new { companyId, CustomerAccountId = linkedResourceToReturn["Id"] },
                                  linkedResourceToReturn));
        }
Beispiel #2
0
        public async Task <ActionResult <CustomerAccount> > OpenAccount(
            [SwaggerParameter("New account request parameters", Required = true)] CustomerAccountCreateRequest customerAccountCreateRequest)
        {
            if (customerAccountCreateRequest.CustomerId <= 0)
            {
                return(BadRequest(new ProblemDetails
                {
                    Detail = "Invalid customer Id"
                }));
            }

            if (customerAccountCreateRequest.InitialCredit < 0)
            {
                return(BadRequest(new ProblemDetails
                {
                    Detail = "Initial credit could not be negative"
                }));
            }

            try
            {
                var customer = _customerAccountRepository.GetCustomer(customerAccountCreateRequest.CustomerId);
                if (customer == null)
                {
                    return(BadRequest($"Customer with id {customerAccountCreateRequest.CustomerId} does not exist"));
                }

                var newAccount = _customerAccountRepository.CreateCustomerAccount(customerAccountCreateRequest.CustomerId, customerAccountCreateRequest.InitialCredit);

                if (customerAccountCreateRequest.InitialCredit != 0)
                {
                    var newTransaction = new AccountTransaction
                    {
                        AccountId  = newAccount.CustomerAccountId,
                        Amount     = customerAccountCreateRequest.InitialCredit,
                        CreateDate = DateTime.UtcNow,
                        CustomerId = customerAccountCreateRequest.CustomerId
                    };

                    await _transactionProxyService.CreateTransaction(newTransaction);
                }

                return(Ok(_mapper.Map <CustomerAccount>(newAccount)));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error in {nameof(OpenAccount)} with customerId {customerAccountCreateRequest?.CustomerId}. Details: {ex}");
                return(StatusCode(500));
            }
        }