public async Task Delete(LocationIdentity identity) { await this.ExecuteInContextAsync(async dbContext => { var entity = await dbContext.Locations.Where(x => x.GUID == identity.Value).SingleAsync(); dbContext.Locations.Remove(entity); await dbContext.SaveChangesAsync(); }); }
public async Task SetReportedLocation(AnomalyIdentity anomalyIdentity, LocationIdentity reportedLocation) { await this.ExecuteInContextAsync(async dbContext => { var entity = await dbContext.GetAnomaly(anomalyIdentity).SingleAsync(); entity.ReportedLocationGUID = reportedLocation.Value; await dbContext.SaveChangesAsync(); }); }
public async Task <bool> Exists(LocationIdentity identity) { var exists = await this.ExecuteInContextAsync(async dbContext => { var entity = await dbContext.Locations.Where(x => x.GUID == identity.Value).SingleOrDefaultAsync(); var output = entity is object; return(output); }); return(exists); }
public async Task SetLngLat(LocationIdentity identity, LngLat lngLat) { await this.ExecuteInContextAsync(async dbContext => { var entity = await dbContext.Locations.Where(x => x.GUID == identity.Value).SingleAsync(); entity.Longitude = lngLat.Lng; entity.Latitude = lngLat.Lat; await dbContext.SaveChangesAsync(); }); }
public async Task <LocationIdentity> GetReportedLocation(AnomalyIdentity anomalyIdentity) { var locationIdentity = await this.ExecuteInContext(async dbContext => { var locationIdentityValue = await dbContext.GetAnomaly(anomalyIdentity).Select(x => x.ReportedLocationGUID).SingleOrDefaultAsync(); var output = locationIdentityValue == null ? null : LocationIdentity.From(locationIdentityValue.Value); return(output); }); return(locationIdentity); }
public static AppType ToAppType(this EntityType entityType) { var appType = new AppType() { Identity = LocationIdentity.From(entityType.GUID), LngLat = new LngLat() { Lat = entityType.Latitude.Value, Lng = entityType.Longitude.Value, }, }; return(appType); }
public async Task <LngLat> GetLngLat(LocationIdentity identity) { var lngLat = await this.ExecuteInContextAsync(async dbContext => { var entity = await dbContext.Locations.Where(x => x.GUID == identity.Value).SingleAsync(); var output = new LngLat() { Lng = entity.Longitude.Value, Lat = entity.Latitude.Value, }; return(output); }); return(lngLat); }
public async Task <LocationIdentity> New() { var locationIdentity = LocationIdentity.New(); await this.ExecuteInContextAsync(async dbContext => { var entity = new Entities.Location() { GUID = locationIdentity.Value, }; dbContext.Locations.Add(entity); await dbContext.SaveChangesAsync(); }); return(locationIdentity); }
public async Task <List <AnomalyInfo> > GetAnomalyInfos(List <AnomalyIdentity> anomalyIdentities) { var anomalyGuids = anomalyIdentities.Select(x => x.Value).ToList(); var output = await this.ExecuteInContextAsync(async dbContext => { // Get all the info, in rows. var query = from anomaly in dbContext.Anomalies where anomaly.GUID != null where anomalyGuids.Contains(anomaly.GUID) join anomalyCatchment in dbContext.AnomalyToCatchmentMappings on anomaly.ID equals anomalyCatchment.AnomalyID into catchmentGroup from c in catchmentGroup.DefaultIfEmpty() join anomalyText in dbContext.AnomalyToTextItemMappings on anomaly.ID equals anomalyText.AnomalyID into textGroup from t in textGroup.DefaultIfEmpty() join anomalyImage in dbContext.AnomalyToImageFileMappings on anomaly.ID equals anomalyImage.AnomalyID into imageGroup from i in imageGroup.DefaultIfEmpty() select new { anomaly.ID, anomaly.GUID, anomaly.ReportedUTC, anomaly.ReportedLocationGUID, anomaly.ReporterLocationGUID, CatchmentIdentity = c == default ? Guid.Empty : c.CatchmentIdentity, TextItemIdentity = t == default ? Guid.Empty : t.TextItemGUID, ImageIdentity = i == default ? Guid.Empty : i.ImageFileGUID, }; // Group it by anomaly (since we need an AnomalyInfo per AnomalyIdentity passed in) var result = await query.ToListAsync(); var grouped = result.GroupBy(group => new { group.ID, group.GUID, group.ReportedUTC, group.ReportedLocationGUID, group.ReporterLocationGUID }, group => group); // Use the grouping to put together anomaly infos. var anomalyInfos = new List <AnomalyInfo>(); foreach (var entry in grouped) { // Console.WriteLine(entry.Key); var images = new HashSet <Guid>(); var catchments = new HashSet <Guid>(); var textItemSet = new HashSet <Guid>(); foreach (var thing in entry) { images.Add(thing.ImageIdentity); catchments.Add(thing.CatchmentIdentity); textItemSet.Add(thing.TextItemIdentity); } var imagesList = images.ToList(); var catchmentsList = catchments.ToList(); var textItemsList = textItemSet.ToList(); // Console.WriteLine($" Images ({images.Count}): {string.Join(',',images.ToList())}"); // Console.WriteLine($" Catchments ({catchments.Count}): {string.Join(',', catchments.ToList())}"); // Console.WriteLine($" Text items ({textItemSet.Count}): {string.Join(',', textItemSet.ToList())}"); if (entry.Key.GUID == default) { // Unfortunately this also catches anomalies added with an all-zeros guid... // and we have at least one of those (ID 228 as of 2020-07-17) // throw new Exception("Got an anomaly without a GUID"); } var anomalyIdentity = new AnomalyIdentity(entry.Key.GUID); var reportedUTC = entry.Key.ReportedUTC; var reportedLocation = entry.Key.ReportedLocationGUID.HasValue ? LocationIdentity.From(entry.Key.ReportedLocationGUID.Value) : null; var reporterLocation = entry.Key.ReporterLocationGUID.HasValue ? LocationIdentity.From(entry.Key.ReporterLocationGUID.Value) : null; var catchmentIdentities = catchmentsList.Select(x => CatchmentIdentity.From(x)).ToList(); var imageFileIdentities = imagesList .Where(x => x != Guid.Empty) .Select(x => ImageFileIdentity.From(x)) .ToList(); var textItems = textItemsList.Select(x => TextItemIdentity.From(x)).ToList(); var info = new AnomalyInfo { AnomalyIdentity = anomalyIdentity, ReportedUTC = reportedUTC, ReportedLocationIdentity = reportedLocation, ReporterLocationIdentity = reporterLocation, CatchmentIdentities = catchmentIdentities, ImageFileIdentities = imageFileIdentities, TextItemsIdentities = textItems }; anomalyInfos.Add(info); } return(anomalyInfos); }); return(output); }
public async Task <AnomalyInfo> GetAnomalyInfo(AnomalyIdentity anomalyIdentity) { var anomalyInfo = await this.ExecuteInContextAsync(async dbContext => { var gettingAnomalyDetails = dbContext.GetAnomaly(anomalyIdentity) .Select(x => new { x.ReportedUTC, x.ReportedLocationGUID, x.ReporterLocationGUID, x.UpvotesCount, }) .SingleAsync(); var gettingImageFileIdentityValues = dbContext.GetAnomaly(anomalyIdentity) .Join(dbContext.AnomalyToImageFileMappings, anomaly => anomaly.ID, mapping => mapping.AnomalyID, (_, mapping) => mapping.ImageFileGUID) .ToListAsync(); var gettingTextItemIdentityValues = dbContext.GetAnomaly(anomalyIdentity) .Join(dbContext.AnomalyToTextItemMappings, anomaly => anomaly.ID, mapping => mapping.AnomalyID, (_, mapping) => mapping.TextItemGUID) .ToListAsync(); var gettingCatchmentMapping = dbContext.GetAnomaly(anomalyIdentity) .Join(dbContext.AnomalyToCatchmentMappings, anomaly => anomaly.ID, mapping => mapping.AnomalyID, (_, mapping) => mapping) .ToListAsync(); var anomalyDetails = await gettingAnomalyDetails; var imageFileIdentityValues = await gettingImageFileIdentityValues; var textItemIdentityValues = await gettingTextItemIdentityValues; var catchmentMapping = await gettingCatchmentMapping; var catchmentIdentities = catchmentMapping.Select(x => CatchmentIdentity.From(x.CatchmentIdentity)).ToList(); var imageFileIdentities = imageFileIdentityValues.Select(x => ImageFileIdentity.From(x)).ToList(); var reportedLocation = anomalyDetails.ReportedLocationGUID.HasValue ? LocationIdentity.From(anomalyDetails.ReportedLocationGUID.Value) : null; var reporterLocation = anomalyDetails.ReporterLocationGUID.HasValue ? LocationIdentity.From(anomalyDetails.ReporterLocationGUID.Value) : null; var reportedUTC = anomalyDetails.ReportedUTC; var textItems = textItemIdentityValues.Select(x => TextItemIdentity.From(x)).ToList(); var upvotesCount = anomalyDetails.UpvotesCount; var output = new AnomalyInfo() { AnomalyIdentity = anomalyIdentity, CatchmentIdentities = catchmentIdentities, ImageFileIdentities = imageFileIdentities, ReportedLocationIdentity = reportedLocation, ReporterLocationIdentity = reporterLocation, ReportedUTC = reportedUTC, TextItemsIdentities = textItems, UpvotesCount = upvotesCount, }; return(output); }); return(anomalyInfo); }
public Task SetReporterLocation(AnomalyIdentity anomalyIdentity, LocationIdentity reporterLocation) { throw new NotImplementedException(); }