diff --git a/modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Contents/ContentAdminAppService.cs b/modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Contents/ContentAdminAppService.cs index 9cd6df6bc7..f83d77af52 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Contents/ContentAdminAppService.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Admin.Application/Volo/CmsKit/Admin/Contents/ContentAdminAppService.cs @@ -1,14 +1,16 @@ using Microsoft.AspNetCore.Authorization; using System; +using System.Threading.Tasks; using Volo.Abp.Application.Services; using Volo.Abp.Domain.Repositories; using Volo.CmsKit.Contents; +using Volo.CmsKit.Domain.Volo.CmsKit.Contents; using Volo.CmsKit.Permissions; namespace Volo.CmsKit.Admin.Contents { [Authorize(CmsKitAdminPermissions.Contents.Default)] - public class ContentAdminAppService : + public class ContentAdminAppService : CrudAppService< Content, ContentDto, @@ -18,19 +20,37 @@ namespace Volo.CmsKit.Admin.Contents ContentUpdateDto >, IContentAdminAppService { + protected IContentManager ContentManager { get; } protected IContentRepository ContentRepository { get; } public ContentAdminAppService( IRepository repository, + IContentManager contentManager, IContentRepository contentRepository) : base(repository) { - ContentRepository = contentRepository; + ContentManager = contentManager; GetListPolicyName = CmsKitAdminPermissions.Contents.Default; GetPolicyName = CmsKitAdminPermissions.Contents.Default; CreatePolicyName = CmsKitAdminPermissions.Contents.Create; UpdatePolicyName = CmsKitAdminPermissions.Contents.Update; DeletePolicyName = CmsKitAdminPermissions.Contents.Delete; + ContentRepository = contentRepository; + } + + [Authorize(CmsKitAdminPermissions.Contents.Create)] + public override async Task CreateAsync(ContentCreateDto input) + { + var entity = new Content( + GuidGenerator.Create(), + input.EntityType, + input.EntityId, + input.Value, + CurrentTenant?.Id); + + await ContentManager.InsertAsync(entity); + + return MapToGetListOutputDto(entity); } } } 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 76ba39c3f2..4b57a5a52e 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 @@ -3,5 +3,6 @@ public static class CmsKitErrorCodes { public const string TagAlreadyExist = "CmsKit:0001"; + public const string ContentAlreadyExist = "CmsKit:0002"; } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/Content.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/Content.cs index 6f4175daad..e2d4a2a4ac 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/Content.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/Content.cs @@ -8,9 +8,9 @@ namespace Volo.CmsKit.Contents { public class Content : FullAuditedAggregateRoot, IMultiTenant { - public virtual Guid? TenantId { get; set; } + public virtual Guid? TenantId { get; protected set; } - public virtual string EntityType { get; set; } + public virtual string EntityType { get; protected set; } public virtual string EntityId { get; set; } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/ContentManager.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/ContentManager.cs new file mode 100644 index 0000000000..33c4889245 --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/ContentManager.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using Volo.Abp.Domain.Services; +using Volo.CmsKit.Contents; + +namespace Volo.CmsKit.Domain.Volo.CmsKit.Contents +{ + public class ContentManager : DomainService, IContentManager + { + protected IContentRepository ContentRepository { get; } + public ContentManager(IContentRepository contentRepository) + { + ContentRepository = contentRepository; + } + + + public async Task InsertAsync(Content content, CancellationToken cancellationToken = default) + { + if (await ContentRepository.ExistAsync(content.EntityType, content.EntityId, content.TenantId, cancellationToken)) + { + throw new ContentAlreadyExistException(content.EntityType, content.EntityId); + } + + return await ContentRepository.InsertAsync(content); + } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/IContentManager.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/IContentManager.cs new file mode 100644 index 0000000000..e795e7895c --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/IContentManager.cs @@ -0,0 +1,11 @@ +using System.Threading; +using System.Threading.Tasks; +using Volo.CmsKit.Contents; + +namespace Volo.CmsKit.Domain.Volo.CmsKit.Contents +{ + public interface IContentManager + { + Task InsertAsync(Content content, CancellationToken cancellationToken = default); + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/IContentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/IContentRepository.cs index acd5ed2a84..70fcf748d2 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/IContentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/IContentRepository.cs @@ -20,6 +20,16 @@ namespace Volo.CmsKit.Contents Guid? tenantId = null, CancellationToken cancellationToken = default); - Task DeleteAsync([NotNull] string entityType, [NotNull] string entityId, Guid? tenantId = null, CancellationToken cancellationToken = default); + Task DeleteAsync( + [NotNull] string entityType, + [NotNull] string entityId, + Guid? tenantId = null, + CancellationToken cancellationToken = default); + + Task ExistAsync( + [NotNull] string entityType, + [NotNull] string entityId, + Guid? tenantId = null, + CancellationToken cancellationToken = default); } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/TagAlreadyExistException.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/TagAlreadyExistException.cs new file mode 100644 index 0000000000..8ea4d4c54a --- /dev/null +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Contents/TagAlreadyExistException.cs @@ -0,0 +1,17 @@ +using JetBrains.Annotations; +using System; +using Volo.Abp; + +namespace Volo.CmsKit.Contents +{ + [Serializable] + public class ContentAlreadyExistException : BusinessException + { + public ContentAlreadyExistException([NotNull] string entityType, [NotNull] string entityId) + { + Code = CmsKitErrorCodes.ContentAlreadyExist; + WithData(nameof(Content.EntityType), entityType); + WithData(nameof(Content.EntityId), entityId); + } + } +} diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Contents/EfCoreContentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Contents/EfCoreContentRepository.cs index 9293d637f7..66704ffb6d 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Contents/EfCoreContentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Contents/EfCoreContentRepository.cs @@ -1,4 +1,6 @@ -using System; +using JetBrains.Annotations; +using Microsoft.EntityFrameworkCore; +using System; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; @@ -51,5 +53,15 @@ namespace Volo.CmsKit.Contents x.TenantId == tenantId, cancellationToken: GetCancellationToken(cancellationToken)); } + + public async Task ExistAsync([NotNull] string entityType, [NotNull] string entityId, Guid? tenantId = null, CancellationToken cancellationToken = default) + { + var dbSet = await GetDbSetAsync(); + return await dbSet.AnyAsync(x => + x.EntityType == entityType && + x.EntityId == entityId && + x.TenantId == tenantId, + cancellationToken: GetCancellationToken(cancellationToken)); + } } } \ No newline at end of file diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Contents/MongoContentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Contents/MongoContentRepository.cs index ccdee8b431..b323d9d686 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Contents/MongoContentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Contents/MongoContentRepository.cs @@ -1,4 +1,6 @@ -using System; +using JetBrains.Annotations; +using MongoDB.Driver.Linq; +using System; using System.Threading; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.MongoDB; @@ -51,5 +53,15 @@ namespace Volo.CmsKit.MongoDB.Contents x.TenantId == tenantId, cancellationToken: GetCancellationToken(cancellationToken)); } + + public async Task ExistAsync([NotNull] string entityType, [NotNull] string entityId, Guid? tenantId = null, CancellationToken cancellationToken = default) + { + return await (await GetMongoQueryableAsync()).AnyAsync(x => + !x.IsDeleted && + x.EntityType == entityType && + x.EntityId == entityId && + x.TenantId == tenantId, + cancellationToken: GetCancellationToken(cancellationToken)); + } } } \ No newline at end of file diff --git a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentAdminAppService_Tests.cs b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentAdminAppService_Tests.cs index 7d3e7b5fd3..d8c5d62a61 100644 --- a/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentAdminAppService_Tests.cs +++ b/modules/cms-kit/test/Volo.CmsKit.Application.Tests/Contents/ContentAdminAppService_Tests.cs @@ -83,7 +83,7 @@ namespace Volo.CmsKit.Contents Value = "Some long content" }); - await Should.ThrowAsync(async () => + await Should.ThrowAsync(async () => await _service.CreateAsync(new ContentCreateDto { EntityId = entityId,