public ActionResult <string> Add([FromBody] WeatherForecast weatherForecast)
        {
            _context.Add(weatherForecast);
            _context.SaveChanges();

            return(JsonSerializer.Serialize(weatherForecast));
        }
        public void SetSelectedId(int id)
        {
            var user = GetSingleUser();

            user.SelectedId           = id;
            context.Entry(user).State = EntityState.Modified;
            context.SaveChanges();
        }
Exemplo n.º 3
0
        private async Task LoadFromApiAndPutToDb(List <WeatherForecastData> weatherForecast, City city)
        {
            var data = await _weatherForecast.LoadByCityName(city.Name);

            var groupedData = data
                              .GroupBy(_ => _.Date.Date)
                              .ToDictionary(_ => _.Key, _ => _.ToList())
                              .Select(_ => new WeatherForecastData
            {
                Date        = _.Key,
                Temperature = _.Value.Average(_ => _.Temperature),
                Humidity    = _.Value.Average(_ => _.Humidity),
                WindSpeed   = _.Value.Average(_ => _.WindSpeed)
            });

            var maxDate = weatherForecast.Count == 0 ? DateTime.MinValue : weatherForecast.Max(_ => _.Date.Date);

            var newWeatherForecast = groupedData.Where(_ => _.Date.Date > maxDate).ToList();

            _context.WeatherForecasts.AddRange(newWeatherForecast.Select(_ => new WeatherForecastData
            {
                City        = city,
                Date        = _.Date,
                Humidity    = _.Humidity,
                Temperature = _.Temperature,
                WindSpeed   = _.WindSpeed
            }));

            _context.SaveChanges();
        }
Exemplo n.º 4
0
        public async System.Threading.Tasks.Task <IActionResult> IndexAsync([FromBody] WeatherForecastModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(model));
            }
            Debug.WriteLine("Redirecting to success action");
            model.Date = DateTime.Now;
            Db.Add(model);
            Db.SaveChanges();

            await _hubContext.Clients.All.SendAsync("ReceiveUpdate",
                                                    model.Date,
                                                    model.TemperatureC,
                                                    model.Humidity,
                                                    model.Pressure,
                                                    model.Summary);

            return(Ok(model));
        }
Exemplo n.º 5
0
        public void UpdateAllCityes()
        {
            _context = new WeatherForecastContext();
            var     currentUrl = URL + REGION;
            HtmlWeb web        = new HtmlWeb();
            var     doc        = web.Load(currentUrl);
            //_logger.LogInformation("КОдировка" + doc.Encoding);
            var TownElements = doc.DocumentNode.SelectNodes("//li[@class='city-block']");

            foreach (var TownElement in TownElements)
            {
                var    temp     = TownElement.SelectSingleNode("a");
                String cityName = temp.InnerText;
                String link     = temp.Attributes["href"].Value;

                City city = _context.Citys
                            .Where(t => t.Name == cityName)
                            .Include(t => t.WeatherForecasts)
                            .FirstOrDefault();

                _logger.LogInformation($"Get weather by {cityName}");
                if (city == null)
                {
                    city                  = new City();
                    city.Name             = cityName;
                    city.WeatherForecasts = new List <WeatherForecast>();
                    setWheatherForecast(link, city);
                    _context.Citys.Add(city);
                }
                else
                {
                    city.WeatherForecasts.Clear();
                    _context.SaveChanges();
                    setWheatherForecast(link, city);
                }
                _context.SaveChanges();
            }
        }
        public WeatherForecastController(WeatherForecastContext context)
        {
            _context = context;

            if (_context.WeatherForecast.Count() == 0)
            {
                _context.WeatherForecast.Add(new WeatherForecast
                {
                    Date         = DateTime.Now,
                    TemperatureC = 15,
                    TemperatureF = 59,
                    Summary      = "Initial"
                });

                _context.SaveChanges();
            }
        }
 public IActionResult Post()
 {
     using (var db = new WeatherForecastContext())
     {
         var rng      = new Random();
         var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast
         {
             Date         = DateTime.Now.AddDays(index),
             TemperatureC = rng.Next(-20, 55),
             Summary      = Summaries[rng.Next(Summaries.Length)]
         });
         foreach (var i in forecast)
         {
             db.WeatherForecasts?.Add(i);
         }
         db.SaveChanges();
     }
     return(new AcceptedResult());
 }
Exemplo n.º 8
0
        public static void Main(string[] args)
        {
            #region Enforce database
            using (var db = new WeatherForecastContext())
            {
                db.Database.EnsureCreated();

                if (db.WeatherForecasts.Count() == 0)
                {
                    string[] Summaries = new []
                    {
                        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
                    };

                    var rng = new Random();

                    for (int i = 1; i <= 20; ++i)
                    {
                        WeatherForecast wf = new WeatherForecast
                        {
                            Id           = i,
                            Date         = DateTime.Today.AddDays(i),
                            TemperatureC = rng.Next(-20, 55),
                            Summary      = Summaries[rng.Next(Summaries.Length)]
                        };

                        db.WeatherForecasts.Add(wf);
                    }

                    db.SaveChanges();
                }
            }
            #endregion

            CreateWebHostBuilder(args).Build().Run();
        }
 public void Add(WeatherForecastEntity entity)
 {
     _weatherForecastContext.WeatherForecastLog.Add(entity);
     _weatherForecastContext.SaveChanges();
 }