From 0e6fb3a202f3a1e321def7580355bd058040ef09 Mon Sep 17 00:00:00 2001 From: Salih Date: Thu, 4 Aug 2022 11:34:31 +0300 Subject: [PATCH 1/4] Added tag search by tag name --- .../Volo/CmsKit/Tags/TagConsts.cs | 4 ++-- .../Volo/CmsKit/Tags/EntityTagManager.cs | 8 ++++++++ .../Volo/CmsKit/Tags/IEntityTagRepository.cs | 6 ++++++ .../CmsKit/Tags/EfCoreEntityTagRepository.cs | 18 +++++++++++++++++ .../MongoDB/Tags/MongoEntityTagRepository.cs | 20 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs index c8ebcecf47..55a31991da 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs @@ -4,7 +4,7 @@ namespace Volo.CmsKit.Tags; public static class TagConsts { - public static int MaxEntityTypeLength { get; set; } = CmsEntityConsts.MaxEntityTypeLength; + public static int MaxEntityTypeLength = CmsEntityConsts.MaxEntityTypeLength; - public static int MaxNameLength { get; set; } = 32; + public const int MaxNameLength = 32; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs index b33d337a06..2707b8854c 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs @@ -79,4 +79,12 @@ public class EntityTagManager : DomainService { return await EntityTagRepository.GetEntityIdsFilteredByTagAsync(tagId, tenantId, cancellationToken); } + public async Task> GetEntityIdsFilteredByTagNameAsync( + [NotNull] string tagName, + [NotNull] string entityType, + [CanBeNull] Guid? tenantId, + CancellationToken cancellationToken = default) + { + return await EntityTagRepository.GetEntityIdsFilteredByTagNameAsync(tagName, entityType,tenantId, cancellationToken); + } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/IEntityTagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/IEntityTagRepository.cs index a96402267d..5105635c82 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/IEntityTagRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/IEntityTagRepository.cs @@ -21,4 +21,10 @@ public interface IEntityTagRepository : IBasicRepository [NotNull] Guid tagId, [CanBeNull] Guid? tenantId, CancellationToken cancellationToken = default); + + Task> GetEntityIdsFilteredByTagNameAsync( + [NotNull] string tagName, + [NotNull] string entityType, + [CanBeNull] Guid? tenantId=null, + CancellationToken cancellationToken=default); } diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreEntityTagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreEntityTagRepository.cs index fe7e622841..c9e501edc0 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreEntityTagRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreEntityTagRepository.cs @@ -52,4 +52,22 @@ public class EfCoreEntityTagRepository : EfCoreRepository q.EntityId) .ToListAsync(cancellationToken: GetCancellationToken(cancellationToken)); } + + public async Task> GetEntityIdsFilteredByTagNameAsync( + [NotNull] string tagName, + [NotNull] string entityType, + [CanBeNull] Guid? tenantId=null, + CancellationToken cancellationToken=default) + { + var dbContext = await GetDbContextAsync(); + var result = from et in dbContext.Set() + join t in dbContext.Set() on et.TagId equals t.Id + where t.Name == tagName + && t.EntityType == entityType + && et.TenantId == tenantId + && t.TenantId == tenantId + && !t.IsDeleted + select et.EntityId; + return await result.ToListAsync(cancellationToken:GetCancellationToken(cancellationToken)); + } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs index 82952c9bf5..b4a3d25941 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs @@ -9,6 +9,7 @@ using Volo.Abp; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; using Volo.CmsKit.Tags; +using Tag = Volo.CmsKit.Tags.Tag; namespace Volo.CmsKit.MongoDB.Tags; @@ -52,4 +53,23 @@ public class MongoEntityTagRepository : MongoDbRepository> GetEntityIdsFilteredByTagNameAsync( + [NotNull] string tagName, + [NotNull] string entityType, + [CanBeNull] Guid? tenantId=null, + CancellationToken cancellationToken = default) + { + var entityTagQueryable = await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)); + var tagQueryable = await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)); + var resultQueryable = from et in entityTagQueryable + join t in tagQueryable on et.TagId equals t.Id + where t.Name == tagName + && t.EntityType == entityType + && et.TenantId == tenantId + && t.TenantId == tenantId + && !t.IsDeleted + select et.EntityId; + return await AsyncExecuter.ToListAsync(resultQueryable, GetCancellationToken(cancellationToken)); + } } From 8b80fbdbbafb23bcae98be1a7d03223ffc258043 Mon Sep 17 00:00:00 2001 From: Salih Date: Thu, 4 Aug 2022 12:04:37 +0300 Subject: [PATCH 2/4] Update TagConsts.cs --- .../Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs index 55a31991da..c8ebcecf47 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Tags/TagConsts.cs @@ -4,7 +4,7 @@ namespace Volo.CmsKit.Tags; public static class TagConsts { - public static int MaxEntityTypeLength = CmsEntityConsts.MaxEntityTypeLength; + public static int MaxEntityTypeLength { get; set; } = CmsEntityConsts.MaxEntityTypeLength; - public const int MaxNameLength = 32; + public static int MaxNameLength { get; set; } = 32; } From 26d646ffcb6c2e52956c1e265d76595484b98b68 Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Thu, 4 Aug 2022 12:04:38 +0300 Subject: [PATCH 3/4] Add test to EntityTagRepositories --- .../Tags/EntityTagRepository_Test.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/Tags/EntityTagRepository_Test.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/Tags/EntityTagRepository_Test.cs index 9da9dd7710..c200fd01f3 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/Tags/EntityTagRepository_Test.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/Tags/EntityTagRepository_Test.cs @@ -1,9 +1,6 @@ -using Shouldly; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; +using System.Linq; using System.Threading.Tasks; +using Shouldly; using Volo.Abp.Modularity; using Xunit; @@ -34,4 +31,15 @@ public abstract class EntityTagRepository_Test : CmsKitTestBase< relatedTags.ShouldBeEmpty(); } + + [Fact] + public async Task GetEntityIdsFilteredByTagNameAsync_ShouldWorkProperly() + { + var entityIds = await _entityTagRepository.GetEntityIdsFilteredByTagNameAsync(_cmsKitTestData.TagName_1, _cmsKitTestData.EntityType1); + + entityIds.ShouldNotBeNull(); + entityIds.ShouldNotBeEmpty(); + + entityIds.Count.ShouldBe(1); + } } From ab666e8e41f73644752580470dfbe0f33562250d Mon Sep 17 00:00:00 2001 From: Enis Necipoglu Date: Thu, 4 Aug 2022 12:04:44 +0300 Subject: [PATCH 4/4] Fix mongodb query --- .../MongoDB/Tags/MongoEntityTagRepository.cs | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs index b4a3d25941..e3835bfe42 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoEntityTagRepository.cs @@ -60,16 +60,23 @@ public class MongoEntityTagRepository : MongoDbRepository(GetCancellationToken(cancellationToken)); - var resultQueryable = from et in entityTagQueryable - join t in tagQueryable on et.TagId equals t.Id - where t.Name == tagName - && t.EntityType == entityType - && et.TenantId == tenantId - && t.TenantId == tenantId - && !t.IsDeleted - select et.EntityId; + var tagQueryable = dbContext.Tags.AsQueryable(); + + var resultQueryable = entityTagQueryable + .Join( + tagQueryable, + o => o.TagId, + i => i.Id, + (entityTag, tag) => new { entityTag, tag }) + .Where(x => x.tag.EntityType == entityType + && x.entityTag.TenantId == tenantId + && x.tag.TenantId == tenantId + && x.tag.IsDeleted == false + ) + .Select(s => s.entityTag.EntityId); + return await AsyncExecuter.ToListAsync(resultQueryable, GetCancellationToken(cancellationToken)); } }