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())); }
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; }
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); }
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(); }
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); }
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); } }
protected void When(WithdrewDomainEvent domainEvent) { Transaction debit = new Debit(domainEvent.AggregateRootId, domainEvent.TransactionAmount); Transactions.Add(debit); }