async Task <EntityLabel> EntityLabelCreated(EntityLabel entityLabel) { if (entityLabel == null) { throw new ArgumentNullException(nameof(entityLabel)); } if (entityLabel.EntityId <= 0) { return(entityLabel); } if (entityLabel.LabelId <= 0) { return(entityLabel); } // Get label var label = await _labelStore.GetByIdAsync(entityLabel.LabelId); // No label found no further work needed if (label == null) { return(entityLabel); } // Get total count for entities labeled with this label var entityLabels = await _entityLabelStore.QueryAsync() .Take(1) .Select <EntityLabelQueryParams>(q => { q.LabelId.Equals(label.Id); q.HidePrivate.True(); q.HideHidden.True(); q.HideSpam.True(); q.HideDeleted.True(); }) .ToList(); // Update label label.TotalEntities = entityLabels?.Total ?? 0; label.LastEntityDate = DateTimeOffset.UtcNow; // Persist label updates await _labelManager.UpdateAsync(label); return(entityLabel); }
async Task <IDictionary <int, IList <Label> > > BuildLookUpTable(IEnumerable <Label> labels) { // Get topic index view model from context var viewModel = _actionContextAccessor.ActionContext.HttpContext.Items[typeof(EntityIndexViewModel <Article>)] as EntityIndexViewModel <Article>; if (viewModel == null) { return(null); } // We need results if (viewModel.Results == null) { return(null); } // Get all entities for our current view var entities = viewModel.Results; // Get all entity label relationships for displayed entities IPagedResults <EntityLabel> entityLabels = null; if (entities?.Data != null) { entityLabels = await _entityLabelStore.QueryAsync() .Take(int.MaxValue, false) .Select <EntityLabelQueryParams>(q => { q.EntityId.IsIn(entities.Data.Select(e => e.Id).ToArray()); }) .ToList(); } // Build a dictionary of entity and label relationships var output = new ConcurrentDictionary <int, IList <Label> >(); if (entityLabels?.Data != null) { var labelList = labels.ToList(); foreach (var entityLabel in entityLabels.Data) { var label = labelList.FirstOrDefault(l => l.Id == entityLabel.LabelId); if (label != null) { output.AddOrUpdate(entityLabel.EntityId, new List <Label>() { label }, (k, v) => { v.Add(label); return(v); }); } } } return(output); }
async Task <IDictionary <int, IList <Label> > > BuildLookUpTable(IEnumerable <Label> labels) { // Get topic index view model from context var viewModel = _actionContextAccessor.ActionContext.HttpContext.Items[typeof(EntityIndexViewModel <Topic>)] as EntityIndexViewModel <Topic>; if (viewModel == null) { return(null); } // Get all entities for our current view var entities = await _entityService .ConfigureQuery(async q => { // Hide private? if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, Permissions.ViewPrivateTopics)) { q.HidePrivate.True(); } // Hide hidden? if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, Permissions.ViewHiddenTopics)) { q.HideHidden.True(); } // Hide spam? if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, Permissions.ViewSpamTopics)) { q.HideSpam.True(); } // Hide deleted? if (!await _authorizationService.AuthorizeAsync(_httpContextAccessor.HttpContext.User, Permissions.ViewDeletedTopics)) { q.HideDeleted.True(); } }) .GetResultsAsync(viewModel?.Options, viewModel?.Pager); // Get all entity label relationships for displayed entities IPagedResults <EntityLabel> entityLabels = null; if (entities?.Data != null) { entityLabels = await _entityLabelStore.QueryAsync() .Select <EntityLabelQueryParams>(q => { q.EntityId.IsIn(entities.Data.Select(e => e.Id).ToArray()); }) .ToList(); } // Build a dictionary of entity and label relationships var output = new ConcurrentDictionary <int, IList <Label> >(); if (entityLabels?.Data != null) { var labelList = labels.ToList(); foreach (var entityLabel in entityLabels.Data) { var label = labelList.FirstOrDefault(l => l.Id == entityLabel.LabelId); if (label != null) { output.AddOrUpdate(entityLabel.EntityId, new List <Label>() { label }, (k, v) => { v.Add(label); return(v); }); } } } return(output); }