|
|
|
@ -1,35 +1,27 @@ |
|
|
|
using JetBrains.Annotations; |
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Volo.Abp; |
|
|
|
using Volo.Abp.Domain.Services; |
|
|
|
|
|
|
|
namespace Volo.CmsKit.Tags |
|
|
|
{ |
|
|
|
public class TagManager : DomainService, ITagManager |
|
|
|
{ |
|
|
|
private readonly ITagRepository _tagRepository; |
|
|
|
private readonly ITagDefinitionStore _tagDefinitionStore; |
|
|
|
protected ITagRepository TagRepository { get; } |
|
|
|
protected ITagDefinitionStore TagDefinitionStore { get; } |
|
|
|
|
|
|
|
public TagManager( |
|
|
|
ITagRepository tagRepository, |
|
|
|
ITagDefinitionStore tagDefinitionStore) |
|
|
|
public TagManager(ITagRepository tagRepository, ITagDefinitionStore tagDefinitionStore) |
|
|
|
{ |
|
|
|
_tagRepository = tagRepository; |
|
|
|
_tagDefinitionStore = tagDefinitionStore; |
|
|
|
TagRepository = tagRepository; |
|
|
|
TagDefinitionStore = tagDefinitionStore; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<Tag> GetOrAddAsync( |
|
|
|
[NotNull] string entityType, |
|
|
|
[NotNull] string name, |
|
|
|
Guid? tenantId = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
public virtual async Task<Tag> GetOrAddAsync([NotNull] string entityType, [NotNull] string name, |
|
|
|
Guid? tenantId = null, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var entity = await _tagRepository.FindAsync(entityType, name, tenantId, cancellationToken); |
|
|
|
var entity = await TagRepository.FindAsync(entityType, name, tenantId, cancellationToken); |
|
|
|
|
|
|
|
if (entity == null) |
|
|
|
{ |
|
|
|
@ -39,53 +31,43 @@ namespace Volo.CmsKit.Tags |
|
|
|
return entity; |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<Tag> InsertAsync( |
|
|
|
Guid id, |
|
|
|
[NotNull] string entityType, |
|
|
|
[NotNull] string name, |
|
|
|
Guid? tenantId = null, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
public virtual async Task<Tag> InsertAsync(Guid id, [NotNull] string entityType, [NotNull] string name, |
|
|
|
Guid? tenantId = null, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
if (await _tagRepository.AnyAsync(entityType, name, tenantId, cancellationToken)) |
|
|
|
if (!await TagDefinitionStore.IsDefinedAsync(entityType)) |
|
|
|
{ |
|
|
|
throw new TagAlreadyExistException(entityType, name); |
|
|
|
throw new EntityNotTaggableException(entityType); |
|
|
|
} |
|
|
|
|
|
|
|
if (!await _tagDefinitionStore.IsDefinedAsync(entityType)) |
|
|
|
if (await TagRepository.AnyAsync(entityType, name, tenantId, cancellationToken)) |
|
|
|
{ |
|
|
|
throw new EntityNotTaggableException(entityType); |
|
|
|
throw new TagAlreadyExistException(entityType, name); |
|
|
|
} |
|
|
|
|
|
|
|
return await _tagRepository.InsertAsync( |
|
|
|
new Tag( |
|
|
|
id, |
|
|
|
entityType, |
|
|
|
name, |
|
|
|
tenantId), |
|
|
|
return await TagRepository.InsertAsync(new Tag(id, entityType, name, tenantId), |
|
|
|
cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public async Task<Tag> UpdateAsync( |
|
|
|
Guid id, |
|
|
|
[NotNull] string name, |
|
|
|
public virtual async Task<Tag> UpdateAsync(Guid id, [NotNull] string name, |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var entity = await _tagRepository.GetAsync(id, cancellationToken: cancellationToken); |
|
|
|
var entity = await TagRepository.GetAsync(id, cancellationToken: cancellationToken); |
|
|
|
|
|
|
|
if (name != entity.Name && |
|
|
|
await _tagRepository.AnyAsync(entity.EntityType, name, entity.TenantId, cancellationToken)) |
|
|
|
await TagRepository.AnyAsync(entity.EntityType, name, entity.TenantId, cancellationToken)) |
|
|
|
{ |
|
|
|
throw new TagAlreadyExistException(entity.EntityType, name); |
|
|
|
} |
|
|
|
|
|
|
|
entity.SetName(name); |
|
|
|
|
|
|
|
return await _tagRepository.UpdateAsync(entity, cancellationToken: cancellationToken); |
|
|
|
return await TagRepository.UpdateAsync(entity, cancellationToken: cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync(CancellationToken cancellationToken = default) |
|
|
|
public virtual Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync( |
|
|
|
CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
return _tagDefinitionStore.GetTagEntityTypeDefinitionListAsync(); |
|
|
|
return TagDefinitionStore.GetTagEntityTypeDefinitionListAsync(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |