Ejemplo n.º 1
0
        public virtual IActionResult ApiV1UsersGetById(
            [FromRoute][Required] int?user_id
            )
        {
            if (!user_id.HasValue)
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.BadRequest,
                           "user_id is required"
                           ));
            }

            var user = _userRepository.GetUserById(user_id.Value);

            if (user == null)
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.NotFound,
                           $"User with id '{user_id}' does not exist"
                           ));
            }

            var model = ViewModelUtility.CreateUserViewModel(user);

            return(Ok(model));
        }
Ejemplo n.º 2
0
        public void CreateUserViewModelReturnsUserData()
        {
            var user = new AppUser {
                Id = 1, Username = "******"
            };

            var model = ViewModelUtility.CreateUserViewModel(user);

            Assert.That(model, Is.Not.Null);
            Assert.That(model.UserId, Is.EqualTo(user.Id));
            Assert.That(model.Username, Is.EqualTo(user.Username));
        }
Ejemplo n.º 3
0
        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));
        }