diff --git a/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagCreateDto.cs b/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagCreateDto.cs index 3eb75bec07..6dd90d1362 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagCreateDto.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagCreateDto.cs @@ -1,12 +1,17 @@ using System.ComponentModel.DataAnnotations; +using Volo.Abp.Validation; +using Volo.CmsKit.Tags; namespace Volo.CmsKit.Admin.Tags { public class TagCreateDto { [Required] + [DynamicMaxLength(typeof(TagConsts), nameof(TagConsts.MaxEntityTypeLength))] public string EntityType { get; set; } + [Required] + [DynamicMaxLength(typeof(TagConsts), nameof(TagConsts.MaxNameLength))] public string Name { get; set; } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagUpdateDto.cs b/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagUpdateDto.cs index 4df242a997..c4298b7730 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagUpdateDto.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Admin.Application.Contracts/Volo/CmsKit/Admin/Tags/TagUpdateDto.cs @@ -1,10 +1,13 @@ using System.ComponentModel.DataAnnotations; +using Volo.Abp.Validation; +using Volo.CmsKit.Tags; namespace Volo.CmsKit.Admin.Tags { public class TagUpdateDto { [Required] + [DynamicMaxLength(typeof(TagConsts), nameof(TagConsts.MaxNameLength))] public string Name { get; set; } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/CmsKitErrorCodes.cs b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/CmsKitErrorCodes.cs index 336d163cfe..76ba39c3f2 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/CmsKitErrorCodes.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/CmsKitErrorCodes.cs @@ -2,6 +2,6 @@ { public static class CmsKitErrorCodes { - //Add your business exception error codes here... + public const string TagAlreadyExist = "CmsKit:0001"; } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagAlreadyExistException.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagAlreadyExistException.cs new file mode 100644 index 0000000000..6cae0dde92 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Tags/TagAlreadyExistException.cs @@ -0,0 +1,17 @@ +using JetBrains.Annotations; +using System; +using Volo.Abp; + +namespace Volo.CmsKit.Tags +{ + [Serializable] + public class TagAlreadyExistException : BusinessException + { + public TagAlreadyExistException([NotNull] string entityType, [NotNull] string name) + { + Code = CmsKitErrorCodes.TagAlreadyExist; + WithData(nameof(Tag.EntityType), entityType); + WithData(nameof(Tag.Name), name); + } + } +} 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 e1e9366a99..042b4e27a9 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 @@ -41,7 +41,7 @@ namespace Volo.CmsKit.Tags { if (await _tagRepository.AnyAsync(entityType, name, tenantId, cancellationToken)) { - throw new BusinessException(message: "Tag already exist!"); // Already Exist + throw new TagAlreadyExistException(entityType, name); } return await _tagRepository.InsertAsync( @@ -63,7 +63,7 @@ namespace Volo.CmsKit.Tags if (name != entity.Name && await _tagRepository.AnyAsync(entity.EntityType, name, entity.TenantId, cancellationToken)) { - throw new BusinessException(message: "Tag already exist!"); // Already Exist + throw new TagAlreadyExistException(entity.EntityType, name); } entity.SetName(name); diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs new file mode 100644 index 0000000000..4ad0d43468 --- /dev/null +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAdminAppService_Tests.cs @@ -0,0 +1,52 @@ +using Microsoft.Extensions.DependencyInjection; +using NSubstitute; +using Shouldly; +using System; +using System.Threading.Tasks; +using Volo.Abp.Users; +using Volo.CmsKit.Admin.Tags; +using Xunit; + +namespace Volo.CmsKit.Tags +{ + public class TagAdminAppService_Tests : CmsKitApplicationTestBase + { + private readonly ITagAdminAppService _tagAdminAppService; + private ICurrentUser _currentUser; + private readonly CmsKitTestData _cmsKitTestData; + + public TagAdminAppService_Tests() + { + _tagAdminAppService = GetRequiredService(); + _cmsKitTestData = GetRequiredService(); + } + + protected override void AfterAddApplication(IServiceCollection services) + { + _currentUser = Substitute.For(); + services.AddSingleton(_currentUser); + } + + [Fact] + public async Task ShouldCreateProperly() + { + var list = await _tagAdminAppService.CreateAsync(new TagCreateDto + { + EntityType = "any_new_type", + Name = "1", + }); + + list.Id.ShouldNotBe(Guid.Empty); + } + + [Fact] + public async Task ShouldThrowException_WhenTagAlreadyExist() + { + await Should.ThrowAsync(async () => await _tagAdminAppService.CreateAsync(new TagCreateDto + { + EntityType = _cmsKitTestData.Content_1_EntityType, + Name = _cmsKitTestData.Content_1_Tags[0], + })); + } + } +} diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagPublicAppService_Tests.cs similarity index 95% rename from modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAppService_Tests.cs rename to modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagPublicAppService_Tests.cs index 35b46b5a04..6ad1136167 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagAppService_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Tags/TagPublicAppService_Tests.cs @@ -10,13 +10,13 @@ using Xunit; namespace Volo.CmsKit.Tags { - public class TagAppService_Tests : CmsKitApplicationTestBase + public class TagPublicAppService_Tests : CmsKitApplicationTestBase { private readonly ITagAppService _tagAppService; private ICurrentUser _currentUser; private readonly CmsKitTestData _cmsKitTestData; - public TagAppService_Tests() + public TagPublicAppService_Tests() { _tagAppService = GetRequiredService(); _cmsKitTestData = GetRequiredService();