Exemplo n.º 1
0
 public virtual void Begin(IsolationLevel level = IsolationLevel.Unspecified)
 {
     _repository.BeginTransaction(level);
 }
        public async Task CreateTransactionAsync(Account fromAccount, Account toAccount, decimal exchangeRate, decimal amount, string description, string fromCurrency, string toCurrency)
        {
            using (var transaction = await _transactionRepository.BeginTransaction())
            {
                try
                {
                    var withdrawTransfer = new Transaction
                    {
                        Id            = new Guid(),
                        FromAccountId = fromAccount.Id,
                        ToAccountId   = null,
                        ExchangeRate  = exchangeRate,
                        Amount        = amount,
                        CardId        = null,
                        Date          = DateTime.Now,
                        Description   = description,
                        StatusId      = 1,
                        FromCurrency  = fromCurrency,
                        ToCurrency    = toCurrency,
                        Number        = _transactionHelper.Number(),
                        Destination   = toAccount.Number
                    };

                    if ((fromAccount.Balance - amount) < 0)
                    {
                        withdrawTransfer.StatusId = 2;

                        await _transactionRepository.Add(withdrawTransfer);
                    }
                    else
                    {
                        fromAccount.Balance -= amount;
                        await _transactionRepository.Add(withdrawTransfer);

                        var incomeTransfer = new Transaction
                        {
                            Id            = new Guid(),
                            FromAccountId = fromAccount.Id,
                            ToAccountId   = toAccount.Id,
                            ExchangeRate  = exchangeRate,
                            Amount        = amount * exchangeRate,
                            CardId        = null,
                            Date          = DateTime.Now,
                            Description   = description,
                            StatusId      = 1,
                            FromCurrency  = fromCurrency,
                            ToCurrency    = toCurrency,
                            Number        = _transactionHelper.Number(),
                            Destination   = toAccount.Number
                        };

                        toAccount.Balance += amount * exchangeRate;
                        await _transactionRepository.Add(incomeTransfer);

                        await _accountRepository.Update();
                    }

                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();

                    await _transactionRepository.Add(new Transaction
                    {
                        Id            = new Guid(),
                        FromAccountId = fromAccount.Id,
                        ToAccountId   = null,
                        ExchangeRate  = exchangeRate,
                        Amount        = amount,
                        CardId        = null,
                        Date          = DateTime.Now,
                        Description   = description,
                        StatusId      = 2,
                        FromCurrency  = fromCurrency,
                        ToCurrency    = null,
                        Number        = _transactionHelper.Number(),
                        Destination   = toAccount.Number
                    });
                }
            }
        }