public async Task Handle(RefundMoney refundMoney, IMessageHandlerContext context)
        {
            try
            {
                log.Info($"RefundMoneyHandler, BankAccountId = {refundMoney.BankAccountId}");
                var nHibernateSession = context.SynchronizedStorageSession.Session();
                var bankAccountId     = BankAccountId.FromExisting(refundMoney.BankAccountId);
                var bankAccount       = nHibernateSession.Get <BankAccount>(bankAccountId) ?? BankAccount.NonExisting();
                if (bankAccount.DoesNotExist())
                {
                    var fromBankAccountNotFound = new FromBankAccountNotFound(refundMoney.BankAccountId);
                    await context.Publish(fromBankAccountNotFound);

                    return;
                }
                bankAccount.Refund(refundMoney.Amount);
                nHibernateSession.Save(bankAccount);
                var moneyRefunded = new MoneyRefunded
                                    (
                    bankAccount.BankAccountId.Id,
                    refundMoney.Amount
                                    );
                await context.Publish(moneyRefunded);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + " ** " + ex.StackTrace);
            }
        }
Ejemplo n.º 2
0
        public async Task Handle(WithdrawMoney withdrawMoney, IMessageHandlerContext context)
        {
            try
            {
                log.Info($"WithdrawMoneyHandler, TransactionId = {withdrawMoney.TransactionId}");
                var nHibernateSession = context.SynchronizedStorageSession.Session();
                var bankAccountId     = BankAccountId.FromExisting(withdrawMoney.FromBankAccountId);
                var fromBankAccount   = nHibernateSession.Get <BankAccount>(bankAccountId) ?? BankAccount.NonExisting();
                if (fromBankAccount.DoesNotExist())
                {
                    var fromBankAccountNotFound = new FromBankAccountNotFound(withdrawMoney.TransactionId);
                    await context.Publish(fromBankAccountNotFound);

                    return;
                }
                if (fromBankAccount.CanBeWithdrawed(withdrawMoney.Amount))
                {
                    fromBankAccount.Withdraw(withdrawMoney.Amount);
                    fromBankAccount.ChangeUpdatedAt();
                    nHibernateSession.Save(fromBankAccount);
                    var moneyWithdrawn = new MoneyWithdrawn
                                         (
                        withdrawMoney.TransactionId,
                        withdrawMoney.FromBankAccountId,
                        withdrawMoney.Amount,
                        fromBankAccount.Balance.Amount
                                         );
                    await context.Publish(moneyWithdrawn);

                    return;
                }
                var withdrawRejected = new WithdrawRejected
                                       (
                    withdrawMoney.TransactionId
                                       );
                await context.Publish(withdrawRejected);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + " ** " + ex.StackTrace);
            }
        }