public async Task Handle(DepositMoney depositMoney, IMessageHandlerContext context) { try { log.Info($"DepositMoneyHandler, TransactionId = {depositMoney.TransactionId}"); var nHibernateSession = context.SynchronizedStorageSession.Session(); var bankAccountId = BankAccountId.FromExisting(depositMoney.ToBankAccountId); var toBankAccount = nHibernateSession.Get <BankAccount>(bankAccountId) ?? BankAccount.NonExisting(); if (toBankAccount.DoesNotExist()) { var toBankAccountNotFound = new ToBankAccountNotFound(depositMoney.TransactionId); await context.Publish(toBankAccountNotFound); return; } toBankAccount.Deposit(depositMoney.Amount); toBankAccount.ChangeUpdatedAt(); nHibernateSession.Save(toBankAccount); var moneyDeposited = new MoneyDeposited ( depositMoney.TransactionId, toBankAccount.BankAccountId.Id, depositMoney.Amount, toBankAccount.Balance.Amount ); await context.Publish(moneyDeposited); } catch (Exception ex) { log.Error(ex.Message + " ** " + ex.StackTrace); } }
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); } }
public static BankAccount NonExisting() { DateTime Now = DateTime.Now; BankAccountId bankAccountId = BankAccountId.FromExisting(null); CustomerId customerId = CustomerId.FromExisting(null); return(new BankAccount( bankAccountId, null, null, BankAccountStateId.NULL, Now, Now, customerId)); }
public static Transaction NonExisting() { DateTime Now = DateTime.Now; TransactionId transactionId = TransactionId.FromExisting(null); BankAccountId fromBankAccountId = BankAccountId.FromExisting(null); BankAccountId toBankAccountId = BankAccountId.FromExisting(null); return(new Transaction( transactionId, fromBankAccountId, toBankAccountId, null, TransactionStateId.NULL, Now, Now)); }
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); } }