コード例 #1
0
        public async Task <WeatherForecast> GetForecast(double latitude, double longitude, int days)
        {
            UriBuilder builder = new UriBuilder(_baseEndpoint)
            {
                Path  = $"data/2.5/onecall",
                Query = $"lat={latitude.ToString(CultureInfo.InvariantCulture)}&lon={longitude.ToString(CultureInfo.InvariantCulture)}&exclude=current,minutely,hourly,alerts&lang={_lang}&units={_unit}&appid={_serviceKey}"
            };

            OpenWeatherMapForecastDTO weatherResponse = await _requestService.GetAsync <OpenWeatherMapForecastDTO>(builder.Uri);

            OpenWeatherMapMapper openWeatherMapMapper = new OpenWeatherMapMapper();
            WeatherForecast      weatherForecast      = openWeatherMapMapper.ToDomainEntities(weatherResponse, _runtimeContext.CityName);

            var indexOfTodaysWeahterItem = weatherForecast.WeatherList.FindIndex(x => x.Date.Date == DateTime.Now.Date);

            weatherForecast.WeatherList = weatherForecast.WeatherList.GetRange(indexOfTodaysWeahterItem, days);

            return(weatherForecast);
        }
コード例 #2
0
        public WeatherForecast ToDomainEntities(OpenWeatherMapForecastDTO openWeatherMapForecastDTO, string cityName)
        {
            List <Weather> weatherList = new List <Weather>();

            foreach (var forecastWeather in openWeatherMapForecastDTO.daily)
            {
                weatherList.Add(ConvertForecastModel(forecastWeather));
            }

            var city = new City
            {
                Name      = cityName,
                Latitude  = openWeatherMapForecastDTO.lat,
                Longitude = openWeatherMapForecastDTO.lon
            };

            return(new WeatherForecast {
                City = city, WeatherList = weatherList
            });
        }