Example #1
0
        public static BoolResultDto ExternalTransfer(AccountActionDto accountAction)
        {
            var result = new BoolResultDto();

            using (var connection = DbContext.SimpleDbConnection())
            {
                connection.Open();
                var queryBalance       = "select Balance from BankAccounts where BankAccountNumber  = @BankId";
                var queryBalanceResult = connection.Query <int>(queryBalance, new { BankId = accountAction.SourceBankNumber });
                if (queryBalanceResult.First() > accountAction.Amount && accountAction.Amount != 0)
                {
                    using (var transactionScope = connection.BeginTransaction())
                    {
                        var        data   = Newtonsoft.Json.JsonConvert.SerializeObject(accountAction);
                        HttpClient client = new HttpClient();
                        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", ConfigurationManager.AppSettings["BasicAuthenticationUserName"], ConfigurationManager.AppSettings["BasicAuthenticationPassword"]))));
                        var content = new StringContent(data, Encoding.UTF8, "application/json");
                        HttpResponseMessage response = client.PostAsync(accountAction.Url, content).Result;
                        if (!response.IsSuccessStatusCode)
                        {
                            throw new Exception(response.ReasonPhrase);
                        }
                        else
                        {
                            var query              = "update BankAccounts set Balance = (Balance - @amount) where BankAccountNumber = @bankId;select BankAccountId from BankAccounts where BankAccountNumber = @bankId";
                            var insertAction       = "insert into AccountActions values(null,@actionType, @title, @sourceName, @destName, @sourceNumber, @destNumber, @amount);select last_insert_rowid()";
                            var insertHistory      = "insert into BankAccountHistory values(null,@actionId, @bankId, (select Balance from BankAccounts where BankAccountNumber = @bankNumber), @date);select last_insert_rowid()";
                            var updateResult       = connection.Execute(query, new { bankId = accountAction.DestinationBankNumber, amount = accountAction.Amount });
                            var insertActionResult = connection.Query <int>(insertAction, new
                            {
                                actionType   = (int)accountAction.ActionType,
                                title        = accountAction.Title,
                                sourceName   = accountAction.SourceName,
                                destName     = accountAction.DestinationName,
                                sourceNumber = accountAction.SourceBankNumber,
                                destNumber   = accountAction.DestinationBankNumber,
                                amount       = accountAction.Amount
                            }).AsList();
                            var insertSelfHistory = connection.Query <int>(insertHistory, new { actionId = insertActionResult.First(), bankId = query.First(), bankNumber = accountAction.DestinationBankNumber, date = DateTime.Now });
                            if (insertSelfHistory.First() != 0)
                            {
                                transactionScope.Commit();
                                result.SetSuccess(true);
                            }
                            else
                            {
                                transactionScope.Rollback();
                            }
                        }
                    }
                    connection.Close();
                    return(result);
                }
                else
                {
                    result.SetErrors("Invalid value");
                    return(result);
                }
            }
        }
Example #2
0
        public BoolResultDto Deposit(AccountActionDto action)
        {
            var result = new BoolResultDto();

            try
            {
                if (Validation.ValidateNrb(action.DestinationBankNumber))
                {
                    action.ActionType = BSRBankingDataContract.Enums.eActionType.Deposit;
                    var transferResult = BankAccount.Deposit(action);
                    if (transferResult.Success())
                    {
                        result = transferResult;
                    }
                }
                else
                {
                    result.SetErrors("Account number is invalid");
                }
            }
            catch (Exception ex)
            {
                result.SetErrors(ex);
            }
            return(result);
        }
Example #3
0
        public BoolResultDto Transfer(AccountActionDto accountAction)
        {
            var result = new BoolResultDto();

            try
            {
                if (Validation.ValidateNrb(accountAction.DestinationBankNumber))
                {
                    var transferType = BankAccount.CheckTransferType(accountAction.DestinationBankNumber);
                    if (transferType.Success() && transferType.Data == eActionType.InternalTransfer)
                    {
                        accountAction.ActionType = eActionType.InternalTransfer;
                        var transferResult = BankAccount.InternalTransfer(accountAction);
                        if (transferResult.Success())
                        {
                            result.SetSuccess(transferResult.Data);
                        }
                        else
                        {
                            result.SetErrors(transferResult.Result.ExceptionMessage);
                        }
                    }
                    else if (transferType.Success() && transferType.Data == eActionType.ExternalTranser)
                    {
                        var externalValidation = BankAccount.CheckExternalAccount(accountAction.DestinationBankNumber);
                        if (externalValidation.Success())
                        {
                            accountAction.Url        = externalValidation.Data;
                            accountAction.ActionType = eActionType.ExternalTranser;
                            var transferResult = BankAccount.ExternalTransfer(accountAction);
                            if (transferResult.Success())
                            {
                                result.SetSuccess(transferResult.Data);
                            }
                            else
                            {
                                result.SetErrors(transferResult.Result.ExceptionMessage);
                            }
                        }
                        else
                        {
                            result.SetErrors(externalValidation.Result.ExceptionMessage);
                        }
                    }
                }
                else
                {
                    result.SetErrors("Account number is invalid");
                }
            }
            catch (Exception ex)
            {
                result.SetErrors(ex);
            }
            return(result);
        }
Example #4
0
 public static BoolResultDto InternalTransfer(AccountActionDto accountAction)
 {
     using (var connection = DbContext.SimpleDbConnection())
     {
         var result = new BoolResultDto();
         connection.Open();
         var queryBalance       = "select Balance from BankAccounts where BankAccountNumber  = @BankId";
         var queryBalanceResult = connection.Query <int>(queryBalance, new { BankId = accountAction.SourceBankNumber });
         if (queryBalanceResult.First() > accountAction.Amount && accountAction.Amount != 0)
         {
             using (var transactionScope = connection.BeginTransaction())
             {
                 var querySelf          = "update BankAccounts set Balance = (Balance - @amount) where BankAccountNumber = @bankId;select BankAccountId from BankAccounts where BankAccountNumber = @bankId";
                 var queryDest          = "update BankAccounts set Balance = (Balance + @amount)  where BankAccountNumber = @bankId;select BankAccountId from  BankAccounts where BankAccountNumber = @bankId";
                 var insertAction       = "insert into AccountActions values(null,@actionType, @title, @sourceName, @destName, @sourceNumber, @destNumber, @amount);select last_insert_rowid()";
                 var insertHistory      = "insert into BankAccountHistory values(null,@actionId, @bankId, (select Balance from BankAccounts where BankAccountNumber = @bankNumber), @date);select last_insert_rowid()";
                 var updateSelfResult   = connection.Query <int>(querySelf, new { bankId = accountAction.SourceBankNumber, amount = accountAction.Amount });
                 var updateDestResult   = connection.Query <int>(queryDest, new { bankId = accountAction.DestinationBankNumber, amount = accountAction.Amount });
                 var insertActionResult = connection.Query <int>(insertAction, new
                 {
                     actionType   = (int)accountAction.ActionType,
                     title        = accountAction.Title,
                     sourceName   = accountAction.SourceName,
                     destName     = accountAction.DestinationName,
                     sourceNumber = accountAction.SourceBankNumber,
                     destNumber   = accountAction.DestinationBankNumber,
                     amount       = accountAction.Amount
                 }).AsList();
                 var insertSelfHistory = connection.Query <int>(insertHistory, new { actionId = insertActionResult.First(), bankId = updateSelfResult.First(), bankNumber = accountAction.SourceBankNumber, date = DateTime.Now });
                 var insertDestHistory = connection.Query <int>(insertHistory, new { actionId = insertActionResult.First(), bankId = updateDestResult.First(), bankNumber = accountAction.DestinationBankNumber, date = DateTime.Now });
                 if (insertDestHistory.First() != 0 && insertSelfHistory.First() != 0)
                 {
                     transactionScope.Commit();
                     result.SetSuccess(true);
                 }
                 else
                 {
                     transactionScope.Rollback();
                     result.SetErrors("");
                 }
                 connection.Close();
                 return(result);
             }
         }
         else
         {
             result.SetErrors("Invalid amount");
             return(result);
         }
     }
 }
Example #5
0
        public IHttpActionResult Post(string accountNumber, [FromBody] object input)
        {
            try
            {
                AccountActionDto dto = JsonConvert.DeserializeObject <AccountActionDto>(input.ToString());
                dto.DestinationBankNumber = accountNumber;
                dto.ActionType            = eActionType.ExternalTranser;
                var validationResult = Validation.ValidateNrb(accountNumber);
                var validateSource   = Validation.ValidateNrb(dto.SourceBankNumber);
                if (!(validationResult && validateSource))
                {
                    var error = new ErrorModel("Account number invalid", "AccountNumber");
                    return(BadRequest(JsonConvert.SerializeObject(error)));
                }
                if (dto.DestinationName == null || dto.DestinationName == string.Empty)
                {
                    var error = new ErrorModel("Destination name cannot be empty", "DestinationName");
                    return(BadRequest(JsonConvert.SerializeObject(error)));
                }
                if (dto.SourceName == null || dto.SourceName == string.Empty)
                {
                    var error = new ErrorModel("Source name cannot be empty", "SourceName");
                    return(BadRequest(JsonConvert.SerializeObject(error)));
                }
                if (dto.Amount < 0)
                {
                    var error = new ErrorModel("Amount cannot be lower than 0", "Amount");
                    return(BadRequest(JsonConvert.SerializeObject(error)));
                }
                if (dto.Title == null || dto.Title == string.Empty)
                {
                    var error = new ErrorModel("Title cannot be empty", "Title");
                    return(BadRequest(JsonConvert.SerializeObject(error)));
                }

                var result = BankAccount.ReceiveTransfer(dto);
                if (result.Success())
                {
                    return(Created <AccountActionDto>("", null));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
            return(BadRequest("Unexpected error"));
        }