Browse Source

Merge pull request #7848 from abpframework/cms-kit/tag-media-refactorings

CMS Kit - Tag and media refactorings
pull/7868/head
İlkay İlknur 6 years ago
committed by GitHub
parent
commit
336271d316
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/EntityTagAdminAppService.cs
  2. 28
      modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/TagAdminAppService.cs
  3. 3
      modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Tags/TagAppService.cs
  4. 2
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/MediaDescriptors/MediaDescriptor.cs
  5. 4
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/MediaDescriptors/MediaDescriptorCheck.cs
  6. 2
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/CmsKitTagOptions.cs
  7. 2
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs
  8. 12
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityNotTaggableException.cs
  9. 30
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs
  10. 24
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/IEntityTagManager.cs
  11. 2
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagDefinitionStore.cs
  12. 33
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagManager.cs
  13. 4
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagRepository.cs
  14. 11
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagEntityTypeDefinitionDictionary.cs
  15. 8
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagEntityTypeDefinitions.cs
  16. 51
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagManager.cs
  17. 16
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Tags/EfCoreTagRepository.cs
  18. 16
      modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Tags/MongoTagRepository.cs
  19. 10
      modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs
  20. 54
      modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/EntityTagManager_Tests.cs
  21. 28
      modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs
  22. 35
      modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs

33
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/EntityTagAdminAppService.cs

@ -8,15 +8,15 @@ namespace Volo.CmsKit.Admin.Tags
public class EntityTagAdminAppService : CmsKitAdminAppServiceBase, IEntityTagAdminAppService
{
protected ITagDefinitionStore TagDefinitionStore { get; }
protected IEntityTagManager EntityTagManager { get; }
protected ITagManager TagManager { get; }
protected EntityTagManager EntityTagManager { get; }
protected TagManager TagManager { get; }
protected ITagRepository TagRepository { get; }
protected IEntityTagRepository EntityTagRepository { get; }
public EntityTagAdminAppService(
ITagDefinitionStore tagDefinitionStore,
IEntityTagManager entityTagManager,
ITagManager tagManager,
EntityTagManager entityTagManager,
TagManager tagManager,
ITagRepository tagRepository,
IEntityTagRepository entityTagRepository)
{
@ -29,11 +29,11 @@ namespace Volo.CmsKit.Admin.Tags
public virtual async Task AddTagToEntityAsync(EntityTagCreateDto input)
{
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionAsync(input.EntityType);
await CheckPolicyAsync(definition.CreatePolicy);
var tag = await TagManager.GetOrAddAsync(input.EntityType, input.TagName, CurrentTenant?.Id);
var tag = await TagManager.GetOrAddAsync(input.EntityType, input.TagName);
await EntityTagManager.AddTagToEntityAsync(
tag.Id,
@ -44,7 +44,7 @@ namespace Volo.CmsKit.Admin.Tags
public virtual async Task RemoveTagFromEntityAsync(EntityTagRemoveDto input)
{
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionAsync(input.EntityType);
await CheckPolicyAsync(definition.DeletePolicy);
@ -57,24 +57,11 @@ namespace Volo.CmsKit.Admin.Tags
public virtual async Task SetEntityTagsAsync(EntityTagSetDto input)
{
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionsAsync(input.EntityType);
var definition = await TagDefinitionStore.GetTagEntityTypeDefinitionAsync(input.EntityType);
await CheckPolicyAsync(definition.UpdatePolicy);
var existingTags =
await TagRepository.GetAllRelatedTagsAsync(input.EntityType, input.EntityId, CurrentTenant?.Id);
var deletedTags = existingTags.Where(x => !input.Tags.Contains(x.Name)).ToList();
var addedTags = input.Tags.Where(x => !existingTags.Any(a => a.Name == x));
await EntityTagRepository.DeleteManyAsync(deletedTags.Select(s => s.Id).ToArray());
foreach (var addedTag in addedTags)
{
var tag = await TagManager.GetOrAddAsync(input.EntityType, addedTag, CurrentTenant?.Id);
await EntityTagManager.AddTagToEntityAsync(tag.Id, input.EntityType, input.EntityId, CurrentTenant?.Id);
}
await EntityTagManager.SetEntityTagsAsync(input.EntityType, input.EntityId, input.Tags);
}
}
}
}

28
modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Tags/TagAdminAppService.cs

@ -22,17 +22,19 @@ namespace Volo.CmsKit.Admin.Tags
TagUpdateDto>,
ITagAdminAppService
{
protected ITagManager TagManager { get; }
protected TagManager TagManager { get; }
protected ITagDefinitionStore TagDefinitionStore { get; }
protected IStringLocalizerFactory StringLocalizerFactory { get; }
public TagAdminAppService(
IRepository<Tag, Guid> repository,
ITagManager tagManager,
TagManager tagManager,
ITagDefinitionStore tagDefinitionStore,
IStringLocalizerFactory stringLocalizerFactory) : base(repository)
{
TagManager = tagManager;
StringLocalizerFactory = stringLocalizerFactory;
TagDefinitionStore = tagDefinitionStore;
StringLocalizerFactory = stringLocalizerFactory;
GetListPolicyName = CmsKitAdminPermissions.Tags.Default;
GetPolicyName = CmsKitAdminPermissions.Tags.Default;
@ -44,12 +46,13 @@ namespace Volo.CmsKit.Admin.Tags
[Authorize(CmsKitAdminPermissions.Tags.Create)]
public override async Task<TagDto> CreateAsync(TagCreateDto input)
{
var tag = await TagManager.InsertAsync(
var tag = await TagManager.CreateAsync(
GuidGenerator.Create(),
input.EntityType,
input.Name,
CurrentTenant?.Id);
input.Name);
await Repository.InsertAsync(tag);
return await MapToGetOutputDtoAsync(tag);
}
@ -60,11 +63,14 @@ namespace Volo.CmsKit.Admin.Tags
id,
input.Name);
await Repository.UpdateAsync(tag);
return await MapToGetOutputDtoAsync(tag);
}
protected override IQueryable<Tag> CreateFilteredQuery(TagGetListInput input)
protected override async Task<IQueryable<Tag>> CreateFilteredQueryAsync(TagGetListInput input)
{
return base.CreateFilteredQuery(input)
return (await base.CreateFilteredQueryAsync(input))
.WhereIf(
!input.Filter.IsNullOrEmpty(),
x =>
@ -74,7 +80,7 @@ namespace Volo.CmsKit.Admin.Tags
public virtual async Task<List<TagDefinitionDto>> GetTagDefinitionsAsync()
{
var definitions = await TagManager.GetTagDefinitionsAsync();
var definitions = await TagDefinitionStore.GetTagEntityTypeDefinitionListAsync();
return definitions
.Select(s =>

3
modules/cms-kit/src/Volo.CmsKit.Common.Application/Volo/CmsKit/Tags/TagAppService.cs

@ -20,8 +20,7 @@ namespace Volo.CmsKit.Tags
{
var entities = await TagRepository.GetAllRelatedTagsAsync(
entityType,
entityId,
CurrentTenant.Id);
entityId);
return ObjectMapper.Map<List<Tag>, List<TagDto>>(entities);
}

2
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/MediaDescriptors/MediaDescriptor.cs

@ -33,7 +33,7 @@ namespace Volo.CmsKit.MediaDescriptors
public void SetName(string name)
{
if (!name.IsValidMediaFileName())
if (!MediaDescriptorCheck.IsValidMediaFileName(name))
{
throw new InvalidMediaDescriptorNameException(name);
}

4
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/MediaDescriptors/Extensions/MediaDescriptorExtensions.cs → modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/MediaDescriptors/MediaDescriptorCheck.cs

@ -3,9 +3,9 @@ using System.Linq;
namespace Volo.CmsKit.MediaDescriptors.Extensions
{
public static class MediaDescriptorExtensions
public static class MediaDescriptorCheck
{
public static bool IsValidMediaFileName(this string name)
public static bool IsValidMediaFileName(string name)
{
if (string.IsNullOrWhiteSpace(name))
{

2
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/CmsKitTagOptions.cs

@ -5,6 +5,6 @@ namespace Volo.CmsKit.Tags
public class CmsKitTagOptions
{
[NotNull]
public TagEntityTypeDefinitionDictionary EntityTypes { get; } = new TagEntityTypeDefinitionDictionary();
public TagEntityTypeDefinitions EntityTypes { get; } = new TagEntityTypeDefinitions();
}
}

2
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs

@ -24,7 +24,7 @@ namespace Volo.CmsKit.Tags
/// <param name="entityType">EntityType to get definition.</param>
/// <exception cref="EntityNotTaggableException">Thrown when EntityType is not configured as taggable.</exception>
/// <exception cref="InvalidOperationException">More than one element satisfies the condition in predicate.</exception>
public virtual Task<TagEntityTypeDefiniton> GetTagEntityTypeDefinitionsAsync([NotNull] string entityType)
public virtual Task<TagEntityTypeDefiniton> GetTagEntityTypeDefinitionAsync([NotNull] string entityType)
{
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));

12
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityNotTaggableException.cs

@ -1,5 +1,5 @@
using Microsoft.Extensions.Logging;
using System;
using System;
using System.Runtime.Serialization;
using Volo.Abp;
namespace Volo.CmsKit.Tags
@ -7,13 +7,7 @@ namespace Volo.CmsKit.Tags
[Serializable]
public class EntityNotTaggableException : BusinessException
{
public EntityNotTaggableException(
string code = null,
string message = null,
string details = null,
Exception innerException = null,
LogLevel logLevel = LogLevel.Warning)
: base(code, message, details, innerException, logLevel)
public EntityNotTaggableException(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context)
{
}

30
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/EntityTagManager.cs

@ -1,22 +1,30 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;
namespace Volo.CmsKit.Tags
{
public class EntityTagManager : DomainService, IEntityTagManager
public class EntityTagManager : DomainService
{
protected IEntityTagRepository EntityTagRepository { get; }
protected ITagRepository TagRepository { get; }
protected ITagDefinitionStore TagDefinitionStore { get; }
protected TagManager TagManager { get; }
public EntityTagManager(
IEntityTagRepository entityTagRepository,
ITagDefinitionStore tagDefinitionStore)
ITagRepository tagRepository,
ITagDefinitionStore tagDefinitionStore,
TagManager tagManager)
{
EntityTagRepository = entityTagRepository;
TagRepository = tagRepository;
TagDefinitionStore = tagDefinitionStore;
TagManager = tagManager;
}
public virtual async Task<EntityTag> AddTagToEntityAsync(
@ -45,5 +53,23 @@ namespace Volo.CmsKit.Tags
var entityTag = await EntityTagRepository.FindAsync(tagId, entityId, tenantId, cancellationToken);
await EntityTagRepository.DeleteAsync(entityTag, cancellationToken: cancellationToken);
}
public async Task SetEntityTagsAsync(string entityType, string entityId, List<string> tags)
{
var existingTags =
await TagRepository.GetAllRelatedTagsAsync(entityType, entityId);
var deletedTags = existingTags.Where(x => !tags.Contains(x.Name)).ToList();
var addedTags = tags.Where(x => !existingTags.Any(a => a.Name == x));
await EntityTagRepository.DeleteManyAsync(deletedTags.Select(s => s.Id).ToArray());
foreach (var addedTag in addedTags)
{
var tag = await TagManager.GetOrAddAsync(entityType, addedTag);
await AddTagToEntityAsync(tag.Id, entityType, entityId, CurrentTenant?.Id);
}
}
}
}

24
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/IEntityTagManager.cs

@ -1,24 +0,0 @@
using JetBrains.Annotations;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Volo.CmsKit.Tags
{
public interface IEntityTagManager
{
Task<EntityTag> AddTagToEntityAsync(
[NotNull] Guid tagId,
[NotNull] string entityType,
[NotNull] string entityId,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default);
Task RemoveTagFromEntityAsync(
[NotNull] Guid tagId,
[NotNull] string entityType,
[NotNull] string entityId,
[CanBeNull] Guid? tenantId = null,
CancellationToken cancellationToken = default);
}
}

2
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagDefinitionStore.cs

@ -8,7 +8,7 @@ namespace Volo.CmsKit.Tags
{
Task<List<TagEntityTypeDefiniton>> GetTagEntityTypeDefinitionListAsync();
Task<TagEntityTypeDefiniton> GetTagEntityTypeDefinitionsAsync([NotNull] string entityType);
Task<TagEntityTypeDefiniton> GetTagEntityTypeDefinitionAsync([NotNull] string entityType);
Task<bool> IsDefinedAsync([NotNull] string entityType);
}

33
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagManager.cs

@ -1,33 +0,0 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp.Domain.Services;
using Volo.CmsKit.Tags;
namespace Volo.CmsKit.Tags
{
public interface ITagManager : IDomainService
{
Task<Tag> InsertAsync(
Guid id,
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
Task<Tag> UpdateAsync(
Guid id,
[NotNull] string name,
CancellationToken cancellationToken = default);
Task<Tag> GetOrAddAsync(
[NotNull] string entityType,
[NotNull] string name,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync(CancellationToken cancellationToken = default);
}
}

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);
}
}

11
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagEntityTypeDefinitionDictionary.cs

@ -1,11 +0,0 @@
using JetBrains.Annotations;
using System;
using System.Collections.Generic;
using Volo.Abp.Localization;
namespace Volo.CmsKit.Tags
{
public class TagEntityTypeDefinitionDictionary : List<TagEntityTypeDefiniton>
{
}
}

8
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagEntityTypeDefinitions.cs

@ -0,0 +1,8 @@
using System.Collections.Generic;
namespace Volo.CmsKit.Tags
{
public class TagEntityTypeDefinitions : List<TagEntityTypeDefiniton>
{
}
}

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

@ -3,11 +3,12 @@ using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Services;
namespace Volo.CmsKit.Tags
{
public class TagManager : DomainService, ITagManager
public class TagManager : DomainService
{
protected ITagRepository TagRepository { get; }
protected ITagDefinitionStore TagDefinitionStore { get; }
@ -18,56 +19,52 @@ namespace Volo.CmsKit.Tags
TagDefinitionStore = tagDefinitionStore;
}
public virtual 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)
{
var entity = await TagRepository.FindAsync(entityType, name, tenantId, cancellationToken);
var tag = await TagRepository.FindAsync(entityType, name);
if (entity == null)
if (tag == null)
{
entity = await InsertAsync(GuidGenerator.Create(), entityType, name, tenantId, cancellationToken);
tag = await CreateAsync(GuidGenerator.Create(), entityType, name);
await TagRepository.InsertAsync(tag);
}
return entity;
return tag;
}
public virtual async Task<Tag> InsertAsync(Guid id, [NotNull] string entityType, [NotNull] string name,
Guid? tenantId = null, CancellationToken cancellationToken = default)
public virtual async Task<Tag> CreateAsync(Guid id,
[NotNull] string entityType,
[NotNull] string name)
{
if (!await TagDefinitionStore.IsDefinedAsync(entityType))
{
throw new EntityNotTaggableException(entityType);
}
if (await TagRepository.AnyAsync(entityType, name, tenantId, cancellationToken))
if (await TagRepository.AnyAsync(entityType, name))
{
throw new TagAlreadyExistException(entityType, name);
}
return await TagRepository.InsertAsync(new Tag(id, entityType, name, tenantId),
cancellationToken: cancellationToken);
return
new Tag(id, entityType, name, CurrentTenant.Id);
}
public virtual async Task<Tag> UpdateAsync(Guid id, [NotNull] string name,
CancellationToken cancellationToken = default)
public virtual async Task<Tag> UpdateAsync(Guid id, [NotNull] string name)
{
var entity = await TagRepository.GetAsync(id, cancellationToken: cancellationToken);
Check.NotNullOrEmpty(name, nameof(name));
if (name != entity.Name &&
await TagRepository.AnyAsync(entity.EntityType, name, entity.TenantId, cancellationToken))
var tag = await TagRepository.GetAsync(id);
if (name != tag.Name &&
await TagRepository.AnyAsync(tag.EntityType, name))
{
throw new TagAlreadyExistException(entity.EntityType, name);
throw new TagAlreadyExistException(tag.EntityType, name);
}
entity.SetName(name);
return await TagRepository.UpdateAsync(entity, cancellationToken: cancellationToken);
}
tag.SetName(name);
public virtual Task<List<TagEntityTypeDefiniton>> GetTagDefinitionsAsync(
CancellationToken cancellationToken = default)
{
return TagDefinitionStore.GetTagEntityTypeDefinitionListAsync();
return tag;
}
}
}
}

16
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,28 +56,25 @@ namespace Volo.CmsKit.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()).Set<EntityTag>()
.Where(q => q.EntityId == entityId && q.TenantId == tenantId)
.Where(q => q.EntityId == entityId)
.Select(q => q.TagId)
.ToListAsync(cancellationToken: GetCancellationToken(cancellationToken));
var query = (await GetDbSetAsync())
.Where(x => x.EntityType == entityType &&
x.TenantId == tenantId &&
entityTagIds.Contains(x.Id));
return await query.ToListAsync(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));

10
modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs

@ -48,5 +48,15 @@ namespace Volo.CmsKit.Tags
Name = _cmsKitTestData.Content_1_Tags[0],
}));
}
public async Task GetTagDefinitionsAsync_ShouldWorkProperly_WithoutParameters()
{
var definitions = await _tagAdminAppService.GetTagDefinitionsAsync();
definitions.ShouldNotBeNull();
definitions.ShouldNotBeEmpty();
definitions.Count.ShouldBeGreaterThan(1);
definitions.ShouldContain(x => x.EntityType == _cmsKitTestData.TagDefinition_1_EntityType);
}
}
}

54
modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/EntityTagManager_Tests.cs

@ -1,4 +1,5 @@
using Shouldly;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Guids;
@ -9,14 +10,14 @@ namespace Volo.CmsKit.Tags
public class EntityTagManager_Tests : CmsKitDomainTestBase
{
private readonly CmsKitTestData _cmsKitTestData;
private readonly IEntityTagManager _entityTagManager;
private readonly EntityTagManager _entityTagManager;
private readonly ITagRepository _tagRepository;
private readonly IGuidGenerator _guidGenerator;
public EntityTagManager_Tests()
{
_cmsKitTestData = GetRequiredService<CmsKitTestData>();
_entityTagManager = GetRequiredService<IEntityTagManager>();
_entityTagManager = GetRequiredService<EntityTagManager>();
_tagRepository = GetRequiredService<ITagRepository>();
_guidGenerator = GetRequiredService<IGuidGenerator>();
}
@ -56,5 +57,54 @@ namespace Volo.CmsKit.Tags
tags.ShouldNotContain(x => x.Id == tagToDelete.Id);
}
[Fact]
public async Task SetEntityTagsAsync_ShouldWorkProperly_WithNonExistingTags()
{
var newTags = new List<string> { "non-existing-awesome-tag-a", "non-existing-awesome-tag-b" };
await _entityTagManager.SetEntityTagsAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, newTags);
var tags = await _tagRepository.GetAllRelatedTagsAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1);
tags.ShouldNotBeNull();
tags.ShouldNotBeEmpty();
tags.ForEach(tag => newTags.Contains(tag.Name));
}
[Fact]
public async Task SetEntityTagsAsync_ShouldWorkProperly_WithExistingTag()
{
var entityTags = new List<string>
{
_cmsKitTestData.TagName_1
};
await _entityTagManager.SetEntityTagsAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, entityTags);
var tags = await _tagRepository.GetAllRelatedTagsAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1);
tags.ShouldNotBeNull();
tags.ShouldNotBeEmpty();
tags.ForEach(tag => entityTags.Contains(tag.Name));
}
[Fact]
public async Task SetEntityTagsAsync_ShouldWorkProperly_WithExistingAndNonExistingTag()
{
var entityTags = new List<string>
{
"New Awesome Tag",
_cmsKitTestData.TagName_1
};
await _entityTagManager.SetEntityTagsAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, entityTags);
var tags = await _tagRepository.GetAllRelatedTagsAsync(_cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1);
tags.ShouldNotBeNull();
tags.ShouldNotBeEmpty();
tags.ForEach(tag => entityTags.Contains(tag.Name));
}
}
}

28
modules/cms-kit/test/Volo.CmsKit.Domain.Tests/Tags/TagManager_Tests.cs

@ -53,24 +53,23 @@ namespace Volo.CmsKit.Tags
}
[Fact]
public async Task ShouldInsertAsync()
public async Task ShouldCreateAsync()
{
var tagName = "Freshly Created New Tag";
var tag = await _tagManager.InsertAsync(Guid.NewGuid(), _cmsKitTestData.EntityType1, tagName);
var tag = await _tagManager.CreateAsync(Guid.NewGuid(), _cmsKitTestData.EntityType1, tagName);
tag.ShouldNotBeNull();
var doesExist = await _tagRepository.AnyAsync(_cmsKitTestData.EntityType1, tagName);
doesExist.ShouldBeTrue();
tag.Id.ShouldNotBe(Guid.Empty);
}
[Fact]
public async Task ShouldntInsertWithUnconfiguredEntityTypeAsync()
{
var notConfiguredEntityType = "My.Namespace.SomeEntity";
var exception = await Should.ThrowAsync<EntityNotTaggableException>(async () =>
await _tagManager.InsertAsync(Guid.NewGuid(), notConfiguredEntityType, "test"));
await _tagManager.CreateAsync(Guid.NewGuid(), notConfiguredEntityType, "test"));
exception.ShouldNotBeNull();
exception.Data[nameof(Tag.EntityType)].ShouldBe(notConfiguredEntityType);
@ -82,7 +81,7 @@ namespace Volo.CmsKit.Tags
var type = _cmsKitTestData.Content_1_EntityType;
var name = _cmsKitTestData.Content_1_Tags[0];
Should.Throw<Exception>(async () => await _tagManager.InsertAsync(Guid.NewGuid(), type, name));
Should.Throw<Exception>(async () => await _tagManager.CreateAsync(Guid.NewGuid(), type, name));
}
[Fact]
@ -94,11 +93,10 @@ namespace Volo.CmsKit.Tags
var tag = await _tagRepository.GetAsync(type, name);
await _tagManager.UpdateAsync(tag.Id, newName);
var updatedTag = await _tagRepository.GetAsync(type, newName);
var updatedTag = await _tagManager.UpdateAsync(tag.Id, newName);
updatedTag.Id.ShouldBe(tag.Id);
updatedTag.Name.ShouldBe(newName);
}
[Fact]
@ -112,15 +110,5 @@ namespace Volo.CmsKit.Tags
Should.Throw<Exception>(async () => await _tagManager.UpdateAsync(tag.Id, newName));
}
[Fact]
public async Task ShouldGetTagDefinitionsProperly_WithoutParameter()
{
var definitions = await _tagManager.GetTagDefinitionsAsync();
definitions.ShouldNotBeNull();
definitions.Count.ShouldBeGreaterThan(1);
definitions.ShouldContain(x => x.EntityType == _cmsKitTestData.TagDefinition_1_EntityType);
}
}
}

35
modules/cms-kit/test/Volo.CmsKit.TestBase/CmsKitDataSeedContributor.cs

@ -34,8 +34,9 @@ namespace Volo.CmsKit
private readonly IRatingRepository _ratingRepository;
private readonly ICurrentTenant _currentTenant;
private readonly IContentRepository _contentRepository;
private readonly IEntityTagManager _entityTagManager;
private readonly ITagManager _tagManager;
private readonly EntityTagManager _entityTagManager;
private readonly TagManager _tagManager;
private readonly ITagRepository _tagRepository;
private readonly IEntityTagRepository _entityTagRepository;
private readonly IPageRepository _pageRepository;
private readonly IBlogRepository _blogRepository;
@ -56,13 +57,14 @@ namespace Volo.CmsKit
IRatingRepository ratingRepository,
ICurrentTenant currentTenant,
IContentRepository contentRepository,
ITagManager tagManager,
TagManager tagManager,
ITagRepository tagRepository,
IEntityTagRepository entityTagRepository,
IPageRepository pageRepository,
IBlogRepository blogRepository,
IBlogPostRepository blogPostRepository,
IBlogFeatureRepository blogFeatureRepository,
IEntityTagManager entityTagManager,
EntityTagManager entityTagManager,
IOptions<CmsKitOptions> options,
IOptions<CmsKitTagOptions> tagOptions,
IMediaDescriptorRepository mediaDescriptorRepository,
@ -78,6 +80,7 @@ namespace Volo.CmsKit
_currentTenant = currentTenant;
_contentRepository = contentRepository;
_tagManager = tagManager;
_tagRepository = tagRepository;
_entityTagManager = entityTagManager;
_entityTagRepository = entityTagRepository;
_pageRepository = pageRepository;
@ -288,24 +291,40 @@ namespace Volo.CmsKit
private async Task SeedTagsAsync()
{
var created1 = await _tagManager.InsertAsync(_cmsKitTestData.TagId_1, _cmsKitTestData.EntityType1, _cmsKitTestData.TagName_1);
var created1 = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_cmsKitTestData.TagId_1,
_cmsKitTestData.EntityType1,
_cmsKitTestData.TagName_1));
await _entityTagManager.AddTagToEntityAsync(created1.Id, created1.EntityType, _cmsKitTestData.EntityId1);
var created2 = await _tagManager.InsertAsync(_cmsKitTestData.TagId_2, _cmsKitTestData.EntityType2, _cmsKitTestData.TagName_2);
var created2 = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_cmsKitTestData.TagId_2,
_cmsKitTestData.EntityType2,
_cmsKitTestData.TagName_2));
await _entityTagManager.AddTagToEntityAsync(created2.Id, created2.EntityType, _cmsKitTestData.EntityId2);
foreach (var tag in _cmsKitTestData.Content_1_Tags)
{
var tagEntity = await _tagManager.InsertAsync(_guidGenerator.Create(), _cmsKitTestData.Content_1_EntityType, tag);
var tagEntity = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_guidGenerator.Create(),
_cmsKitTestData.Content_1_EntityType,
tag));
await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_1_EntityType, _cmsKitTestData.Content_1_EntityId);
}
foreach (var tag in _cmsKitTestData.Content_2_Tags)
{
var tagEntity = await _tagManager.InsertAsync(_guidGenerator.Create(), _cmsKitTestData.Content_2_EntityType, tag);
var tagEntity = await _tagRepository.InsertAsync(
await _tagManager.CreateAsync(
_guidGenerator.Create(),
_cmsKitTestData.Content_2_EntityType,
tag));
await _entityTagManager.AddTagToEntityAsync(tagEntity.Id, _cmsKitTestData.Content_2_EntityType, _cmsKitTestData.Content_2_EntityId);
}

Loading…
Cancel
Save