Пример #1
0
        public void Withdraw(Transaction transaction)
        {
            if (GetCurrentBalance() < transaction.GetAmount())
            {
                throw new InsuficientFundsException($"The account {Id} does not have enough funds to withdraw {transaction.GetAmount()}.");
            }

            Raise(WithdrewDomainEvent.Create(this, transaction.Id, transaction.GetCustomerId(), transaction.GetAmount()));
        }
Пример #2
0
        protected void When(WithdrewDomainEvent domainEvent)
        {
            if (transactions == null)
            {
                transactions = new List <Transaction>();
            }

            Transaction transaction = Debit.Load(domainEvent.TransactionId, domainEvent.CustomerId, domainEvent.Amount);

            transactions.Add(transaction);

            currentBalance = currentBalance - domainEvent.Amount;
        }
Пример #3
0
        public void Withdraw(Debit debit)
        {
            if (Transactions.GetCurrentBalance() < debit.Amount)
            {
                throw new InsuficientFundsException($"The account {Id} does not have enough funds to withdraw {debit.Amount}.");
            }

            var domainEvent = new WithdrewDomainEvent(
                Id,
                Version,
                debit.Id,
                debit.Amount,
                DateTime.Now
                );

            Raise(domainEvent);
        }
Пример #4
0
        public async Task Update(WithdrewDomainEvent domainEvent)
        {
            Account account = await context.Accounts.FindAsync(domainEvent.AggregateRootId);

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {domainEvent.AggregateRootId} does not exists or is already closed.");
            }

            if (account.Version != domainEvent.Version)
            {
                throw new TransactionConflictException(account, domainEvent);
            }

            account.Apply(domainEvent);

            int affectedRows = await context.SaveChangesAsync();
        }
Пример #5
0
        public async Task Update(WithdrewDomainEvent domainEvent)
        {
            Account account = await mongoContext
                              .Accounts
                              .Find(e => e.Id == domainEvent.AggregateRootId)
                              .SingleOrDefaultAsync();

            if (account == null)
            {
                throw new AccountNotFoundException($"The account {domainEvent.AggregateRootId} does not exists or is already closed.");
            }

            if (account.Version != domainEvent.Version)
            {
                throw new TransactionConflictException(account, domainEvent);
            }

            account.Apply(domainEvent);

            await mongoContext.Accounts.ReplaceOneAsync(e => e.Id == account.Id, account);
        }
Пример #6
0
        public async Task Update(WithdrewDomainEvent domainEvent)
        {
            using (IDbConnection db = new SqlConnection(connectionString))
            {
                string            updateAccountSQL  = "UPDATE Account SET Version = @Version WHERE Id = @Id";
                DynamicParameters accountParameters = new DynamicParameters();
                accountParameters.Add("@id", domainEvent.AggregateRootId);
                accountParameters.Add("@version", domainEvent.Version);

                int rowsAffected = await db.ExecuteAsync(updateAccountSQL, accountParameters);

                string insertCreditSQL = "INSERT INTO [Transaction] (Id, Amount, TransactionDate, AccountId, TransactionType) " +
                                         "VALUES (@Id, @Amount, @TransactionDate, @AccountId, @TransactionType)";

                DynamicParameters transactionParameters = new DynamicParameters();
                transactionParameters.Add("@id", domainEvent.TransactionId);
                transactionParameters.Add("@amount", domainEvent.TransactionAmount.Value);
                transactionParameters.Add("@transactionDate", domainEvent.TransactionDate);
                transactionParameters.Add("@accountId", domainEvent.AggregateRootId);
                transactionParameters.Add("@transactionType", 0);

                int debitRows = await db.ExecuteAsync(insertCreditSQL, transactionParameters);
            }
        }
Пример #7
0
        protected void When(WithdrewDomainEvent domainEvent)
        {
            Transaction debit = new Debit(domainEvent.AggregateRootId, domainEvent.TransactionAmount);

            Transactions.Add(debit);
        }