mirror of https://github.com/abpframework/abp.git
10 changed files with 205 additions and 4 deletions
@ -1,13 +1,61 @@ |
|||
using System; |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Domain.Entities.Auditing; |
|||
using Volo.CmsKit.Blogs; |
|||
|
|||
namespace Volo.CmsKit.Domain.Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogPost : FullAuditedEntity<Guid> |
|||
{ |
|||
public Guid BlogId { get; set; } |
|||
public string Title { get; set; } |
|||
public Guid BlogId { get; protected set; } |
|||
|
|||
public string Title { get; protected set; } |
|||
|
|||
public string UrlSlug { get; protected set; } |
|||
|
|||
public string CoverImageUrl { get; set; } |
|||
public bool IsPublished { get; set; } |
|||
|
|||
public bool IsPublished { get; protected set; } |
|||
|
|||
public DateTime? PublishDate { get; protected set; } |
|||
|
|||
protected BlogPost() |
|||
{ |
|||
} |
|||
|
|||
public BlogPost( |
|||
Guid blogId, |
|||
[NotNull] string title, |
|||
[NotNull] string urlSlug, |
|||
[CanBeNull] string coverImageUrl = null, |
|||
bool isPublished = true) |
|||
{ |
|||
BlogId = blogId; |
|||
SetTitle(title); |
|||
SetUrlSlug(urlSlug); |
|||
CoverImageUrl = coverImageUrl; |
|||
IsPublished = isPublished; |
|||
} |
|||
|
|||
public void SetTitle(string title) |
|||
{ |
|||
Title = Check.NotNullOrWhiteSpace(title, nameof(title), BlogPostConsts.MaxTitleLength); |
|||
} |
|||
|
|||
public void SetUrlSlug(string urlSlug) |
|||
{ |
|||
UrlSlug = Check.NotNullOrWhiteSpace(urlSlug, nameof(urlSlug), BlogPostConsts.MaxUrlSlugLength, BlogPostConsts.MinUrlSlugLength); |
|||
} |
|||
|
|||
public void SetIsPublished(bool isPublished) |
|||
{ |
|||
if (!IsPublished && isPublished) |
|||
{ |
|||
this.PublishDate = DateTime.UtcNow; |
|||
} |
|||
|
|||
IsPublished = IsPublished; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.Domain.Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogPostManager : IBlogPostManager |
|||
{ |
|||
protected readonly IBlogPostRepository _blogPostRepository; |
|||
|
|||
public BlogPostManager(IBlogPostRepository blogPostRepository) |
|||
{ |
|||
_blogPostRepository = blogPostRepository; |
|||
} |
|||
|
|||
public async Task<BlogPost> CreateAsync(BlogPost blogPost) |
|||
{ |
|||
await CheckUrlSlugExistenceAsync(blogPost.UrlSlug); |
|||
|
|||
return await _blogPostRepository.InsertAsync(blogPost); |
|||
} |
|||
|
|||
public async Task UpdateAsync(BlogPost blogPost) |
|||
{ |
|||
await CheckUrlSlugExistenceAsync(blogPost.UrlSlug); |
|||
|
|||
await _blogPostRepository.UpdateAsync(blogPost); |
|||
} |
|||
|
|||
private async Task CheckUrlSlugExistenceAsync(string urlSlug) |
|||
{ |
|||
if (await _blogPostRepository.SlugExistsAsync(urlSlug)) |
|||
{ |
|||
throw new BlogPostUrlSlugAlreadyExistException(urlSlug); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.Domain.Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogPostUrlSlugAlreadyExistException : BusinessException |
|||
{ |
|||
internal BlogPostUrlSlugAlreadyExistException(string code = null, string message = null, string details = null, Exception innerException = null, Microsoft.Extensions.Logging.LogLevel logLevel = Microsoft.Extensions.Logging.LogLevel.Warning) : base(code, message, details, innerException, logLevel) |
|||
{ |
|||
} |
|||
|
|||
internal BlogPostUrlSlugAlreadyExistException(System.Runtime.Serialization.SerializationInfo serializationInfo, System.Runtime.Serialization.StreamingContext context) : base(serializationInfo, context) |
|||
{ |
|||
} |
|||
|
|||
public BlogPostUrlSlugAlreadyExistException(string urlSlug) |
|||
{ |
|||
Code = CmsKitErrorCodes.Blogs.UrlSlugAlreadyExist; |
|||
|
|||
WithData(nameof(BlogPost.UrlSlug), urlSlug); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.Domain.Volo.CmsKit.Blogs |
|||
{ |
|||
public interface IBlogPostManager |
|||
{ |
|||
Task<BlogPost> CreateAsync(BlogPost blogPost); |
|||
|
|||
Task UpdateAsync(BlogPost blogPost); |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Repositories; |
|||
|
|||
namespace Volo.CmsKit.Domain.Volo.CmsKit.Blogs |
|||
{ |
|||
public interface IBlogPostRepository : IBasicRepository<BlogPost, Guid> |
|||
{ |
|||
Task<bool> SlugExistsAsync(string slug); |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using Microsoft.EntityFrameworkCore; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Repositories.EntityFrameworkCore; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
using Volo.CmsKit.Domain.Volo.CmsKit.Blogs; |
|||
using Volo.CmsKit.EntityFrameworkCore; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogPostEfCoreRepository : EfCoreRepository<CmsKitDbContext, BlogPost, Guid>, IBlogPostRepository |
|||
{ |
|||
public BlogPostEfCoreRepository(IDbContextProvider<CmsKitDbContext> dbContextProvider) : base(dbContextProvider) |
|||
{ |
|||
} |
|||
|
|||
public async Task<bool> SlugExistsAsync(string slug) |
|||
{ |
|||
var dbSet = await GetDbSetAsync(); |
|||
|
|||
return await dbSet.AnyAsync(x => x.UrlSlug.ToLower() == slug); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Repositories.MongoDB; |
|||
using Volo.Abp.MongoDB; |
|||
using Volo.CmsKit.Domain.Volo.CmsKit.Blogs; |
|||
|
|||
namespace Volo.CmsKit.MongoDB.Blogs |
|||
{ |
|||
public class BlogPostMongoRepository : MongoDbRepository<CmsKitMongoDbContext, BlogPost, Guid>, IBlogPostRepository |
|||
{ |
|||
public BlogPostMongoRepository(IMongoDbContextProvider<CmsKitMongoDbContext> dbContextProvider) : base(dbContextProvider) |
|||
{ |
|||
} |
|||
|
|||
public async Task<bool> SlugExistsAsync(string slug) |
|||
{ |
|||
var queryable = await GetQueryableAsync(); |
|||
|
|||
return await AsyncExecuter.AnyAsync(queryable, x => x.UrlSlug.ToLower() == slug); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue