Ejemplo n.º 1
0
        internal AccountDomainEntity DepositToOwnAccount(int accountId, decimal amount)
        {
            AccountTransactionDomainEntity transaction = null;
            AccountDomainEntity            account     = null;
            bool isDepositOk = false;

            try
            {
                account = GetAccountById(accountId);

                transaction = coreContext.New <AccountTransactionDomainEntity>()
                              .With(null, account, amount, TransactionTypeEnum.AccountItself, TransactionStatusEnum.InProgress, account);

                transaction.Insert();
                isDepositOk = account.Deposit(amount);

                transaction.SetStatus(TransactionStatusEnum.Succeeded);
                transaction.Save();

                var transactionDetail = transaction.CreateTransactionDetail(TransactionDirection.In);
                transactionDetail.Insert();
            }
            catch (Exception ex)
            {
                if (transaction != null && isDepositOk == false)
                {
                    transaction.SetStatus(TransactionStatusEnum.Failed);
                    transaction.Save();
                }

                throw ex;
            }

            return(account);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Does assets transfer from one account to another account with given TransactionType
        /// </summary>
        /// <param name="fromAccount"></param>
        /// <param name="toAccount"></param>
        /// <param name="amount"></param>
        /// <param name="transactionTypeEnum"></param>
        private void TransferAssets(
            AccountDomainEntity fromAccount,
            AccountDomainEntity toAccount,
            decimal amount,
            TransactionTypeEnum transactionTypeEnum)
        {
            AccountTransactionDomainEntity transaction = null;
            bool isWithdrawOk = false;
            bool isDepositOk  = false;

            try
            {
                if (fromAccount.AccountType.AccountTypeKey != toAccount.AccountType.AccountTypeKey)
                {
                    throw new AccountManagementException.DifferentAccountTypesCouldNotTransferAssetsToEachOther(string
                                                                                                                .Format("FromAccountType: {0} - ToAccountType: {1}", fromAccount.AccountType.AccountTypeName, toAccount.AccountType.AccountTypeName));
                }

                transaction = coreContext.New <AccountTransactionDomainEntity>()
                              .With(fromAccount, toAccount, amount, transactionTypeEnum, TransactionStatusEnum.InProgress, null);
                transaction.Insert();

                isWithdrawOk = fromAccount.Withdraw(amount);
                isDepositOk  = toAccount.Deposit(amount);

                transaction.SetStatus(TransactionStatusEnum.Succeeded);
                transaction.Save();

                var transactionDetailIn = transaction.CreateTransactionDetail(TransactionDirection.In);
                transactionDetailIn.Insert();

                var transactionDetailOut = transaction.CreateTransactionDetail(TransactionDirection.Out);
                transactionDetailOut.Insert();
            }
            catch (Exception ex)
            {
                if (transaction != null)
                {
                    if (isWithdrawOk && isDepositOk == false)
                    {
                        fromAccount.Deposit(amount);
                        transaction.SetStatus(TransactionStatusEnum.Failed);

                        transaction.Save();
                    }
                }

                throw ex;
            }
        }