public static WeatherLocatioinModel LoadLocationWeatherData(DateTime now, Location location, WeatherData[] locationData)
        {
            var minDate = now.AddMinutes(-90);
            var maxDate = now.AddMinutes(90);

            // погода на текущее время
            var current = locationData.FirstOrDefault(d => d.Date > minDate && d.Date <= maxDate);

            // погода на ближайшие сутки
            var xxx = current != null ? current.Date : now;
            var day = locationData
                        .Where(d => d.Date > xxx && d.Date <= xxx.AddDays(1))
                        .Where(FilterByHours)
                        .OrderBy(d => d.Date)
                        .Take(3)
                        .ToArray();

            // прогноз на несколько дней
            var forecast = locationData
                        .Where(d => d.Date.Date > now.Date)
                        .GroupBy(d => d.Date.Date)
                        .Take(3);

            return new WeatherLocatioinModel
            {
                LocationId = location.Id,
                LocationName = location.DisplayName,
                Now = CreateNowModel(current),
                Today = day.Select(CreateNowModel).ToArray(),
                Forecast = forecast.Select(CreateDayModel).ToArray()
            };
        }
        public object AddLocation(HttpRequestParams request)
        {
            var displayName = request.GetRequiredString("displayName");
            var query = request.GetRequiredString("query");

            using (var session = Context.OpenSession())
            {
                var location = new Location
                {
                    Id = Guid.NewGuid(),
                    DisplayName = displayName,
                    Query = query
                };

                session.Save(location);
                session.Flush();
            }

            return null;
        }
        private static void UpdateWeatherDataItem(Location location, dynamic forecast, ISession session, Logger logger)
        {
            var list = forecast.list as IEnumerable;

            int count = 0;

            if (list != null)
                foreach (dynamic item in list)
                {
                    var date = DateTimeFromUnixTimestampSeconds((long)item.dt);
                    var dataItem = GetWeatherDataItem(location, date, session);
                    PopulateWeatherDataItem(dataItem, item);
                    session.Flush();

                    count++;
                }

            logger.Info("Updated {0} items", count);
        }
        private static WeatherData GetWeatherDataItem(Location location, DateTime date, ISession session)
        {
            var dataItem = session.Query<WeatherData>().FirstOrDefault(obj => obj.Date == date && obj.Location.Id == location.Id);
            if (dataItem == null)
            {
                dataItem = new WeatherData { Id = Guid.NewGuid(), Date = date, Location = location };
                session.Save(dataItem);
            }

            return dataItem;
        }
 private void UpdateOneLocation(Location location, ISession session)
 {
     try
     {
         var forecast = LoadForecast(location.Query, Logger);
         UpdateWeatherDataItem(location, forecast, session, Logger);
     }
     catch (Exception ex)
     {
         string msg = string.Format("loading error (location {0})", location);
         Logger.Error(ex, msg);
     }
 }