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 virtual IActionResult ApiV1UsersGetAccountsByUserId(
            [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 accounts = _accountRepository.GetAllAccountsByUserId(user_id.Value).ToList();

            var models = accounts
                         .Select(ViewModelUtility.CreateAccountOverviewViewModel)
                         .ToList();

            return(Ok(models));
        }
 public string Get(int id)
 {
     try
     {
         var item = _complaintService.FindById(id);
         return(JsonConvert.SerializeObject(ApiResponseUtility.CreateSuccessResponse(item)));
     }
     catch (Exception ex)
     {
         return(JsonConvert.SerializeObject(ApiResponseUtility.CreateErrorResponse(ex)));
     }
 }
Ejemplo n.º 4
0
        public virtual async Task <IActionResult> ApiV1AccountsGetById(
            [FromRoute][Required] int?account_id
            )
        {
            if (!account_id.HasValue)
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.BadRequest,
                           "account_id is required"
                           ));
            }

            var account = _accountRepository.GetAccountById(account_id.Value);

            if (account == null)
            {
                return(ApiResponseUtility.ApiError(
                           HttpStatusCode.NotFound,
                           $"Bank account with id '{account_id}' does not exist"
                           ));
            }

            // when we are fetching account details for a known account, we use the account data service
            // rather than accessing the connection directly. this allows the service to perform caching
            // and additional post-processing on the account data before it is returned to the user
            var accountResult = await _accountDataProvider.GetAccountDetails(account.BankId, account.AccountNumber);

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

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

            var model = ViewModelUtility.CreateAccountDetailsViewModel(account, accountResult.Result);

            return(Ok(model));
        }
Ejemplo n.º 5
0
        public string Login(User user)
        {
            try
            {
                bool isLoggedIn = _accountService.Login(user);
                if (isLoggedIn)
                {
                    return(JsonConvert.SerializeObject(ApiResponseUtility.CreateSuccessResponse("Login Sucessful", isLoggedIn)));
                }

                return(JsonConvert.SerializeObject(ApiResponseUtility.CreateSuccessResponse("Username or Password Incorrect", isLoggedIn)));
            }
            catch (Exception ex)
            {
                return(JsonConvert.SerializeObject(ApiResponseUtility.CreateErrorResponse(ex)));
            }
        }
Ejemplo n.º 6
0
        public void ApiErrorCreatesErrorViewModel()
        {
            const int    statusCode   = (int)HttpStatusCode.InternalServerError;
            const string errorMessage = "An error has occurred";

            var actionResult  = ApiResponseUtility.ApiError((HttpStatusCode)statusCode, errorMessage);
            var contentResult = actionResult as ObjectResult;

            Assert.That(contentResult, Is.Not.Null);
            Assert.That(contentResult.StatusCode, Is.EqualTo(statusCode));

            var errorResult = contentResult.Value as ErrorViewModel;

            Assert.That(errorResult, Is.Not.Null);
            Assert.That(errorResult.Status, Is.EqualTo(statusCode));
            Assert.That(errorResult.Message, Is.EqualTo(errorMessage));
        }
        public string Post(ComplaintModel complaint)
        {
            try
            {
                complaint.ComplaintDate = DateTime.Now;
                bool isCreated = _complaintService.Create(complaint);
                if (isCreated)
                {
                    return(JsonConvert.SerializeObject(ApiResponseUtility.CreateSuccessResponse("Complaint Logged", isCreated)));
                }

                return(JsonConvert.SerializeObject(ApiResponseUtility.CreateSuccessResponse("Complaint Logged Unsucessful", isCreated)));
            }
            catch (Exception ex)
            {
                return(JsonConvert.SerializeObject(ApiResponseUtility.CreateErrorResponse(ex)));
            }
        }
Ejemplo n.º 8
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));
        }