Browse Source

CmsKit - TagManager & ITagRepository refactoring

pull/7848/head
enisn 6 years ago
parent
commit
a327467752
  1. 4
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagRepository.cs
  2. 14
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs
  3. 12
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs
  4. 16
      modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs

4
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagRepository.cs

@ -12,25 +12,21 @@ namespace Volo.CmsKit.Tags
Task<Tag> GetAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
Task<bool> AnyAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
Task<Tag> FindAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
Task<List<Tag>> GetAllRelatedTagsAsync(
[NotNull] string entityType,
[NotNull] string entityId,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
}
}

14
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs

@ -21,14 +21,8 @@ namespace Volo.CmsKit.Tags
public virtual async Task<Tag> GetOrAddAsync([NotNull] string entityType, [NotNull] string name)
{
var entity = await TagRepository.FindAsync(entityType, name, CurrentTenant.Id);
if (entity == null)
{
entity = await InsertAsync(GuidGenerator.Create(), entityType, name);
}
return entity;
return await TagRepository.FindAsync(entityType, name)
?? await InsertAsync(GuidGenerator.Create(), entityType, name);
}
public virtual async Task<Tag> InsertAsync(Guid id,
@ -40,7 +34,7 @@ namespace Volo.CmsKit.Tags
throw new EntityNotTaggableException(entityType);
}
if (await TagRepository.AnyAsync(entityType, name, CurrentTenant.Id))
if (await TagRepository.AnyAsync(entityType, name))
{
throw new TagAlreadyExistException(entityType, name);
}
@ -57,7 +51,7 @@ namespace Volo.CmsKit.Tags
var entity = await TagRepository.GetAsync(id);
if (name != entity.Name &&
await TagRepository.AnyAsync(entity.EntityType, name, entity.TenantId))
await TagRepository.AnyAsync(entity.EntityType, name))
{
throw new TagAlreadyExistException(entity.EntityType, name);
}

12
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs

@ -21,7 +21,6 @@ namespace Volo.CmsKit.Tags
public virtual async Task<bool> AnyAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
@ -29,15 +28,13 @@ namespace Volo.CmsKit.Tags
return await (await GetDbSetAsync()).AnyAsync(x =>
x.EntityType == entityType &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
GetCancellationToken(cancellationToken));
}
public virtual Task<Tag> GetAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
@ -45,15 +42,13 @@ namespace Volo.CmsKit.Tags
return GetAsync(x =>
x.EntityType == entityType &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
cancellationToken: GetCancellationToken(cancellationToken));
}
public virtual Task<Tag> FindAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
@ -61,8 +56,7 @@ namespace Volo.CmsKit.Tags
return FindAsync(x =>
x.EntityType == entityType &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
cancellationToken: GetCancellationToken(cancellationToken));
}

16
modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs

@ -22,7 +22,6 @@ namespace Volo.CmsKit.MongoDB.Tags
public async Task<bool> AnyAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
@ -31,28 +30,24 @@ namespace Volo.CmsKit.MongoDB.Tags
return await (await GetMongoQueryableAsync(cancellationToken))
.AnyAsync(x =>
x.EntityType == entityType &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
GetCancellationToken(cancellationToken));
}
public Task<Tag> GetAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
return GetAsync(x =>
x.EntityType == entityType &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
cancellationToken: GetCancellationToken(cancellationToken));
}
public Task<Tag> FindAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
@ -60,29 +55,26 @@ namespace Volo.CmsKit.MongoDB.Tags
return FindAsync(x =>
x.EntityType == entityType &&
x.Name == name &&
x.TenantId == tenantId,
x.Name == name,
cancellationToken: GetCancellationToken(cancellationToken));
}
public virtual async Task<List<Tag>> GetAllRelatedTagsAsync(
[NotNull] string entityType,
[NotNull] string entityId,
Guid? tenantId = null,
CancellationToken cancellationToken = default)
{
Check.NotNullOrEmpty(entityType, nameof(entityType));
Check.NotNullOrEmpty(entityId, nameof(entityId));
var entityTagIds = await (await GetDbContextAsync(cancellationToken)).EntityTags.AsQueryable()
.Where(q => q.EntityId == entityId && q.TenantId == tenantId)
.Where(q => q.EntityId == entityId)
.Select(q => q.TagId)
.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));
var query = (await GetMongoQueryableAsync(cancellationToken))
.Where(x =>
x.EntityType == entityType &&
x.TenantId == tenantId &&
entityTagIds.Contains(x.Id));
var result = await query.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));

Loading…
Cancel
Save