public async Task <IActionResult> GetForecastByCity([FromQuery] string cityName, [FromQuery] string countryCode = null, [FromQuery] string unit = null) { var query = new GetForecastByCityQuery(cityName, countryCode, unit); var result = await _mediator.Send(query); return(Ok(result)); }
public async Task <List <WeatherForecastDto> > Handle(GetForecastByCityQuery request, CancellationToken cancellationToken) { var takeCount = int.Parse(_configuration.GetSection("TotalShow").Value); var selectedCity = _context.Cities.FirstOrDefault(_ => _.Name == request.CityName); if (selectedCity == null) { throw new ArgumentNullException(nameof(selectedCity)); } var weatherForecast = await LoadWeatherForecast(selectedCity, takeCount); if (weatherForecast.Count == 0 || weatherForecast.Count < takeCount) { await LoadFromApiAndPutToDb(weatherForecast, selectedCity); weatherForecast = await LoadWeatherForecast(selectedCity, takeCount); } var result = weatherForecast.Select(_ => new WeatherForecastDto { Date = _.Date, Humidity = Math.Round(_.Humidity, 1), Temperature = Math.Round(_.Temperature, 1), WindSpeed = Math.Round(_.WindSpeed, 1) }).ToList(); return(result); }
public void Should_Not_Have_Validation_Error_If_City_Is_Not_Empty() { var validator = new GetForecastByCityValidator(); var model = new GetForecastByCityQuery("Hamburg", null, null); var result = validator.TestValidate(model); result.ShouldNotHaveValidationErrorFor(x => x.City); }
public void Should_Have_Validation_Error_If_City_Is_Null() { var validator = new GetForecastByCityValidator(); var model = new GetForecastByCityQuery(null, null, null); var result = validator.TestValidate(model); result.ShouldHaveValidationErrorFor(x => x.City) .WithErrorMessage("'City' must not be empty.") .WithSeverity(Severity.Error) .WithErrorCode("NotEmptyValidator"); }