@ -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 )
{
_ t agRepository = tagRepository ;
_ t agDefinitionStore = tagDefinitionStore ;
TagRepository = tagRepository ;
T agDefinitionStore = 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 _ t agRepository. 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 . Any Async( entityType , name , tenantId , cancellationToken ) )
if ( ! await TagDefinitionStore . IsDefinedAsync ( entityType ) )
{
throw new TagAlreadyExist Exception( entityType , nam e ) ;
throw new EntityNotTaggableException ( entityType ) ;
}
if ( ! await _ tagDefinitionStore . IsDefinedAsync ( entityType ) )
if ( await TagRepository . AnyAsync ( entityType , name , tenantId , cancellationToken ) )
{
throw new EntityNotTaggable Exception( entityType ) ;
throw new TagAlreadyExist Exception( 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 _ t agRepository. GetAsync ( id , cancellationToken : cancellationToken ) ;
var entity = await T agRepository. GetAsync ( id , cancellationToken : cancellationToken ) ;
if ( name ! = entity . Name & &
await _ t agRepository. AnyAsync ( entity . EntityType , name , entity . TenantId , cancellationToken ) )
await T agRepository. AnyAsync ( entity . EntityType , name , entity . TenantId , cancellationToken ) )
{
throw new TagAlreadyExistException ( entity . EntityType , name ) ;
}
entity . SetName ( name ) ;
return await _ t agRepository. UpdateAsync ( entity , cancellationToken : cancellationToken ) ;
return await T agRepository. UpdateAsync ( entity , cancellationToken : cancellationToken ) ;
}
public Task < List < TagEntityTypeDefiniton > > GetTagDefinitionsAsync ( CancellationToken cancellationToken = default )
public virtual Task < List < TagEntityTypeDefiniton > > GetTagDefinitionsAsync (
CancellationToken cancellationToken = default )
{
return _ t agDefinitionStore. GetTagEntityTypeDefinitionListAsync ( ) ;
return T agDefinitionStore. GetTagEntityTypeDefinitionListAsync ( ) ;
}
}
}