// Adding new transaction to JSON FILE.
        public string AddTransaction(Models.Transaction.Transaction newTransaction)
        {
            string ID;

            ID = _dataProvider.AddObject <Models.Transaction.Transaction>(newTransaction);
            return(string.IsNullOrEmpty(ID) ? string.Empty : ID);
        }
        public bool UpdateTransaction(Models.Transaction.Transaction transaction)
        {
            bool isUpdate = true;

            using (SqlConnection connection = new SqlConnection(CommonUtility.ConnectionString))
            {
                SqlCommand command = new SqlCommand(StoreProcedure.UPDATETRANSACTIONS, connection);
                command.CommandType = CommandType.StoredProcedure;

                foreach (var transactions in transaction.GetType().GetProperties())
                {
                    string name  = transactions.Name;
                    var    value = transactions.GetValue(transaction, null);
                    command.Parameters.Add(new SqlParameter("@" + name, value == null ? DBNull.Value : value));
                }

                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    isUpdate = false;
                    throw new Exception("Exception Updating Data." + e.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            return(isUpdate);
        }
        public Models.Transaction.Transaction GetTransactionById(long transactionId)
        {
            using (SqlConnection connection = new SqlConnection(CommonUtility.ConnectionString))
            {
                SqlCommand command = new SqlCommand(StoreProcedure.GETTRANSACTIONSBYID, connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@TransactionId", transactionId));

                try
                {
                    connection.Open();
                    SqlDataReader reader = command.ExecuteReader();
                    Models.Transaction.Transaction transaction = new Models.Transaction.Transaction();
                    transaction = UtilityManager.DataReaderMap <Models.Transaction.Transaction>(reader);
                    return(transaction);
                }
                catch (Exception e)
                {
                    throw new Exception("Exception retrieving reviews. " + e.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
Exemple #4
0
        // Service for tranfering
        public string Transfer(string debitingAccountID, string creditingAccountID, float amount)
        {
            Models.Transaction.Transaction newTransaction;
            Account creditingAccount, debitingAccount;
            string  message;
            bool    IsSufficientBalance = IsBalanceSufficient(debitingAccountID, amount);

            creditingAccount = _dataProvider.GetObjectByID <Account>(creditingAccountID);
            if (creditingAccount == null)
            {
                return("creditingAccountID is not valid");
            }
            debitingAccount = _dataProvider.GetObjectByID <Account>(debitingAccountID);
            if (debitingAccount == null)
            {
                return("DebitingAccountID is not valid");
            }
            //Check Wheather sufficient balance in the account.
            if (IsSufficientBalance)
            {
                newTransaction = new Models.Transaction.Transaction()
                {
                    CreatedOn  = DateTime.Now,
                    IsReverted = false,
                    Type       = Models.Transaction.TransactionType.Transfer,
                    Amount     = amount,
                    PayeeID    = creditingAccountID,
                    PayorID    = debitingAccountID
                };

                // Adding Transaction.
                _transactionService.AddTransaction(newTransaction);
                // Updating balance for the credited account.
                creditingAccount.Balance += amount;
                // Updating balance fot the debited account.
                debitingAccount.Balance -= amount;
                // saving those Updated accounts in JSON.
                if (_dataProvider.UpdateObject <Account>(debitingAccount) && _dataProvider.UpdateObject <Account>(creditingAccount))
                {
                    message = "  TransactionID:" + newTransaction.ID + " Balance:" + debitingAccount.Balance;
                }
                else
                {
                    message = "  Transaction Failed";
                }
            }
            else
            {
                message = "  Unsufficient Balance";
            }
            return(message);
        }
Exemple #5
0
        // WithDraw amount from bankaccount.
        public string WithDraw(string accountID, float amount)
        {
            string  message;
            Account account;

            Models.Transaction.Transaction newTransaction;

            account = _dataProvider.GetObjectByID <Account>(accountID);
            if (account == null)
            {
                return("Invalid Account ID");
            }
            // Transaction is stopped if amount is 0 or neagative value.
            if (amount <= 0)
            {
                message = "Enter only possitive value for the amount";
                return(message);
            }
            bool IsSufficientBalance = IsBalanceSufficient(accountID, amount);

            //Check Wheather sufficient balance in the account.
            if (IsSufficientBalance)
            {
                // Creating Transaction.
                newTransaction = new Models.Transaction.Transaction()
                {
                    CreatedOn  = DateTime.Now,
                    IsReverted = false,
                    Type       = Models.Transaction.TransactionType.Withdraw,
                    Amount     = amount,
                    PayorID    = accountID,
                };
                // Adding Transaction
                _transactionService.AddTransaction(newTransaction);
                // Updating balance of the account.
                account.Balance -= amount;
                if (_dataProvider.UpdateObject <Account>(account))
                {
                    message = "  TransactionID:" + newTransaction.ID + " Balance:" + account.Balance;
                }
                else
                {
                    message = "  Transaction Failed";
                }
            }
            else
            {
                message = "  No suffcient balance";
            }
            return(message);
        }
        public long InsertTransaction(Models.Transaction.Transaction transaction)
        {
            long id = 0;

            using (SqlConnection connection = new SqlConnection(CommonUtility.ConnectionString))
            {
                SqlCommand command = new SqlCommand(StoreProcedure.INSERTTRANSACTIONS, connection);
                command.CommandType = CommandType.StoredProcedure;
                SqlParameter returnValue = new SqlParameter("@" + "TransactionId", SqlDbType.Int);
                returnValue.Direction = ParameterDirection.Output;
                command.Parameters.Add(returnValue);
                foreach (var transactions in transaction.GetType().GetProperties())
                {
                    if (transactions.Name != "TransactionId")
                    {
                        string name  = transactions.Name;
                        var    value = transactions.GetValue(transaction, null);

                        command.Parameters.Add(new SqlParameter("@" + name, value == null ? DBNull.Value : value));
                    }
                }
                try
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                    id = (int)command.Parameters["@TransactionId"].Value;
                }
                catch (Exception ex)
                {
                    throw new Exception("Execption Adding Data. " + ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
            return(id);
        }
Exemple #7
0
        // Depositing amount to account.
        public string Deposit(string accountID, float amount)
        {
            string message;

            Models.Transaction.Transaction newTransaction;
            Account account;

            if (amount <= 0)
            {
                message = "  Enter only possitive value for the amount";
                return(message);
            }
            // Creating Transaction.
            newTransaction = new Models.Transaction.Transaction()
            {
                CreatedOn  = DateTime.Now,
                IsReverted = false,
                Type       = Models.Transaction.TransactionType.Deposit,
                Amount     = amount,
                PayeeID    = accountID,
            };

            // Adding Transaction
            _transactionService.AddTransaction(newTransaction);
            // Updating balance of the account.
            account          = _dataProvider.GetObjectByID <Account>(accountID);
            account.Balance += amount;
            if (_dataProvider.UpdateObject <Account>(account))
            {
                message = "  TransactionID:" + newTransaction.ID + " Balance:" + account.Balance;
            }
            else
            {
                message = "  Transaction Failed";
            }
            return(message);
        }