Beispiel #1
0
        public async Task <GetWeatherDto> GetWeatherAsync(string city, string units, string sortColumn = null, SortOrder sortOrder = SortOrder.Ascending)
        {
            SortingCriteria sorting = null;

            if (!string.IsNullOrWhiteSpace(sortColumn))
            {
                sorting = new SortingCriteria {
                    ColumnName = sortColumn, SortOrder = sortOrder
                }
            }
            ;

            // Create two async calls
            var currentWeatherTask  = _weatherService.GetCurrentWeatherAsync(city, units);
            var forecastWeatherTask = _weatherService.GetDayAverageWeatherForecastAsync(city, units);

            // Wait until all calls will be completed
            var currentWeather  = await currentWeatherTask;
            var forecastWeather = await forecastWeatherTask;

            if (sorting != null)
            {
                forecastWeather = forecastWeather.OrderByCriteria(sorting);
            }

            var dto = new GetWeatherDto
            {
                Current       = _mapper.Map <WeatherItem, WeatherItemDto>(currentWeather),
                ForecastItems = _mapper.Map <IEnumerable <WeatherItem>, IEnumerable <WeatherItemDto> >(forecastWeather)
            };

            return(dto);
        }
    }
Beispiel #2
0
        public async Task <WeatherDto[]> GetWeatherAsync(GetWeatherDto dto)
        {
            var weathers = await _manager.GetForecastAsync(dto.City, dto.Metrics.UnitsMap(),
                                                           Enum.Parse <Langs>(dto.Lang), dto.SortBy);

            return(weathers
                   .GroupBy(x => x.Date.Day)
                   .Select(x => x.First(xx => xx.Date.TimeOfDay >= TimeSpan.FromHours(12)))
                   .Select(x => new WeatherDto
            {
                City = x.City,
                Date = x.Date.ToString("dd MMM yyyy"),
                Wind = x.Wind,
                Clouds = x.Clouds,
                Temperature = x.Temperature,
                TempMin = x.TempMin,
                TempMax = x.TempMax,
                Pressure = x.Pressure,
                Humidity = x.Humidity,
                Description = x.Description
            })
                   .ToArray());
        }