Beispiel #1
0
        public async Task <ActionResult> Tracker()
        {
            var watch = new System.Diagnostics.Stopwatch();

            watch.Start();

            SummaryViewModel summary = new SummaryViewModel();
            NewsAPI          newsAPI = new NewsAPI();
            TrackerViewModel tracker = new TrackerViewModel();

            #region HttpClient Config
            // Covid Stats
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseUrl);

                HttpResponseMessage responseMessage = await client.GetAsync("summary");

                if (responseMessage.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api
                    var covidResponse = responseMessage.Content
                                        .ReadAsStringAsync().Result;

                    summary = JsonConvert.DeserializeObject <SummaryViewModel>(covidResponse);
                }
            }
            // South Africa News
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(baseUrlNews);

                HttpResponseMessage responseMessage = await client.GetAsync("v2/top-headlines?country=za&category=health&apiKey=8066df69f5c2435c9a6b0510ea7b16d5");

                if (responseMessage.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api
                    var newsResponse = responseMessage.Content
                                       .ReadAsStringAsync().Result;

                    newsAPI = JsonConvert.DeserializeObject <NewsAPI>(newsResponse);
                }
            }
            #endregion

            NewsViewModel news = new NewsViewModel
            {
                Articles = newsAPI.Articles.ToList()
            };

            tracker.FillTrackerModel(summary, news);

            var latLongTableExists = await summary.CheckDBForLatLongExists(db, geocoder);

            if (latLongTableExists)
            {
                ViewBag.DataPoints = db.LatLongDtos.ToList();
            }

            watch.Stop();
            _ = watch.ElapsedMilliseconds;
            _ = watch.Elapsed;

            return(View(tracker));
        }