Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMS Kit - Tag and media refactorings #7848

Merged
merged 14 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -29,11 +29,11 @@ public class EntityTagAdminAppService : CmsKitAdminAppServiceBase, IEntityTagAdm

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,
Expand All @@ -44,7 +44,7 @@ public virtual async Task AddTagToEntityAsync(EntityTagCreateDto input)

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

Expand All @@ -57,24 +57,11 @@ public virtual async Task RemoveTagFromEntityAsync(EntityTagRemoveDto input)

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);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,19 @@ public class TagAdminAppService :
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;
Expand All @@ -44,12 +46,13 @@ public class TagAdminAppService :
[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);
enisn marked this conversation as resolved.
Show resolved Hide resolved

await Repository.InsertAsync(tag);

return await MapToGetOutputDtoAsync(tag);
}

Expand All @@ -60,11 +63,14 @@ public override async Task<TagDto> UpdateAsync(Guid id, TagUpdateDto input)
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 =>
Expand All @@ -74,7 +80,7 @@ protected override IQueryable<Tag> CreateFilteredQuery(TagGetListInput input)

public virtual async Task<List<TagDefinitionDto>> GetTagDefinitionsAsync()
{
var definitions = await TagManager.GetTagDefinitionsAsync();
var definitions = await TagDefinitionStore.GetTagEntityTypeDefinitionListAsync();

return definitions
.Select(s =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public virtual async Task<List<TagDto>> GetAllRelatedTagsAsync(string entityType
{
var entities = await TagRepository.GetAllRelatedTagsAsync(
entityType,
entityId,
CurrentTenant.Id);
entityId);

return ObjectMapper.Map<List<Tag>, List<TagDto>>(entities);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MediaDescriptor(Guid id, string name, string mimeType, long size, Guid? t

public void SetName(string name)
{
if (!name.IsValidMediaFileName())
if (!MediaDescriptorCheck.IsValidMediaFileName(name))
{
throw new InvalidMediaDescriptorNameException(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ namespace Volo.CmsKit.Tags
public class CmsKitTagOptions
{
[NotNull]
public TagEntityTypeDefinitionDictionary EntityTypes { get; } = new TagEntityTypeDefinitionDictionary();
public TagEntityTypeDefinitions EntityTypes { get; } = new TagEntityTypeDefinitions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DefaultTagDefinitionStore(IOptions<CmsKitTagOptions> options)
/// <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));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using Microsoft.Extensions.Logging;
using System;
using System;
using System.Runtime.Serialization;
using Volo.Abp;

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

Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -45,5 +53,23 @@ public class EntityTagManager : DomainService, IEntityTagManager
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);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface ITagDefinitionStore
{
Task<List<TagEntityTypeDefiniton>> GetTagEntityTypeDefinitionListAsync();

Task<TagEntityTypeDefiniton> GetTagEntityTypeDefinitionsAsync([NotNull] string entityType);
Task<TagEntityTypeDefiniton> GetTagEntityTypeDefinitionAsync([NotNull] string entityType);

Task<bool> IsDefinedAsync([NotNull] string entityType);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,21 @@ public interface ITagRepository : IBasicRepository<Tag, Guid>
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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;

namespace Volo.CmsKit.Tags
{
public class TagEntityTypeDefinitions : List<TagEntityTypeDefiniton>
{
}
}