From a32746775253e79aed02a92816831d5dc4d0a538 Mon Sep 17 00:00:00 2001 From: enisn Date: Wed, 24 Feb 2021 16:56:18 +0300 Subject: [PATCH] CmsKit - TagManager & ITagRepository refactoring --- .../Volo/CmsKit/Tags/ITagRepository.cs | 4 ---- .../Volo/CmsKit/Tags/TagManager.cs | 14 ++++---------- .../Volo/CmsKit/Tags/EfCoreTagRepository.cs | 12 +++--------- .../CmsKit/MongoDB/Tags/MongoTagRepository.cs | 16 ++++------------ 4 files changed, 11 insertions(+), 35 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagRepository.cs index 8493fe8401..b45bd30db7 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagRepository.cs @@ -12,25 +12,21 @@ namespace Volo.CmsKit.Tags Task GetAsync( [NotNull] string entityType, [NotNull] string name, - Guid? tenantId = null, CancellationToken cancellationToken = default); Task AnyAsync( [NotNull] string entityType, [NotNull] string name, - Guid? tenantId = null, CancellationToken cancellationToken = default); Task FindAsync( [NotNull] string entityType, [NotNull] string name, - Guid? tenantId = null, CancellationToken cancellationToken = default); Task> GetAllRelatedTagsAsync( [NotNull] string entityType, [NotNull] string entityId, - Guid? tenantId = null, CancellationToken cancellationToken = default); } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs index 0aabd2fba1..5b675a0db9 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs @@ -21,14 +21,8 @@ namespace Volo.CmsKit.Tags public virtual async Task 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 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); } diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs index 7325141c40..a57a7af53f 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs @@ -21,7 +21,6 @@ namespace Volo.CmsKit.Tags public virtual async Task 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 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 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)); } diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs index 5e8fdf94a7..bb35a19075 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs +++ b/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 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 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 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> 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));