Exemple #1
0
        static Functions()
        {
            var ctx = new WeatherForecastContext();

            _repo       = new WeatherForecastRepository(ctx);
            _weatherSvc = new AccuweatherWeatherSerivce(CloudConfigurationManager.GetSetting("AccuweatherWeatherService:ApiKey"));
        }
 public WeatherForecastController(
     ILogger <WeatherForecastController> logger,
     WeatherForecastContext dbContext)
 {
     _logger        = logger;
     this.dbContext = dbContext;
 }
Exemple #3
0
 public WeatherForecastService(HttpClient httpClient, IConfiguration configuration, WeatherForecastContext context)
 {
     _httpClient    = httpClient;
     _configuration = configuration;
     _context       = context;
     _apiUri        = _configuration.GetSection("WeatherAPIUri").Value;
     _apiKey        = _configuration.GetSection("WeatherAPIKey").Value;
 }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddDbContext <WeatherForecastContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            WeatherForecastContext weatherForecastContext = new WeatherForecastContext(this.Configuration);

            weatherForecastContext.EnsureCreated();
        }
        public void Setup()
        {
            System.Diagnostics.Debugger.Launch();

            _weatherForecast = LoadResource.LoadWeatherForecast(_weatherForecast);

            _context = new WeatherForecastContext();

            _context.WeatherForecast = DbSetExtension.GetQueryableMockDbSet(_weatherForecast);

            _controller = new WeatherForecastController(_context);
        }
        private readonly int updateInterval; //update interval (minutes)

        public WeatherService(WeatherForecastContext cotext, IOptions <WeatherServiceSettings> settings)
        {
            this.context         = cotext;
            this.httpDataService = new HttpDataService(settings.Value.Appid);
            this.updateInterval  = settings.Value.UpdateInterval;

            var config = new AutoMapper.MapperConfiguration(c =>
            {
                c.AddProfile(new ApplicationProfile());
            });

            mapper = config.CreateMapper();
        }
        public ActionResult <IEnumerable <WeatherForecast> > WeatherForecasts()
        {
            var rng = new Random();

            FaultyResultType Occasion = (FaultyResultType)(rng.Next(Enum.GetNames(typeof(FaultyResultType)).Length));

            switch (Occasion)
            {
            case FaultyResultType.Ok:
                // okay
                WeatherForecast[] Result;
                using (var db = new WeatherForecastContext())
                {
                    Result = db.WeatherForecasts
                             .OrderBy(wf => wf.Date)
                             .Skip(rng.Next(10))
                             .Take(6)
                             .Select(wf => new WeatherForecast
                    {
                        DateFormatted = wf.Date.ToString("d"),
                        TemperatureC  = wf.TemperatureC,
                        Summary       = wf.Summary
                    })
                             .ToArray()
                    ;
                }

                return(Ok(Result));

            case FaultyResultType.Long:
                // long
                System.Threading.Thread.Sleep(4000);
                goto case FaultyResultType.Ok;

            case FaultyResultType.TooLong:
                // way too long
                System.Threading.Thread.Sleep(10000);
                goto case FaultyResultType.Ok;

            case FaultyResultType.Error500:
                // server fault
                return(StatusCode(500));

            case FaultyResultType.Garbage:
                // garbage response
                return(StatusCode(200, "Qb5VfjkQ9dPVj42dprhN"));
            }

            throw new Exception("Faulty math");
        }
        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());
 }
        public IEnumerable <WeatherForecast> Get()
        {
            _logger.LogInformation("test");
            // var rng = new Random();
            // return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            //     {
            //         Date = DateTime.Now.AddDays(index),
            //         TemperatureC = rng.Next(-20, 55),
            //         Summary = Summaries[rng.Next(Summaries.Length)]
            //     })
            //     .ToArray();
            //Post();

            using (var db = new WeatherForecastContext())
            {
                return((db.WeatherForecasts ?? throw new NullReferenceException()).ToList());
                //return projects;
            }
        }
Exemple #11
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();
            }
        }
Exemple #12
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 WeatherForecastRepository(WeatherForecastContext weatherForecastContext)
 {
     _weatherForecastContext = weatherForecastContext;
 }
 public RequestRepository(WeatherForecastContext context) : base(context)
 {
 }
Exemple #15
0
 public GetForecastGeneralInfoHandler(WeatherForecastContext context, IWeatherForecast weatherForecast, IConfiguration configuration)
 {
     _context         = context;
     _weatherForecast = weatherForecast;
     _configuration   = configuration;
 }
 public WeatherForecastRepository(WeatherForecastContext ctx)
 {
     this._ctx = ctx;
 }
 public WeatherForecastController(ILogger <WeatherForecastController> logger)
 {
     _logger  = logger;
     _context = new WeatherForecastContext();
 }
Exemple #18
0
        public WeatherForecast Get()
        {
            WeatherForecastContext db = new WeatherForecastContext();

            return(db.weatherforecast.SingleOrDefault(w => w.id == 1));
        }
 public WeatherForecastController(WeatherForecastContext weatherForecastContext,
                                  ILogger <WeatherForecastController> logger)
 {
     this.weatherForecastContext = weatherForecastContext;
     _logger = logger;
 }
 public WeatherForecastController(WeatherForecastContext context)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
 }
Exemple #21
0
 public CityRepository(WeatherForecastContext context) : base(context)
 {
 }
Exemple #22
0
 public GetForecastByCityHandler(WeatherForecastContext context, IWeatherForecast weatherForecast, IConfiguration configuration)
 {
     _context         = context;
     _weatherForecast = weatherForecast;
     _configuration   = configuration;
 }