public async Task <IActionResult> Edit(int id) { var day = await dayRepository.GetByIdAsync(id); var locations = (await locationRepository.GetAllASync()).OrderBy(l => l.Label); ViewBag.LocationOptions = locations.Select(x => new SelectListItem { Text = x.Label, Value = x.Id.ToString(), Selected = x.Id == day.LocationId }).ToList(); return(View(day)); }
private async Task SaveLocationPings(List <Ping> pings) { var coords = utility.GetCoordinates(pings); var locations = await locationRepository.GetByCoordinates(coords.Latitude, coords.Longitude); var knownLocation = locations.Any(); var lastPing = await pingRepository.GetLastPing(true); Day day = null; Location location = locations.Select(k => k.Key).FirstOrDefault();; if (!knownLocation || !(location is Location)) { location = new Location() { Label = string.Format("Onbekend {0}", DateTime.Now.ToShortDateString()), Latitude = coords.Latitude, Longitude = coords.Longitude }; locationRepository.Insert(location); await locationRepository.SaveAsync(); } // Lookup if this matches with the last day record if (lastPing.LocationId == location.Id && lastPing.DayId != null) { day = await dayRepository.GetByIdAsync(lastPing.DayId); } // If not create a new day if (!(day is Day)) { day = new Day() { TimeFrom = pings[0].Time, TimeTo = pings[pings.Count - 1].Time, LocationId = location.Id }; dayRepository.Insert(day); await dayRepository.SaveAsync(); } else { day.TimeTo = pings[pings.Count - 1].Time; dayRepository.Update(day); await dayRepository.SaveAsync(); } // Bind the pings to the location & day record foreach (var ping in pings) { ping.LocationId = location.Id; ping.RideId = null; ping.Processed = 1; ping.DayId = day.Id; pingRepository.Update(ping); } await pingRepository.SaveAsync(); cache.ClearCache(); }