|
|
|
@ -1,5 +1,7 @@ |
|
|
|
using JetBrains.Annotations; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Volo.Abp.Domain.Services; |
|
|
|
@ -9,14 +11,20 @@ namespace Volo.CmsKit.Tags |
|
|
|
public class EntityTagManager : DomainService |
|
|
|
{ |
|
|
|
protected IEntityTagRepository EntityTagRepository { get; } |
|
|
|
protected ITagRepository TagRepository { get; } |
|
|
|
protected ITagDefinitionStore TagDefinitionStore { get; } |
|
|
|
protected TagManager TagManager { get; } |
|
|
|
|
|
|
|
public EntityTagManager( |
|
|
|
IEntityTagRepository entityTagRepository, |
|
|
|
ITagDefinitionStore tagDefinitionStore) |
|
|
|
ITagRepository tagRepository, |
|
|
|
ITagDefinitionStore tagDefinitionStore, |
|
|
|
TagManager tagManager) |
|
|
|
{ |
|
|
|
EntityTagRepository = entityTagRepository; |
|
|
|
TagRepository = tagRepository; |
|
|
|
TagDefinitionStore = tagDefinitionStore; |
|
|
|
TagManager = tagManager; |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task<EntityTag> AddTagToEntityAsync( |
|
|
|
@ -45,5 +53,23 @@ namespace Volo.CmsKit.Tags |
|
|
|
var entityTag = await EntityTagRepository.FindAsync(tagId, entityId, tenantId, cancellationToken); |
|
|
|
await EntityTagRepository.DeleteAsync(entityTag, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task SetEntityTagsAsync(string entityType, string entityId, List<string> tags) |
|
|
|
{ |
|
|
|
var existingTags = |
|
|
|
await TagRepository.GetAllRelatedTagsAsync(entityType, entityId); |
|
|
|
|
|
|
|
var deletedTags = existingTags.Where(x => !tags.Contains(x.Name)).ToList(); |
|
|
|
var addedTags = tags.Where(x => !existingTags.Any(a => a.Name == x)); |
|
|
|
|
|
|
|
await EntityTagRepository.DeleteManyAsync(deletedTags.Select(s => s.Id).ToArray()); |
|
|
|
|
|
|
|
foreach (var addedTag in addedTags) |
|
|
|
{ |
|
|
|
var tag = await TagManager.GetOrAddAsync(entityType, addedTag); |
|
|
|
|
|
|
|
await AddTagToEntityAsync(tag.Id, entityType, entityId, CurrentTenant?.Id); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |