Exemplo n.º 1
0
        public async Task <IActionResult> Withdraw(string command, WithdrawlRequest data)
        {
            int  withdrawlamount = 0;
            bool invalid         = false;


            //Custom Amount
            if (command == "amount")
            {
                withdrawlamount = data.Amount;

                //Validation if amount is in multiples of $5 and not zero
                if (withdrawlamount == 0 || withdrawlamount % 5 != 0)
                {
                    data.ErrorMesage.Add("Amount should be greater than zero and in multiples of $5");
                }
            }
            else
            {
                //invalid amount input
                if (!int.TryParse(command, out withdrawlamount))
                {
                    invalid = true;
                }
            }


            if (invalid)
            {
                data.ErrorMesage.Add("Invalid Selection");
            }

            //If no validation errors are raised, proceed with the call.
            if (data.ErrorMesage.Count == 0)
            {
                try
                {
                    string custId   = GetIdentity();
                    var    response = await _service.WithdrawAmountAsync(custId, data.AccountType, withdrawlamount);

                    if (response.Success)
                    {
                        data.SuccessMessage.Add("Transaction Completed. Updated balance: $" + response.Balance);
                        data.Balance = response.Balance.ToString();
                    }
                    else
                    {
                        //Process error message from service
                        data.ErrorMesage.Add(response.ErrorMessage);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                    throw;
                }
            }

            return(View(data));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Withdraw(string accType)
        {
            //validate if account type is specified
            if (string.IsNullOrEmpty(accType))
            {
                _logger.LogWarning("Invalid account name");
                return(Redirect("/"));
            }

            //getting Customer Id from claim
            string custId = GetIdentity();

            //validate if customer ID is available
            if (string.IsNullOrEmpty(custId))
            {
                _logger.LogWarning("Invalid Customer ID");
                return(Redirect("/"));
            }
            else
            {
                //Get available balance
                try
                {
                    var response = await _service.GetBalanceAsync(custId, accType);

                    var viewResponse = new WithdrawlRequest()
                    {
                        AccountType = accType, Amount = 0, Balance = response
                    };

                    if (string.IsNullOrEmpty(response))
                    {
                        viewResponse.InvalidAccount = true;
                        viewResponse.ErrorMesage.Add("Invalid account details or Insufficient balance. Please try again");
                    }

                    //initialize View
                    return(View(viewResponse));
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex.Message);
                    throw;
                }
            }
        }