Esempio n. 1
0
        public BaseResponse <TransferResponse> Transfer(string userId, TransferRequest transferRequest)
        {
            var response = new BaseResponse <TransferResponse>();

            try
            {
                transferRequest.UserId = userId;
                TransferValidator transferValidator = new TransferValidator(_checkingAccountService);
                FluentValidation.Results.ValidationResult validationResult = transferValidator.Validate(transferRequest);

                if (!validationResult.IsValid)
                {
                    response.AddErrors(validationResult.Errors);
                    return(response);
                }

                var from = _checkingAccountService.UpdateBalance(transferRequest.NumberFrom, transferRequest.Amount * -1);
                var to   = _checkingAccountService.UpdateBalance(transferRequest.NumberTo, transferRequest.Amount);

                _transactionService.Add(new Transaction
                {
                    Amount = transferRequest.Amount,
                    FromCheckingAccountId = from.Id,
                    ToCheckingAccountId   = to.Id
                });

                _checkingAccountService.Commit();

                response.Data = new TransferResponse {
                    Balance = from.Balance
                };
            }
            catch (Exception ex)
            {
                _checkingAccountService.Rollback();
                response.AddError(1, "Erro inesperado");
            }
            return(response);
        }
Esempio n. 2
0
 public CheckingAccount UpdateBalance(long accountNumber, decimal newBalance)
 {
     return(_checkingAccountService.UpdateBalance(accountNumber, newBalance));
 }