コード例 #1
0
ファイル: UsersApi.cs プロジェクト: Tehl/bank-api
        public virtual async Task <IActionResult> ApiV1UsersCreate(
            [FromBody] CreateUserViewModel user_data
            )
        {
            if (user_data == null ||
                string.IsNullOrEmpty(user_data.Username) ||
                string.IsNullOrEmpty(user_data.BankId) ||
                string.IsNullOrEmpty(user_data.AccountNumber)
                )
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.BadRequest,
                           "All user_data fields must be supplied"
                           ));
            }

            if (!Validation.AccountNumberIsValid(user_data.AccountNumber))
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.BadRequest,
                           $"Account number '{user_data.AccountNumber}' is invalid"
                           ));
            }

            if (!_connectionManager.GetRegisteredBankIds().Contains(user_data.BankId))
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.BadRequest,
                           $"Banking service '{user_data.BankId}' is not available"
                           ));
            }

            var existingUser = _userRepository.GetUserByUsername(user_data.Username);

            if (existingUser != null)
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.Conflict,
                           $"User '{user_data.Username}' already exists"
                           ));
            }

            var existingAccount = _accountRepository.GetAccountByBankIdAndAccountNumber(
                user_data.BankId,
                user_data.AccountNumber
                );

            if (existingAccount != null)
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.Conflict,
                           $"Bank account '{user_data.AccountNumber}' is already registered"
                           ));
            }

            // when we are registering a new account, we go directly to the bank connection to verify the account,
            // as it will not yet exist in our system
            var bankConnection = _connectionManager.CreateConnection(user_data.BankId);
            var accountResult  = await bankConnection.GetAccountDetails(user_data.AccountNumber);

            if (!accountResult.Success)
            {
                var error = new ErrorViewModel
                {
                    Status    = accountResult.StatusCode,
                    Message   = accountResult.Error.ErrorMessage,
                    ErrorCode = accountResult.Error.ErrorCode
                };

                if (accountResult.StatusCode == (int)HttpStatusCode.NotFound)
                {
                    error.Message =
                        $"Account number '{user_data.AccountNumber}' does not exist at bank {user_data.BankId}";
                }

                return(new ObjectResult(error)
                {
                    StatusCode = accountResult.StatusCode
                });
            }

            var appUser = _userRepository.CreateUser(user_data.Username);

            _accountRepository.CreateAccount(appUser.Id, user_data.BankId, user_data.AccountNumber);

            var model = ViewModelUtility.CreateUserViewModel(appUser);

            return(Ok(model));
        }
コード例 #2
0
        /// <summary>
        ///     Gets account details for the specified account number
        /// </summary>
        /// <param name="bankId">Id of the bank which operates the specified account</param>
        /// <param name="accountNumber">Account number to retrieve account details for</param>
        /// <returns>OperationResult instance describing the outcome of the query</returns>
        public Task <OperationResult <AccountDetails> > GetAccountDetails(string bankId, string accountNumber)
        {
            var connection = _connectionManager.CreateConnection(bankId);

            return(connection.GetAccountDetails(accountNumber));
        }