mirror of https://github.com/abpframework/abp.git
committed by
GitHub
21 changed files with 475 additions and 82 deletions
@ -1,20 +1,13 @@ |
|||||
using System; |
using System; |
||||
using System.ComponentModel.DataAnnotations; |
|
||||
using Volo.Abp.Application.Dtos; |
using Volo.Abp.Application.Dtos; |
||||
using Volo.Abp.Validation; |
|
||||
using Volo.CmsKit.Blogs; |
|
||||
|
|
||||
namespace Volo.CmsKit.Admin.Blogs |
namespace Volo.CmsKit.Admin.Blogs |
||||
{ |
{ |
||||
[Serializable] |
[Serializable] |
||||
public class BlogDto : EntityDto<Guid> |
public class BlogDto : EntityDto<Guid> |
||||
{ |
{ |
||||
[Required] |
|
||||
[DynamicMaxLength(typeof(BlogConsts), nameof(BlogConsts.MaxNameLength))] |
|
||||
public string Name { get; set; } |
public string Name { get; set; } |
||||
|
|
||||
[Required] |
|
||||
[DynamicMaxLength(typeof(BlogConsts), nameof(BlogConsts.MaxSlugLength))] |
|
||||
public string Slug { get; set; } |
public string Slug { get; set; } |
||||
} |
} |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,17 @@ |
|||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
using Volo.Abp.Validation; |
||||
|
using Volo.CmsKit.Blogs; |
||||
|
|
||||
|
namespace Volo.CmsKit.Admin.Blogs |
||||
|
{ |
||||
|
public class CreateBlogDto |
||||
|
{ |
||||
|
[Required] |
||||
|
[DynamicMaxLength(typeof(BlogConsts), nameof(BlogConsts.MaxNameLength))] |
||||
|
public string Name { get; set; } |
||||
|
|
||||
|
[Required] |
||||
|
[DynamicMaxLength(typeof(BlogConsts), nameof(BlogConsts.MaxSlugLength))] |
||||
|
public string Slug { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
using Volo.Abp.Validation; |
||||
|
using Volo.CmsKit.Blogs; |
||||
|
|
||||
|
namespace Volo.CmsKit.Admin.Blogs |
||||
|
{ |
||||
|
public class UpdateBlogDto |
||||
|
{ |
||||
|
[Required] |
||||
|
[DynamicMaxLength(typeof(BlogConsts), nameof(BlogConsts.MaxNameLength))] |
||||
|
public string Name { get; set; } |
||||
|
|
||||
|
[Required] |
||||
|
[DynamicMaxLength(typeof(BlogConsts), nameof(BlogConsts.MaxSlugLength))] |
||||
|
public string Slug { get; set; } |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
using System.Threading.Tasks; |
||||
|
using JetBrains.Annotations; |
||||
|
using Volo.Abp.Domain.Services; |
||||
|
|
||||
|
namespace Volo.CmsKit.Blogs |
||||
|
{ |
||||
|
public class BlogManager : DomainService |
||||
|
{ |
||||
|
protected IBlogRepository BlogRepository { get; } |
||||
|
|
||||
|
public BlogManager(IBlogRepository blogRepository) |
||||
|
{ |
||||
|
BlogRepository = blogRepository; |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<Blog> CreateAsync([NotNull] string name, [NotNull] string slug) |
||||
|
{ |
||||
|
await CheckSlugAsync(slug); |
||||
|
|
||||
|
return new Blog(GuidGenerator.Create(), name, slug, CurrentTenant.Id); |
||||
|
} |
||||
|
|
||||
|
public virtual async Task<Blog> UpdateAsync([NotNull] Blog blog, [NotNull] string name, [NotNull] string slug) |
||||
|
{ |
||||
|
if (slug != blog.Slug) |
||||
|
{ |
||||
|
await CheckSlugAsync(slug); |
||||
|
} |
||||
|
|
||||
|
blog.SetName(name); |
||||
|
blog.SetSlug(slug); |
||||
|
|
||||
|
return blog; |
||||
|
} |
||||
|
|
||||
|
protected virtual async Task CheckSlugAsync([NotNull] string slug) |
||||
|
{ |
||||
|
if (await BlogRepository.SlugExistsAsync(slug)) |
||||
|
{ |
||||
|
throw new BlogSlugAlreadyExistException(slug); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using System; |
||||
|
using System.Runtime.Serialization; |
||||
|
using Volo.Abp; |
||||
|
|
||||
|
namespace Volo.CmsKit.Blogs |
||||
|
{ |
||||
|
[Serializable] |
||||
|
public class BlogSlugAlreadyExistException : BusinessException |
||||
|
{ |
||||
|
public BlogSlugAlreadyExistException(string slug) |
||||
|
{ |
||||
|
Code = CmsKitErrorCodes.Blogs.SlugAlreadyExists; |
||||
|
|
||||
|
WithData(nameof(Blog.Slug), slug); |
||||
|
} |
||||
|
|
||||
|
public BlogSlugAlreadyExistException( |
||||
|
SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,92 @@ |
|||||
|
using System.Linq; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Volo.Abp.Domain.Entities; |
||||
|
using Volo.CmsKit.Admin.Blogs; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.CmsKit.Blogs |
||||
|
{ |
||||
|
public class BlogAdminAppService_Tests : CmsKitApplicationTestBase |
||||
|
{ |
||||
|
protected IBlogAdminAppService BlogAdminAppService { get; } |
||||
|
protected CmsKitTestData CmsKitTestData { get; } |
||||
|
protected IBlogRepository BlogRepository { get; } |
||||
|
|
||||
|
public BlogAdminAppService_Tests() |
||||
|
{ |
||||
|
BlogAdminAppService = GetRequiredService<IBlogAdminAppService>(); |
||||
|
CmsKitTestData = GetRequiredService<CmsKitTestData>(); |
||||
|
BlogRepository = GetRequiredService<IBlogRepository>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetAsync() |
||||
|
{ |
||||
|
var blog = await BlogAdminAppService.GetAsync(CmsKitTestData.Blog_Id); |
||||
|
|
||||
|
blog.Slug.ShouldBe(CmsKitTestData.BlogSlug); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task GetListAsync() |
||||
|
{ |
||||
|
var blogs = await BlogAdminAppService.GetListAsync(new BlogGetListInput()); |
||||
|
|
||||
|
blogs.TotalCount.ShouldBeGreaterThan(0); |
||||
|
blogs.Items.Any(x => x.Slug == CmsKitTestData.BlogSlug).ShouldBeTrue(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task CreateAsync_ShouldWork() |
||||
|
{ |
||||
|
var blog = await BlogAdminAppService.CreateAsync(new CreateBlogDto |
||||
|
{ |
||||
|
Name = "News", |
||||
|
Slug = "latest-news" |
||||
|
}); |
||||
|
|
||||
|
blog.ShouldNotBeNull(); |
||||
|
blog.Name.ShouldBe("News"); |
||||
|
blog.Slug.ShouldBe("latest-news"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task CreateAsync_ShouldThrow_WithExistSlug() |
||||
|
{ |
||||
|
await Should.ThrowAsync<BlogSlugAlreadyExistException>( |
||||
|
async () => |
||||
|
await BlogAdminAppService.CreateAsync(new CreateBlogDto |
||||
|
{ |
||||
|
Name = "News", |
||||
|
Slug = CmsKitTestData.BlogSlug |
||||
|
})); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task UpdateAsync_ShouldWork() |
||||
|
{ |
||||
|
var blog = await BlogAdminAppService.UpdateAsync(CmsKitTestData.Blog_Id, new UpdateBlogDto |
||||
|
{ |
||||
|
Name = "New Name", |
||||
|
Slug = "new-slug" |
||||
|
}); |
||||
|
|
||||
|
var updatedBlog = await BlogAdminAppService.GetAsync(CmsKitTestData.Blog_Id); |
||||
|
|
||||
|
updatedBlog.Name.ShouldBe("New Name"); |
||||
|
updatedBlog.Slug.ShouldBe("new-slug"); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task DeleteAsync_ShouldWork() |
||||
|
{ |
||||
|
await BlogAdminAppService.DeleteAsync(CmsKitTestData.Blog_Id); |
||||
|
|
||||
|
await Should.ThrowAsync<EntityNotFoundException>( |
||||
|
async () => |
||||
|
await BlogAdminAppService.GetAsync(CmsKitTestData.Blog_Id) |
||||
|
); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,50 @@ |
|||||
|
using System; |
||||
|
using System.Threading.Tasks; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.CmsKit.Blogs |
||||
|
{ |
||||
|
public class BlogManager_Test : CmsKitDomainTestBase |
||||
|
{ |
||||
|
protected IBlogRepository BlogRepository { get; } |
||||
|
protected BlogManager BlogManager { get; } |
||||
|
protected CmsKitTestData TestData { get; } |
||||
|
|
||||
|
public BlogManager_Test() |
||||
|
{ |
||||
|
BlogRepository = GetRequiredService<IBlogRepository>(); |
||||
|
BlogManager = GetRequiredService<BlogManager>(); |
||||
|
TestData = GetRequiredService<CmsKitTestData>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task BlogCreate_ShouldThrowException_WithExistSlug() |
||||
|
{ |
||||
|
await Should.ThrowAsync<BlogSlugAlreadyExistException>( |
||||
|
async () => |
||||
|
await BlogManager.CreateAsync("test-name", TestData.BlogSlug) |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task BlogCreate_ShouldWorkProperly() |
||||
|
{ |
||||
|
var blog = await BlogManager.CreateAsync("test-name", "test-slug"); |
||||
|
|
||||
|
blog.ShouldNotBeNull(); |
||||
|
blog.Id.ShouldNotBe(Guid.Empty); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public async Task BlogUpdate_ShouldWork() |
||||
|
{ |
||||
|
var blog = await BlogRepository.GetAsync(TestData.Blog_Id); |
||||
|
|
||||
|
await BlogManager.UpdateAsync(blog, "New name", "new-slug"); |
||||
|
|
||||
|
blog.Name.ShouldBe("New name"); |
||||
|
blog.Slug.ShouldBe("new-slug"); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue