Exemplo n.º 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);
        }
 public string GetBalance([FromUri] int accountId)
 {
     if (accountId != 0)
     {
         return(_accountService.GetBalance(accountId));
     }
     Log.Error("Model is in-valid for entry GetAccount api/transaction");
     throw WebExceptionFactory.GetBadRequestError("Model is in valid please check your payload");
 }
Exemplo n.º 3
0
 public AccountEntity GetAccount([FromUri] int accountId)
 {
     if (accountId != 0)
     {
         return(_accountService.GetAccount(accountId));
     }
     Log.Error("Model is in-valid for entry GET api/account");
     throw WebExceptionFactory.GetBadRequestError("Invalid account ID");
 }
Exemplo n.º 4
0
        public HttpResponseMessage PostAccount(NewAccountDetailEntity account)
        {
            if (!ModelState.IsValid)
            {
                Log.Error("Model is in-valid for entry POST api/account");
                throw WebExceptionFactory.GetBadRequestError("Model is in valid please check your payload");
            }
            var newAccount = _accountService.CreateAccount(account);

            Log.Info("Account has been created");
            // send response
            return(Request.CreateResponse(HttpStatusCode.Created, newAccount));
        }
 public IHttpActionResult PostLogin([FromBody] AuthenticationEntity auth)
 {
     if (!ModelState.IsValid)
     {
         Log.Error("Model is in-valid for entry POST api/employee");
         throw WebExceptionFactory.GetBadRequestError("Model is in valid check your payload");
     }
     if (!_employeeService.AuthenticateUser(auth.username, auth.password))
     {
         Log.Info($"{auth.username} is not authenticated");
         throw WebExceptionFactory.GetBadRequestError("Model is in valid check your payload");
     }
     Log.Info($"{auth.username} has been authenticated");
     return(Ok());
 }
        public HttpResponseMessage PutTransaction(int id, [FromBody] TransactionEntity transaction)
        {
            if (!ModelState.IsValid)
            {
                Log.Error("Model is in-valid for entry PUT api/account");
                throw WebExceptionFactory.GetBadRequestError("Model is in valid please check your payload");
            }
            if (id != transaction.AccountId)
            {
                Log.Error("Sent Id is not same as account ID");
                throw WebExceptionFactory.GetBadRequestError("Account ID is different");
            }
            var newTransaction = _accountService.ExecuteTransaction(transaction);

            return(Request.CreateResponse(HttpStatusCode.Created, newTransaction));
        }
Exemplo n.º 7
0
        /// <summary>
        ///     Create a Account
        /// </summary>
        /// <param name="account">Account Object</param>
        public AccountEntity CreateAccount(NewAccountDetailEntity account)
        {
            Mapper.CreateMap <Account, AccountEntity>();
            var newAccount = new Account
            {
                Account_Balance = decimal.Parse(account.DepositAmount),
                Customer_Id     = CheckCustomer(account.CustomerId),
                Account_Type    = account.AccountType,
                Account_Number  = GetNextAccountNumber()
            };

            try
            {
                var createdAccount = _instance.DataContext.Account.Add(newAccount);
                Commit();
                Log.Debug($"New account has been created, account number {newAccount.Account_Number}");
                Mapper.CreateMap <Account, AccountEntity>();
                return(Mapper.Map <AccountEntity>(createdAccount));
            }
            catch (InvalidOperationException e)
            {
                const string msg = "Error occured while creating account";
                throw WebExceptionFactory.GetBadRequestError(msg, e);
            }
            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);
            }
        }
Exemplo n.º 8
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);
            }
        }
Exemplo n.º 9
0
 /// <summary>
 ///     GetAccount a account details
 /// </summary>
 /// <param name="accountId">Account ID</param>
 /// <returns></returns>
 public AccountEntity GetAccount(int accountId)
 {
     try
     {
         var account = _instance.DataContext.Account.FirstOrDefault(acc => acc.Account_Id.Equals(accountId));
         Mapper.CreateMap <Account, AccountEntity>();
         return(Mapper.Map <AccountEntity>(account));
     }
     catch (ArgumentNullException ex)
     {
         const string msg = "Error occured while retrieving the account";
         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);
     }
     return(null);
 }
Exemplo n.º 10
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);
            }
        }