public Task <Dictionary <AnomalyIdentity, List <ImageFileIdentity> > > GetImageFilesForAnomalies(IEnumerable <AnomalyIdentity> anomalyIdentities) { return(this.ExecuteInContext(async dbContext => { var anomalyGuids = anomalyIdentities.Select(x => x.Value).ToList(); var query = from anomaly in dbContext.Anomalies where anomalyGuids.Contains(anomaly.GUID) join anomalyImage in dbContext.AnomalyToImageFileMappings on anomaly.ID equals anomalyImage.AnomalyID into imageGroup from i in imageGroup.DefaultIfEmpty() select new { anomaly.GUID, ImageIdentity = i == default ? Guid.Empty : i.ImageFileGUID, }; var result = await query.ToListAsync(); // Group it by anomaly (since we need an AnomalyInfo per AnomalyIdentity passed in) var grouped = result.GroupBy( x => AnomalyIdentity.From(x.GUID), x => ImageFileIdentity.From(x.ImageIdentity)); var output = grouped.ToDictionary( grouping => grouping.Key, grouping => grouping .Where(x => !x.IsEmpty()) .ToList()); return output; })); }
public async Task <List <ImageFileIdentity> > GetImageFiles(AnomalyIdentity anomalyIdentity) { var imageFileIdentities = await this.ExecuteInContextAsync(async dbContext => { var imageFileGuids = dbContext.AnomalyToImageFileMappings.Where(x => x.Anomaly.GUID == anomalyIdentity.Value).Select(x => x.ImageFileGUID); var output = await imageFileGuids.Select(x => ImageFileIdentity.From(x)).ToListAsync(); // Execute now. return(output); }); return(imageFileIdentities); }
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); }