public async Task Handle(AccountReplenish msg, IMessageMetadata metadata = null) { using (var context = _contextFactory()) { var account = await context.Accounts.FindAsync(msg.SourceId); account.LastModified = msg.CreatedTime; var initialAmount = account.Amount; account.Amount += msg.Amount.Amount; var transaction = new AccountTransaction { AccountId = msg.SourceId, ChangeAmount = msg.Amount.Amount, Created = msg.CreatedTime, Currency = msg.Amount.CurrencyCode.ToString(), InitialAmount = initialAmount, NewAmount = account.Amount, Operation = AccountOperations.Replenish, TransactionId = msg.ChangeId }; context.TransactionHistory.Add(transaction); await context.SaveChangesAsync(); } }
public Account_transactions_projection_tests() { // Given_account_created_and_projecting() var msgCreated = new AccountCreated(Guid.NewGuid(), Guid.NewGuid(), 42); var user = new User { Id = msgCreated.UserId, Login = "******" }; _msgReplenish = new AccountReplenish(msgCreated.SourceId, Guid.NewGuid(), new Money(100)); _msgWithdrawal = new AccountWithdrawal(msgCreated.SourceId, Guid.NewGuid(), new Money(30)); using (var context = CreateContext()) { context.Users.Add(user); context.SaveChanges(); } ProjectionBuilder.Handle(msgCreated).Wait(); ProjectionBuilder.Handle(_msgReplenish).Wait(); ProjectionBuilder.Handle(_msgWithdrawal).Wait(); }