Exemple #1
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));
        }