Exemplo n.º 1
0
        async Task ValidateModelData(SymbolsPairConfigurationDTO model, IValidationService validationService)
        {
            if (model == null)
            {
                throw new ValidationException("Invalid data");
            }

            if (model.BaseSymbol.ToLower() == model.TargetSymbol.ToLower())
            {
                throw new ValidationException("Such a configuration is not acceptable");
            }

            if (validationService.DoesConfigurationPairExist(model.BaseSymbol, model.TargetSymbol))
            {
                throw new ValidationException("Such a configuration already exists");
            }

            if (!validationService.IsProperBaseSymbol(model.BaseSymbol))
            {
                throw new ValidationException("Incorrect base symbol, only EUR is supported");
            }

            if (!await validationService.DoesSuchASymbolExist(model.TargetSymbol))
            {
                throw new ValidationException($"Incorrect target symbol: {model.TargetSymbol}");
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateSymbolConfiguration(
            [FromRoute] Guid configurationId,
            [FromBody] SymbolsPairConfigurationDTO model,
            [FromServices] IConfigurationService configurationService,
            [FromServices] IValidationService validationService)
        {
            try
            {
                await ValidateModelData(model, validationService);
            }
            catch (ValidationException ex)
            {
                return(BadRequest(ex.Message));
            }

            var response = InvokeMethod(configurationService.UpdateCurrency,
                                        new SymbolConfigurationDTO(configurationId, model.BaseSymbol, model.TargetSymbol));

            if (response is SymbolConfigurationDTO)
            {
                return(NoContent());
            }

            return(response as IActionResult);
        }
Exemplo n.º 3
0
        public async Task <IActionResult> ConfigurePair(
            [FromBody] SymbolsPairConfigurationDTO model,
            [FromServices] IConfigurationService configurationService,
            [FromServices] IValidationService validationService)
        {
            try
            {
                await ValidateModelData(model, validationService);
            }
            catch (ValidationException ex)
            {
                return(BadRequest(ex.Message));
            }

            var dto      = new SymbolConfigurationDTO(model.BaseSymbol, model.TargetSymbol);
            var response = InvokeMethod(configurationService.AddCurrency, dto);

            if (response is SymbolConfigurationDTO)
            {
                var responseDTO = response as SymbolConfigurationDTO;
                return(Created($"api/configuration/{responseDTO.Id}", responseDTO));
            }

            return(response as IActionResult);
        }
        public async Task Invalid_Symbol_Configuration()
        {
            // Arrange
            var configurationRequestURI = "api/configurations";
            var model = new SymbolsPairConfigurationDTO("eurr", "usd");
            var configurationRequest = new HttpRequestMessage(HttpMethod.Post, configurationRequestURI);

            configurationRequest.Content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");;

            // Act
            var response = await Client.SendAsync(configurationRequest);

            // Assert
            Assert.NotNull(response);
            Assert.True(response.StatusCode == System.Net.HttpStatusCode.BadRequest);
        }
        public async Task Get_Currency_Exchange_Average_After_Configuration()
        {
            // Arrange
            var configurationRequestURI = "api/configurations";
            var model = new SymbolsPairConfigurationDTO("eur", "usd");
            var configurationRequest = new HttpRequestMessage(HttpMethod.Post, configurationRequestURI);

            configurationRequest.Content = new StringContent(JsonConvert.SerializeObject(model), Encoding.UTF8, "application/json");;

            var exchangeRequestURI = "/api/exchange-rates/eur/usd/average";

            // Act
            await Client.SendAsync(configurationRequest);

            var response = await Client.GetAsync(exchangeRequestURI);

            // Assert
            Assert.NotNull(response);
            Assert.True(response.StatusCode == System.Net.HttpStatusCode.OK);
            Assert.NotNull(response.Content);
        }