From aa34547aa8b64e2b625ccdba251a7eeaf5b27dc1 Mon Sep 17 00:00:00 2001 From: enisn Date: Thu, 28 Jan 2021 12:55:42 +0300 Subject: [PATCH] CmsKit - Refactor TagDefinitionStore --- .../CmsKit/Tags/DefaultTagDefinitionStore.cs | 24 ++++++++++++++++--- .../Volo/CmsKit/Tags/ITagDefinitionStore.cs | 2 +- .../Volo/CmsKit/Tags/TagManager.cs | 2 +- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs index 0eea8b178b..5a79bea25a 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/DefaultTagDefinitionStore.cs @@ -1,5 +1,6 @@ using JetBrains.Annotations; using Microsoft.Extensions.Options; +using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -17,24 +18,41 @@ namespace Volo.CmsKit.Tags this.options = options.Value; } + /// + /// Gets single by entityType. + /// + /// EntityType to get definition. + /// Thrown when EntityType is not configured as taggable. + /// More than one element satisfies the condition in predicate. public virtual Task GetTagEntityTypeDefinitionsAsync([NotNull] string entityType) { Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); - var result = options.EntityTypes.FirstOrDefault(x => x.EntityType == entityType) ?? throw new EntityNotTaggableException(entityType); + var result = options.EntityTypes.SingleOrDefault(x => x.EntityType == entityType) ?? throw new EntityNotTaggableException(entityType); return Task.FromResult(result); } - public virtual Task> GetTagDefinitionsAsync() + /// + /// Gets all defined elements. + /// + public virtual Task> GetTagEntityTypeDefinitionListAsync() { return Task.FromResult(options.EntityTypes.ToList()); } + /// + /// Checks if EntityType defined as taggable. + /// + /// EntityType to check. + /// More than one element satisfies the condition in predicate." public virtual Task IsDefinedAsync([NotNull] string entityType) { Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); - return Task.FromResult(options.EntityTypes.Any(a => a.EntityType == entityType)); + + var definition = options.EntityTypes.SingleOrDefault(x => x.EntityType == entityType); + + return Task.FromResult(definition != null); } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagDefinitionStore.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagDefinitionStore.cs index 1221e3660d..1dd8ae2bc3 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagDefinitionStore.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/ITagDefinitionStore.cs @@ -6,7 +6,7 @@ namespace Volo.CmsKit.Tags { public interface ITagDefinitionStore { - Task> GetTagDefinitionsAsync(); + Task> GetTagEntityTypeDefinitionListAsync(); Task GetTagEntityTypeDefinitionsAsync([NotNull] string entityType); 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 9c881226fa..77d50e9949 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 @@ -85,7 +85,7 @@ namespace Volo.CmsKit.Tags public Task> GetTagDefinitionsAsync(CancellationToken cancellationToken = default) { - return _tagDefinitionStore.GetTagDefinitionsAsync(); + return _tagDefinitionStore.GetTagEntityTypeDefinitionListAsync(); } } } \ No newline at end of file