public override void Delete(Location location)
 {
     if (location.EntityState == System.Data.EntityState.Detached)
     {
         this._entities.Locations.Attach(location);
     }
     this._entities.Locations.DeleteObject(location);
 }
Пример #2
0
        public List<Weather> FindWeather(Location location)
        {
            var weather = this._repository
            .QueryWeather()
            .Where(u => u.LocationID == location.LocationID)
            .ToList();

            if (weather == null || weather.Count == 0)
            {
                var webService = new WeatherWebService();
                weather = webService.FindWeather(location);

                location.NextUpdate = webService.NextUpdate;
                this._repository.Update(location);

                foreach (var item in weather)
                {
                    this._repository.Add(item);
                }

                this._repository.Save();
            }

            if (!weather.Any() || location.NextUpdate < DateTime.Now)
            {

                weather.ToList()
                .ForEach(t => this._repository.Delete(t));

                weather.Clear();

                var webService = new WeatherWebService();
                webService.FindWeather(location)
                    .ForEach(t => weather.Add(t));

                location.NextUpdate = webService.NextUpdate;

                foreach (var item in weather)
                {
                    this._repository.Add(item);
                }
                //Krashar här...
                this._repository.Update(location);

                // ...save the changes in the database.
                this._repository.Save();
            }

            return weather.ToList();
        }
        public override void Update(Location location)
        {
            ObjectStateEntry entry;
            if(!this._entities.ObjectStateManager.TryGetObjectStateEntry(location, out entry) ||
                entry.State == System.Data.EntityState.Detached)
            {
                this._entities.Locations.Attach(location);
            }

            if(location.EntityState != System.Data.EntityState.Modified)
            {
                this._entities.ObjectStateManager.ChangeObjectState(location, System.Data.EntityState.Modified);
            }
        }
        public List<Weather> FindWeather(Location location)
        {
            // Load the response from the webservice into an XML document.
            //document = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/scottgu_timeline.xml"));
            string requestUriString = String.Format(@"http://www.yr.no/place/{0}/{1}/{2}/forecast.xml", location.Country, location.County, location.City);
            var document = LoadDocument(requestUriString);

            NextUpdate = DateTime.Parse((from nu in document.Descendants("nextupdate") select nu).SingleOrDefault().Value); ;

            return (from time in document.Descendants("time") // Alla time noder.
                    where Int32.Parse(time.Attribute("period").Value) >= 2 // Där period är större än 2.
                    group time by DateTime.Parse(time.Attribute("from").Value).Date into g // Gruppera varje nodes perioders värde. Sätter in dem i gruppen g.
                    select (from t in g select t).First()) // Så väljs den första i varje par.
                    .Take(5).Select(forecast => WeatherFactory.Create(forecast, location.LocationID)).ToList();
        }
        public ActionResult Weather(Location loc)
        {
            try
            {
                var service = new WeatherService();
                var model = new LocationIndexViewModel();
                model.Lat = loc.Lat;
                model.Lng = loc.Lng;
                model.Weathers = service.FindWeather(loc);

                return View("Weather", model);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(String.Empty, ex.Message);
            }
            return View("Error");
        }
 public override void Add(Location location)
 {
     this._entities.Locations.AddObject(location);
 }
 /// <summary>
 /// Create a new Location object.
 /// </summary>
 /// <param name="locationID">Initial value of the LocationID property.</param>
 /// <param name="city">Initial value of the City property.</param>
 /// <param name="country">Initial value of the Country property.</param>
 public static Location CreateLocation(global::System.Int32 locationID, global::System.String city, global::System.String country)
 {
     Location location = new Location();
     location.LocationID = locationID;
     location.City = city;
     location.Country = country;
     return location;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the Locations EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToLocations(Location location)
 {
     base.AddObject("Locations", location);
 }