Ejemplo n.º 1
0
        public async void GetForeignExchangeRates_Not_Found_Result()
        {
            var getForeignExchangeRateRequest = new GetForeignExchangeRateRequest {
                Base = "TRY", Date = "20200615"
            };

            _mediator.Setup(x => x.Send(It.IsAny <GetForeignExchangeRateRequest>(), new CancellationToken()))
            .ReturnsAsync(new GetForeignExchangeRateResponse());

            var currencyController = new CurrencyController(_mediator.Object);

            var result = await currencyController.GetForeignExchangeRates(getForeignExchangeRateRequest);

            Assert.IsType <NotFoundObjectResult>(result);
        }
        public async Task <IActionResult> GetForeignExchangeRates
        (
            [ModelBinder(BinderType = typeof(GetForeignExchangeRateRequestBinder))]
            GetForeignExchangeRateRequest request
        )
        {
            var response = await _mediator.Send(request);

            if (response.HasError)
            {
                return(BadRequest(response));
            }

            if (response.Data == null || response.Data.Rates == null || response.Data.Rates.Count == 0)
            {
                return(NotFound(response));
            }

            return(Ok(response.Data));
        }
        public Task BindModelAsync(ModelBindingContext bindingContext)
        {
            string       baseValue = string.Empty;
            StringValues baseValueQuery;

            if (bindingContext.HttpContext.Request.Query.TryGetValue(AppConstants.QueryStringKeyBase, out baseValueQuery) && baseValueQuery.Any())
            {
                baseValue = baseValueQuery.FirstOrDefault();
            }

            if (string.IsNullOrWhiteSpace(baseValue))
            {
                baseValue = AppConstants.CurrencyCodeTry;
            }

            baseValue = baseValue.ToEnglishUpper();

            string       dateValue = string.Empty;
            StringValues dateValueQuery;

            if (bindingContext.HttpContext.Request.Query.TryGetValue(AppConstants.QueryStringKeyDate, out dateValueQuery) && dateValueQuery.Any())
            {
                dateValue = dateValueQuery.FirstOrDefault();
            }

            if (string.IsNullOrWhiteSpace(dateValue))
            {
                dateValue = DateTime.Now.ToString(AppConstants.DateFormat);
            }

            var result = new GetForeignExchangeRateRequest
            {
                Base = baseValue,
                Date = dateValue,
            };

            bindingContext.Result = ModelBindingResult.Success(result);

            return(Task.CompletedTask);
        }
Ejemplo n.º 4
0
        public async void GetForeignExchangeRates_Success_Result()
        {
            var getForeignExchangeRateRequest = new GetForeignExchangeRateRequest {
                Base = "TRY", Date = "20200613"
            };

            _mediator.Setup(x => x.Send(It.IsAny <GetForeignExchangeRateRequest>(), new CancellationToken()))
            .ReturnsAsync(new GetForeignExchangeRateResponse {
                Errors = new List <string> {
                },
                Data   = new Model.InverseRatesDto {
                    Base  = getForeignExchangeRateRequest.Base,
                    Rates = new Dictionary <string, string> {
                        { "USD", "0.14641048" }
                    }
                }
            });

            var currencyController = new CurrencyController(_mediator.Object);

            var result = await currencyController.GetForeignExchangeRates(getForeignExchangeRateRequest);

            Assert.IsType <OkObjectResult>(result);
        }