public IHttpActionResult OpenAccount(NewBankAccountModel newBankAccount)
        {
            var message = new AccountsBl().AddBankAccount(newBankAccount);

            if (message == "Success")
            {
                return(Ok(newBankAccount));
            }
            return(BadRequest(message));
        }
        public string AddBankAccount(NewBankAccountModel newBankAccountModel)
        {
            var accountRepo   = new AccountsRepo();
            var accountNumber = BankAccountNumberGenerator.GetUniqueAccountNumber();

            newBankAccountModel.AccountNumber = accountNumber;
            newBankAccountModel.Iban          = IbanGenerator.GenerateIban(accountNumber);
            newBankAccountModel.Expired       = false;

            var withdrawnAccount = accountRepo.GetWhitdrawnAccount(newBankAccountModel.Username_Fk, newBankAccountModel.WithdrawnAccountNo);

            if (withdrawnAccount == null)
            {
                return("Invalid Withdrawn Account");
            }
            if (newBankAccountModel.FriendlyName.Length <= 0)
            {
                return("A New Bank Account Must Be Given A Friendly Name");
            }
            if (newBankAccountModel.AmountWithdrawn <= 10)
            {
                return("A New Bank Account Must Have Some Funds Withdrawn (10 Minimum)");
            }
            if (!IsSufficientFunds(withdrawnAccount, newBankAccountModel.AmountWithdrawn))
            {
                return("Not Enough Funds (Withdrawn Account)");
            }
            if (newBankAccountModel.Currency_Fk == withdrawnAccount.Currency_Fk)
            {
                withdrawnAccount.Balance    -= newBankAccountModel.AmountWithdrawn;
                newBankAccountModel.Balance += newBankAccountModel.AmountWithdrawn;

                accountRepo.UpdateAccount(withdrawnAccount);
            }
            if (newBankAccountModel.Currency_Fk != withdrawnAccount.Currency_Fk)
            {
                withdrawnAccount.Balance    -= newBankAccountModel.AmountWithdrawn;
                newBankAccountModel.Balance += ConvertCurrency(withdrawnAccount.Currency_Fk, newBankAccountModel.Currency_Fk,
                                                               newBankAccountModel.AmountWithdrawn);

                accountRepo.UpdateAccount(withdrawnAccount);
            }

            newBankAccountModel.DateOpened  = DateTime.Now;
            newBankAccountModel.DateExpired = newBankAccountModel.DateOpened.AddMonths(newBankAccountModel.DurationMonth);

            var newBankAccount = BankAccountModelToEntity(newBankAccountModel);

            accountRepo.AddBankAccount(newBankAccount);
            return("Success");
        }
        public BankAccount BankAccountModelToEntity(NewBankAccountModel newBankAccountModel)
        {
            var bankAccount = new BankAccount
            {
                DateExpired    = newBankAccountModel.DateExpired,
                DateOpened     = newBankAccountModel.DateOpened,
                Currency_Fk    = newBankAccountModel.Currency_Fk,
                AccountNumber  = newBankAccountModel.AccountNumber,
                AccountType_Fk = newBankAccountModel.AccountType_Fk,
                Iban           = newBankAccountModel.Iban,
                Balance        = newBankAccountModel.Balance,
                FriendlyName   = newBankAccountModel.FriendlyName,
                Expired        = newBankAccountModel.Expired,
                Username_Fk    = newBankAccountModel.Username_Fk
            };

            return(bankAccount);
        }