Browse Source

CmsKit - Refactoring for blogs domain

pull/7763/head
enisn 6 years ago
parent
commit
edae47d9e0
  1. 12
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/Blog.cs
  2. 17
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPost.cs
  3. 29
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs
  4. 7
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostSlugAlreadyExistException.cs
  5. 5
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogPostManager.cs
  6. 5
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs
  7. 9
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs
  8. 5
      modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs

12
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/Blog.cs

@ -20,18 +20,20 @@ namespace Volo.CmsKit.Blogs
TenantId = tenantId; TenantId = tenantId;
} }
public string Name { get; protected set; } [NotNull]
public virtual string Name { get; protected set; }
public string Slug { get; protected set; } [NotNull]
public virtual string Slug { get; protected set; }
public Guid? TenantId { get; protected set; } public virtual Guid? TenantId { get; protected set; }
public void SetName(string name) public virtual void SetName(string name)
{ {
Name = Check.NotNullOrWhiteSpace(name, nameof(name), maxLength: BlogConsts.MaxNameLength); Name = Check.NotNullOrWhiteSpace(name, nameof(name), maxLength: BlogConsts.MaxNameLength);
} }
public void SetSlug(string slug) public virtual void SetSlug(string slug)
{ {
Check.NotNullOrWhiteSpace(slug, nameof(slug), maxLength: BlogConsts.MaxNameLength); Check.NotNullOrWhiteSpace(slug, nameof(slug), maxLength: BlogConsts.MaxNameLength);

17
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPost.cs

@ -12,15 +12,18 @@ namespace Volo.CmsKit.Blogs
{ {
public class BlogPost : FullAuditedAggregateRootWithUser<Guid, CmsUser>, IMultiTenant public class BlogPost : FullAuditedAggregateRootWithUser<Guid, CmsUser>, IMultiTenant
{ {
public Guid BlogId { get; protected set; } public virtual Guid BlogId { get; protected set; }
public string Title { get; protected set; } [NotNull]
public virtual string Title { get; protected set; }
public string Slug { get; protected set; } [NotNull]
public virtual string Slug { get; protected set; }
public string ShortDescription { get; protected set; } [NotNull]
public virtual string ShortDescription { get; protected set; }
public Guid? TenantId { get; protected set; } public virtual Guid? TenantId { get; protected set; }
protected BlogPost() protected BlogPost()
{ {
@ -39,7 +42,7 @@ namespace Volo.CmsKit.Blogs
ShortDescription = shortDescription; ShortDescription = shortDescription;
} }
public void SetTitle(string title) public virtual void SetTitle(string title)
{ {
Title = Check.NotNullOrWhiteSpace(title, nameof(title), BlogPostConsts.MaxTitleLength); Title = Check.NotNullOrWhiteSpace(title, nameof(title), BlogPostConsts.MaxTitleLength);
} }
@ -51,7 +54,7 @@ namespace Volo.CmsKit.Blogs
Slug = slug.NormalizeSlug(); Slug = slug.NormalizeSlug();
} }
public void SetShortDescription(string shortDescription) public virtual void SetShortDescription(string shortDescription)
{ {
ShortDescription = Check.Length(shortDescription, nameof(shortDescription), BlogPostConsts.MaxShortDescriptionLength); ShortDescription = Check.Length(shortDescription, nameof(shortDescription), BlogPostConsts.MaxShortDescriptionLength);
} }

29
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs

@ -1,44 +1,49 @@
using System; using JetBrains.Annotations;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Services; using Volo.Abp.Domain.Services;
using Volo.Abp.VirtualFileSystem;
namespace Volo.CmsKit.Blogs namespace Volo.CmsKit.Blogs
{ {
public class BlogPostManager : DomainService, IBlogPostManager public class BlogPostManager : DomainService, IBlogPostManager
{ {
protected readonly IBlogPostRepository blogPostRepository; protected IBlogPostRepository BlogPostRepository { get; }
protected readonly IBlogRepository blogRepository; protected IBlogRepository BlogRepository { get; }
public BlogPostManager( public BlogPostManager(
IBlogPostRepository blogPostRepository, IBlogPostRepository blogPostRepository,
IBlogRepository blogRepository) IBlogRepository blogRepository)
{ {
this.blogPostRepository = blogPostRepository; this.BlogPostRepository = blogPostRepository;
this.blogRepository = blogRepository; this.BlogRepository = blogRepository;
} }
public async Task<BlogPost> CreateAsync(BlogPost blogPost) public virtual async Task<BlogPost> CreateAsync(BlogPost blogPost)
{ {
await CheckBlogExistenceAsync(blogPost.BlogId); await CheckBlogExistenceAsync(blogPost.BlogId);
await CheckSlugExistenceAsync(blogPost.BlogId, blogPost.Slug); await CheckSlugExistenceAsync(blogPost.BlogId, blogPost.Slug);
return await blogPostRepository.InsertAsync(blogPost); return await BlogPostRepository.InsertAsync(blogPost);
} }
public async Task UpdateAsync(BlogPost blogPost) public virtual async Task UpdateAsync(BlogPost blogPost)
{ {
await CheckBlogExistenceAsync(blogPost.BlogId); await CheckBlogExistenceAsync(blogPost.BlogId);
await blogPostRepository.UpdateAsync(blogPost); await BlogPostRepository.UpdateAsync(blogPost);
} }
public async Task SetSlugUrlAsync(BlogPost blogPost, string newSlug) public virtual async Task SetSlugUrlAsync(BlogPost blogPost, [NotNull] string newSlug)
{ {
Check.NotNullOrWhiteSpace(newSlug, nameof(newSlug));
await CheckSlugExistenceAsync(blogPost.BlogId, newSlug); await CheckSlugExistenceAsync(blogPost.BlogId, newSlug);
blogPost.SetSlug(newSlug); blogPost.SetSlug(newSlug);
@ -46,7 +51,7 @@ namespace Volo.CmsKit.Blogs
private async Task CheckSlugExistenceAsync(Guid blogId, string slug) private async Task CheckSlugExistenceAsync(Guid blogId, string slug)
{ {
if (await blogPostRepository.SlugExistsAsync(blogId, slug)) if (await BlogPostRepository.SlugExistsAsync(blogId, slug))
{ {
throw new BlogPostSlugAlreadyExistException(blogId, slug); throw new BlogPostSlugAlreadyExistException(blogId, slug);
} }
@ -54,7 +59,7 @@ namespace Volo.CmsKit.Blogs
private async Task CheckBlogExistenceAsync(Guid blogId) private async Task CheckBlogExistenceAsync(Guid blogId)
{ {
if (!await blogRepository.ExistsAsync(blogId)) if (!await BlogRepository.ExistsAsync(blogId))
throw new EntityNotFoundException(typeof(Blog), blogId); throw new EntityNotFoundException(typeof(Blog), blogId);
} }
} }

7
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostSlugAlreadyExistException.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Runtime.Serialization;
using Volo.Abp; using Volo.Abp;
namespace Volo.CmsKit.Blogs namespace Volo.CmsKit.Blogs
@ -9,7 +10,7 @@ namespace Volo.CmsKit.Blogs
{ {
} }
internal BlogPostSlugAlreadyExistException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext context) : base(serializationInfo, context) internal BlogPostSlugAlreadyExistException(SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext context) : base(serializationInfo, context)
{ {
} }
@ -24,8 +25,8 @@ namespace Volo.CmsKit.Blogs
WithData(nameof(BlogId), BlogId); WithData(nameof(BlogId), BlogId);
} }
public string Slug { get; } public virtual string Slug { get; }
public Guid BlogId { get; } public virtual Guid BlogId { get; }
} }
} }

5
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogPostManager.cs

@ -1,4 +1,5 @@
using System; using JetBrains.Annotations;
using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Volo.CmsKit.Blogs namespace Volo.CmsKit.Blogs
@ -9,6 +10,6 @@ namespace Volo.CmsKit.Blogs
Task UpdateAsync(BlogPost blogPost); Task UpdateAsync(BlogPost blogPost);
Task SetSlugUrlAsync(BlogPost blogPost, string newSlug); Task SetSlugUrlAsync(BlogPost blogPost, [NotNull] string newSlug);
} }
} }

5
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs

@ -1,4 +1,5 @@
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
@ -6,7 +7,7 @@ namespace Volo.CmsKit.Blogs
{ {
public interface IBlogRepository : IBasicRepository<Blog, Guid> public interface IBlogRepository : IBasicRepository<Blog, Guid>
{ {
public Task<Blog> GetBySlugAsync(string slug); public Task<Blog> GetBySlugAsync(string slug, CancellationToken cancellationToken = default);
Task<bool> ExistsAsync(Guid blogId); Task<bool> ExistsAsync(Guid blogId, CancellationToken cancellationToken = default);
} }
} }

9
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs

@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System; using System;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore;
@ -14,14 +15,14 @@ namespace Volo.CmsKit.Blogs
{ {
} }
public async Task<bool> ExistsAsync(Guid blogId) public virtual async Task<bool> ExistsAsync(Guid blogId, CancellationToken cancellationToken = default)
{ {
return await (await GetQueryableAsync()).AnyAsync(x => x.Id == blogId); return await (await GetQueryableAsync()).AnyAsync(x => x.Id == blogId, cancellationToken);
} }
public Task<Blog> GetBySlugAsync(string slug) public virtual Task<Blog> GetBySlugAsync(string slug, CancellationToken cancellationToken = default)
{ {
return GetAsync(x => x.Slug == slug); return GetAsync(x => x.Slug == slug, cancellationToken: cancellationToken);
} }
} }
} }

5
modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs

@ -1,5 +1,6 @@
using MongoDB.Driver.Core.Operations; using MongoDB.Driver.Core.Operations;
using System; using System;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.Domain.Repositories.MongoDB;
using Volo.Abp.MongoDB; using Volo.Abp.MongoDB;
@ -13,14 +14,14 @@ namespace Volo.CmsKit.MongoDB.Blogs
{ {
} }
public async Task<bool> ExistsAsync(Guid blogId) public virtual async Task<bool> ExistsAsync(Guid blogId, CancellationToken cancellationToken = default)
{ {
return await AsyncExecuter.AnyAsync( return await AsyncExecuter.AnyAsync(
await GetQueryableAsync(), await GetQueryableAsync(),
x => x.Id == blogId); x => x.Id == blogId);
} }
public Task<Blog> GetBySlugAsync(string slug) public virtual Task<Blog> GetBySlugAsync(string slug, CancellationToken cancellationToken = default)
{ {
return GetAsync(x => x.Slug == slug); return GetAsync(x => x.Slug == slug);
} }

Loading…
Cancel
Save