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..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 @@ -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,30 @@ public class MongoEntityTagRepository : MongoDbRepository> GetEntityIdsFilteredByTagNameAsync( + [NotNull] string tagName, + [NotNull] string entityType, + [CanBeNull] Guid? tenantId=null, + CancellationToken cancellationToken = default) + { + var dbContext = await GetDbContextAsync(); + var entityTagQueryable = await GetMongoQueryableAsync(GetCancellationToken(cancellationToken)); + 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)); + } } 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); + } }