Beispiel #1
0
        private void Handle(Withrowal cmd)
        {
            var sender = Sender;

            if (cmd.From != _state.AccountNumber)
            {
                throw new NotFoundException("Invalid Account Number");
            }

            if (cmd.Amount <= 0)
            {
                throw new ValidationException("Withrowal Amount need to be positive value.");
            }

            if (cmd.Amount > _state.AvailableBalance)
            {
                throw new ValidationException("Insuficient funds");
            }

            var withrowal = new AccountDebited
            {
                TransactionId = cmd.TransactionId,
                From          = _state.AccountNumber,
                Amount        = cmd.Amount,
                CurrencyDate  = DateTime.UtcNow,
                To            = cmd.To
            };

            Persist(withrowal, e =>
            {
                ApplyEvent(e);
                Respond(sender, "Ok", new[] { withrowal });
            });
        }
Beispiel #2
0
 private void Apply(AccountDebited accountDebited)
 {
     AvailableBalance -= accountDebited.Amount;
     OutstandingTransactions.Add(accountDebited.TransactionId, new Transaction {
         Amount = accountDebited.Amount
     });
 }
Beispiel #3
0
 public static IImmutableList <string> GetTopics(this AccountDebited self)
 {
     return(ImmutableArray.CreateRange(
                new[]
     {
         "\\Accounts",
         $"\\Account\\{self.AccountId}",
     }));
 }
Beispiel #4
0
        public IImmutableList <string> GetTopics(object message)
        {
            return(message switch
            {
                AccountCreated evt => evt.GetTopics(),
                AccountCredited evt => evt.GetTopics(),
                AccountDebited evt => evt.GetTopics(),

                _ => _onlyRootTopic
            });
Beispiel #5
0
 public void Apply(AccountDebited @event)
 {
     Transactions.Add(new Transaction()
     {
         Amount      = @event.Amount,
         Id          = @event.Id,
         Type        = "Debit",
         Description = @event.Description
     });
 }
        public bool HasSufficientFunds(AccountDebited debit)
        {
            var result = (Balance - debit.Amount) >= 0;

            if (!result)
            {
                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine($"{Owner} has insufficient funds for debit");
            }
            return(result);
        }
        public static object CreateAccountObject(int version)
        {
            object accountObject = null;

            var internalCounter = version + 1;

            {
                const int checkpointVersion = 10;

                var checkPointModVersion = internalCounter % checkpointVersion;
                if (checkPointModVersion == 0)
                {
                    int otherCheckPointsCount = internalCounter / checkpointVersion;

                    var elementsCount = internalCounter / 2;

                    var creditedSum = ComputeSum(20, elementsCount, 20) - ComputeSum(100, otherCheckPointsCount, 100);
                    var debitedSum  = ComputeSum(10, elementsCount, 20);

                    var checkpoint = new AccountCheckPoint(creditedSum, debitedSum);

                    accountObject = checkpoint;
                }
                else
                {
                    var modVersion = internalCounter % 2;
                    if (modVersion == 0)
                    {
                        var credited = new AccountCredited(internalCounter * 10, internalCounter % 17);
                        accountObject = credited;
                    }
                    else
                    {
                        var debited = new AccountDebited(internalCounter * 10, internalCounter % 17);
                        accountObject = debited;
                    }
                }
            }
            return(accountObject);
        }
        public static object CreateAccountObject(int version)
        {
            object accountObject = null;

            var internalCounter = version + 1;
            
            {
                const int checkpointVersion = 10;

                var checkPointModVersion = internalCounter % checkpointVersion;
                if (checkPointModVersion == 0)
                {
                    int otherCheckPointsCount = internalCounter / checkpointVersion;

                    var elementsCount = internalCounter / 2;

                    var creditedSum = ComputeSum(20, elementsCount, 20) - ComputeSum(100, otherCheckPointsCount, 100);
                    var debitedSum = ComputeSum(10, elementsCount, 20);

                    var checkpoint = new AccountCheckPoint(creditedSum, debitedSum);

                    accountObject = checkpoint;
                }
                else
                {
                    var modVersion = internalCounter % 2;
                    if (modVersion == 0)
                    {
                        var credited = new AccountCredited(internalCounter * 10, internalCounter % 17);
                        accountObject = credited;
                    }
                    else
                    {
                        var debited = new AccountDebited(internalCounter * 10, internalCounter % 17);
                        accountObject = debited;
                    }
                }
            }
            return accountObject;
        }
 public void Apply(AccountDebited toApply)
 {
     Balance -= toApply.Amount;
 }
Beispiel #10
0
        public static void Main(string[] args)
        {
            var store = DocumentStore.For(_ =>
            {
                _.Connection("host=localhost;database=marten_test;password=postgres;username=postgres");

                _.AutoCreateSchemaObjects = AutoCreate.All;

                _.Events.AddEventTypes(new[] {
                    typeof(AccountCreated),
                    typeof(AccountCredited),
                    typeof(AccountDebited)
                });

                _.Events.InlineProjections.AggregateStreamsWith <Account>();
            });

            var khalid = new AccountCreated
            {
                Owner           = "Khalid Abuhakmeh",
                AccountId       = Guid.NewGuid(),
                StartingBalance = 1000m
            };

            var bill = new AccountCreated
            {
                Owner     = "Bill Boga",
                AccountId = Guid.NewGuid()
            };

            using (var session = store.OpenSession())
            {
                // create banking accounts
                session.Events.Append(khalid.AccountId, khalid);
                session.Events.Append(bill.AccountId, bill);

                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                // load khalid's account
                var account = session.Load <Account>(khalid.AccountId);
                // let's be generous
                var amount = 100m;
                var give   = new AccountDebited
                {
                    Amount      = amount,
                    To          = bill.AccountId,
                    From        = khalid.AccountId,
                    Description = "Bill helped me out with some code."
                };

                if (account.HasSufficientFunds(give))
                {
                    session.Events.Append(give.From, give);
                    session.Events.Append(give.To, give.ToCredit());
                }
                // commit these changes
                session.SaveChanges();
            }

            using (var session = store.OpenSession())
            {
                // load bill's account
                var account = session.Load <Account>(bill.AccountId);
                // let's try to over spend
                var amount = 1000m;
                var spend  = new AccountDebited
                {
                    Amount      = amount,
                    From        = bill.AccountId,
                    To          = khalid.AccountId,
                    Description = "Trying to buy that Ferrari"
                };

                if (account.HasSufficientFunds(spend))
                {
                    // should not get here
                    session.Events.Append(spend.From, spend);
                    session.Events.Append(spend.To, spend.ToCredit());
                }
                else
                {
                    session.Events.Append(account.Id, new InvalidOperationAttempted {
                        Description = "Overdraft"
                    });
                }
                // commit these changes
                session.SaveChanges();
            }

            using (var session = store.LightweightSession())
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.DarkYellow;
                Console.WriteLine("----- Final Balance ------");

                var accounts = session.LoadMany <Account>(khalid.AccountId, bill.AccountId);

                foreach (var account in accounts)
                {
                    Console.WriteLine(account);
                }
            }

            using (var session = store.LightweightSession())
            {
                foreach (var account in new[] { khalid, bill })
                {
                    Console.WriteLine();
                    Console.WriteLine($"Transaction ledger for {account.Owner}");
                    var stream = session.Events.FetchStream(account.AccountId);
                    foreach (var item in stream)
                    {
                        Console.WriteLine(item.Data);
                    }
                    Console.WriteLine();
                }
            }

            Console.ReadLine();
        }
 public void Apply(AccountDebited debit)
 {
     debit.Apply(this);
     Console.ForegroundColor = ConsoleColor.Red;
     Console.WriteLine($"Debiting {Owner} ({debit.Amount.ToString("C")}): {debit.Description}");
 }
Beispiel #12
0
 public void Apply(AccountDebited @event)
 {
     AvailableBalance -= @event.Amount;
     OutstandingTransaction.Add(@event.TransactionId, @event.Amount);
 }
Beispiel #13
0
 public void Handle(AccountDebited accountDebited)
 {
     Balance -= accountDebited.Amount;
 }
Beispiel #14
0
 private void When(AccountDebited @event)
 {
     this.Balance = this.Balance - @event.DebitAmount;
 }
Beispiel #15
0
 private void Apply(AccountDebited risedEvent) => Balance -= risedEvent.Amount;
 private void UpdateState(AccountDebited ad)
 {
     this.accountState.Debit(ad.Amount);
     log.Info($"Account debited by {ad.AccountId}, current state: {this.accountState.CurrentAmount}");
 }
Beispiel #17
0
 protected void When(AccountDebited e)
 {
     balance = Money.Amount(balance - Money.Amount(e.Amount));
 }