Exemple #1
0
        /// <summary>
        ///     Check if the customer exsist.
        /// </summary>
        /// <param name="customerId"></param>
        /// <returns>customer id</returns>
        private int CheckCustomer(string customerId)
        {
            int custId;

            try
            {
                custId = int.Parse(customerId);
            }
            catch (Exception ex)
            {
                throw WebExceptionFactory.GetBadRequestError($"Invalid Customer ID {customerId}", ex);
            }
            if (custId == 0)
            {
                throw WebExceptionFactory.GetBadRequestError("Customer ID cannot be zero");
            }
            var customer = _instance.DataContext.Customer.FirstOrDefault(x => x.Customer_Id.Equals(custId));

            if (customer == null) // new customer
            {
                throw WebExceptionFactory.GetNotFoundError(
                          $"Customer with id {customerId} dont exist, please create new customer");
            }
            return(customer.Customer_Id);
        }
Exemple #2
0
        /// <summary>
        ///     GetAccount the balance details
        /// </summary>
        /// <param name="accountId">account id</param>
        /// <returns>balance</returns>
        public string GetBalance(int accountId)
        {
            try
            {
                var account =
                    _instance.DataContext.Account.FirstOrDefault(a => a.Account_Id.Equals(accountId));
                if (account == null)
                {
                    throw WebExceptionFactory.GetNotFoundError("Account not found");
                }

                return(account.Account_Balance.ToString(CultureInfo.InvariantCulture));
            }
            catch (ArgumentNullException ex)
            {
                const string msg = "Error occured while executing a transaction in the account";
                throw WebExceptionFactory.GetBadRequestError(msg, ex);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Log.Error(
                        $"Entity of type \"{eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors:");
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Log.Error($"- Property: \"{ve.PropertyName}\", Error: \"{ve.ErrorMessage}\"");
                    }
                }
                throw WebExceptionFactory.GetBadRequestError("Database error", e);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw WebExceptionFactory.GetServerError(ex.Message);
            }
        }
Exemple #3
0
        /// <summary>
        ///     Execute a transaction
        /// </summary>
        /// <param name="transaction">Transaction object</param>
        public TransactionEntity ExecuteTransaction(TransactionEntity transaction)
        {
            try
            {
                var account =
                    _instance.DataContext.Account.FirstOrDefault(a => a.Account_Id.Equals(transaction.AccountId));
                if (account == null)
                {
                    throw WebExceptionFactory.GetNotFoundError("Account not found");
                }
                if (transaction.IsDeposit)
                {
                    account.Account_Balance += transaction.Amount;
                }
                else
                {
                    if (account.Account_Balance < transaction.Amount)
                    {
                        throw WebExceptionFactory.GetBadRequestError(
                                  "Low availble funds cannot execute the transaction.");
                    }
                    account.Account_Balance -= transaction.Amount;
                }

                // Create a transaction
                var trans = new Transaction
                {
                    Account_Id       = transaction.AccountId,
                    Amount           = transaction.Amount,
                    Transaction_Type = transaction.IsDeposit ? 1 : 2,
                    Message          = transaction.Message,
                    Details          = transaction.Details,
                    Timestamp        = DateTime.Now.ToString("yyyyMMddHHmmssffff")
                };
                var newTransaction = _instance.DataContext.Transaction.Add(trans);
                Log.Info($"new transaction has been created with id {newTransaction.Transaction_Id}");
                Commit();
                Mapper.CreateMap <Transaction, TransactionEntity>();
                return(Mapper.Map <TransactionEntity>(newTransaction));
            }
            catch (ArgumentNullException ex)
            {
                const string msg = "Error occured while executing a transaction in the account";
                throw WebExceptionFactory.GetBadRequestError(msg, ex);
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Log.Error(
                        $"Entity of type \"{eve.Entry.Entity.GetType().Name}\" in state \"{eve.Entry.State}\" has the following validation errors:");
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Log.Error($"- Property: \"{ve.PropertyName}\", Error: \"{ve.ErrorMessage}\"");
                    }
                }
                throw WebExceptionFactory.GetBadRequestError("Database error", e);
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw WebExceptionFactory.GetServerError(ex.Message);
            }
        }