CmsKit - Add Business Logic to Content

pull/6926/head
enisn 5 years ago
parent 4c18d77f80
commit 15a5273f5e

@ -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<Content, Guid> 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<ContentDto> CreateAsync(ContentCreateDto input)
{
var entity = new Content(
GuidGenerator.Create(),
input.EntityType,
input.EntityId,
input.Value,
CurrentTenant?.Id);
await ContentManager.InsertAsync(entity);
return MapToGetListOutputDto(entity);
}
}
}

@ -3,5 +3,6 @@
public static class CmsKitErrorCodes
{
public const string TagAlreadyExist = "CmsKit:0001";
public const string ContentAlreadyExist = "CmsKit:0002";
}
}

@ -8,9 +8,9 @@ namespace Volo.CmsKit.Contents
{
public class Content : FullAuditedAggregateRoot<Guid>, 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; }

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

@ -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<Content> InsertAsync(Content content, CancellationToken cancellationToken = default);
}
}

@ -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<bool> ExistAsync(
[NotNull] string entityType,
[NotNull] string entityId,
Guid? tenantId = null,
CancellationToken cancellationToken = default);
}
}

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

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

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

@ -83,7 +83,7 @@ namespace Volo.CmsKit.Contents
Value = "Some long content"
});
await Should.ThrowAsync<Exception>(async () =>
await Should.ThrowAsync<ContentAlreadyExistException>(async () =>
await _service.CreateAsync(new ContentCreateDto
{
EntityId = entityId,

Loading…
Cancel
Save