Пример #1
0
        public static List <Forcast> GetForcast(System.Security.Principal.IPrincipal User)
        {
            ApplicationDbContext context = new ApplicationDbContext();
            DateTime             today   = DateTime.Now;
            string          todayDate    = today.Year.ToString() + "-" + today.Month.ToString() + "-" + today.Day;
            ApplicationUser user         = UserRetriever.RetrieveUser(User, context);

            if (context.Forcasts.ToList().Count > 0)
            {
                if (context.Forcasts.First().Date == todayDate && context.Forcasts.First().User.Id == user.Id)
                {
                    return(context.Forcasts.ToList());
                }
                else
                {
                    string userName         = User.Identity.Name;
                    string convertedAddress = ConvertAddressToSearch.ConvertAddress(userName, context);
                    var    geoLocation      = ApiCalls.GeoLocationApiUserAddress(userName, context, convertedAddress);
                    string paramater        = "/" + geoLocation[0] + "/" + geoLocation[1];
                    ApiCalls.WeatherApi(paramater, User, context);
                    return(context.Forcasts.ToList());
                }
            }
            else
            {
                string userName         = User.Identity.Name;
                string convertedAddress = ConvertAddressToSearch.ConvertAddress(userName, context);
                var    geoLocation      = ApiCalls.GeoLocationApiUserAddress(userName, context, convertedAddress);
                string paramater        = "/" + geoLocation[0] + "/" + geoLocation[1];
                ApiCalls.WeatherApi(paramater, User, context);
                //make api call to update DB
                return(context.Forcasts.ToList());
            }
        }
Пример #2
0
        public static void WeatherApi(string location, System.Security.Principal.IPrincipal User, ApplicationDbContext context)
        {
            List <string> paramaters = new List <string>();

            paramaters.Add("daily-high-temperature");
            paramaters.Add("daily-low-temperature");
            paramaters.Add("daily-precipitation");
            List <seriesItem> allData  = new List <seriesItem>();
            List <seriesItem> DayOne   = new List <seriesItem>();
            List <seriesItem> DayTwo   = new List <seriesItem>();
            List <seriesItem> DayThree = new List <seriesItem>();
            List <seriesItem> DayFour  = new List <seriesItem>();
            List <seriesItem> DayFive  = new List <seriesItem>();

            //find a way to enter peramater for search
            foreach (var paramater in paramaters)
            {
                var client  = new RestClient("https://insight.api.wdtinc.com/" + paramater + location);
                var request = new RestRequest(Method.GET);
                request.AddHeader("postman-token", "586e3421-1f96-a0fd-f4d7-5af711fba4e3");
                request.AddHeader("cache-control", "no-cache");
                request.AddHeader("authorization", "Basic NjhmODM5MGE6ZDBmNTc2ZjM3MjYxZTk4NWUzZmE4YTk3YTZlMDUyNTg=");
                IRestResponse <WeatherData> response = client.Execute <WeatherData>(request);

                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    var listTempData = response.Data.series;
                    foreach (var item in listTempData)
                    {
                        allData.Add(item);
                    }
                }
            }
            foreach (var item in allData)
            {
                if (item.validDate == allData[0].validDate)
                {
                    DayOne.Add(item);
                }
                else if (item.validDate == allData[1].validDate)
                {
                    DayTwo.Add(item);
                }
                else if (item.validDate == allData[2].validDate)
                {
                    DayThree.Add(item);
                }
                else if (item.validDate == allData[3].validDate)
                {
                    DayFour.Add(item);
                }

                else if (item.validDate == allData[4].validDate)
                {
                    DayFive.Add(item);
                }
            }
            for (int i = 0; i < 5; i++)
            {
                try
                {
                    var day = (from data in context.Forcasts where data.ID == i + 1 select data).First();
                    day.Date = allData[i].validDate;
                    if (i == 0)
                    {
                        day.Date          = DayOne[0].validDate;
                        day.HighTemp      = float.Parse(DayOne[0].value);
                        day.LowTemp       = float.Parse(DayOne[1].value);
                        day.Percipitation = float.Parse(DayOne[2].value);
                        day.User          = UserRetriever.RetrieveUser(User, context);
                    }
                    else if (i == 1)
                    {
                        day.Date          = DayTwo[0].validDate;
                        day.HighTemp      = float.Parse(DayTwo[0].value);
                        day.LowTemp       = float.Parse(DayTwo[1].value);
                        day.Percipitation = float.Parse(DayTwo[2].value);
                        day.User          = UserRetriever.RetrieveUser(User, context);
                    }
                    else if (i == 2)
                    {
                        day.Date          = DayThree[0].validDate;
                        day.HighTemp      = float.Parse(DayThree[0].value);
                        day.LowTemp       = float.Parse(DayThree[1].value);
                        day.Percipitation = float.Parse(DayThree[2].value);
                        day.User          = UserRetriever.RetrieveUser(User, context);
                    }
                    else if (i == 3)
                    {
                        day.Date          = DayFour[0].validDate;
                        day.HighTemp      = float.Parse(DayFour[0].value);
                        day.LowTemp       = float.Parse(DayFour[1].value);
                        day.Percipitation = float.Parse(DayFour[2].value);
                        day.User          = UserRetriever.RetrieveUser(User, context);
                    }
                    else if (i == 4)
                    {
                        day.Date          = DayFive[0].validDate;
                        day.HighTemp      = float.Parse(DayFive[0].value);
                        day.LowTemp       = float.Parse(DayFive[1].value);
                        day.Percipitation = float.Parse(DayFive[2].value);
                        day.User          = UserRetriever.RetrieveUser(User, context);
                    }
                    context.SaveChanges();
                }
                catch
                {
                    Forcast forcast = new Forcast();
                    forcast.ID   = i + 1;
                    forcast.Date = allData[i].validDate;
                    context.Forcasts.Add(forcast);
                    context.SaveChanges();
                }
            }
        }