mirror of https://github.com/abpframework/abp.git
committed by
GitHub
34 changed files with 400 additions and 344 deletions
@ -0,0 +1,12 @@ |
|||
using System; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
[Serializable] |
|||
public class BlogFeatureCacheItem |
|||
{ |
|||
public Guid Id { get; set; } |
|||
public string FeatureName { get; set; } |
|||
public bool IsEnabled { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogFeatureCacheKey |
|||
{ |
|||
public BlogFeatureCacheKey(Guid id, [NotNull] string featureName) |
|||
{ |
|||
Id = id; |
|||
FeatureName = Check.NotNullOrEmpty(featureName, nameof(featureName)); |
|||
} |
|||
|
|||
public Guid Id { get; set; } |
|||
|
|||
public string FeatureName { get; set; } |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return $"BlogFeature_{Id}_{FeatureName}"; |
|||
} |
|||
} |
|||
} |
|||
@ -1,14 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public interface IBlogFeatureCacheManager |
|||
{ |
|||
Task<BlogFeatureDto> AddOrGetAsync(Guid blogId, string featureName, Func<Task<BlogFeatureDto>> factory); |
|||
Task ClearAsync(Guid blogId, string featureName); |
|||
} |
|||
} |
|||
@ -1,46 +0,0 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.Caching.Distributed; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogFeatureCacheManager : IBlogFeatureCacheManager, ITransientDependency |
|||
{ |
|||
protected IDistributedCache<BlogFeatureDto> Cache { get; } |
|||
|
|||
public BlogFeatureCacheManager(IDistributedCache<BlogFeatureDto> cache) |
|||
{ |
|||
Cache = cache; |
|||
} |
|||
|
|||
public async Task<BlogFeatureDto> AddOrGetAsync(Guid blogId, string featureName, Func<Task<BlogFeatureDto>> factory) |
|||
{ |
|||
return await Cache.GetOrAddAsync( |
|||
GetCacheKey(blogId, featureName), |
|||
factory, |
|||
() => new DistributedCacheEntryOptions |
|||
{ |
|||
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1) |
|||
}); |
|||
} |
|||
|
|||
public Task ClearAsync(Guid blogId, string featureName) |
|||
{ |
|||
return Cache.RemoveAsync(GetCacheKey(blogId, featureName)); |
|||
} |
|||
|
|||
private string GetCacheKey(Guid blogId, [NotNull] string featureName) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(featureName, nameof(featureName)); |
|||
|
|||
return $"{blogId}_{featureName}"; |
|||
} |
|||
} |
|||
} |
|||
@ -1,22 +1,38 @@ |
|||
using System.Threading.Tasks; |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Caching; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Domain.Entities.Events; |
|||
using Volo.Abp.EventBus; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogFeatureChangedHandler : IDistributedEventHandler<BlogFeatureChangedEto>, ITransientDependency |
|||
public class BlogFeatureChangedHandler : |
|||
ILocalEventHandler<EntityCreatedEventData<BlogFeature>>, |
|||
ILocalEventHandler<EntityUpdatedEventData<BlogFeature>>, |
|||
ITransientDependency |
|||
{ |
|||
protected IDistributedCache<BlogFeatureDto> Cache { get; } |
|||
protected IDistributedCache<BlogFeatureCacheItem, BlogFeatureCacheKey> Cache { get; } |
|||
|
|||
public BlogFeatureChangedHandler(IDistributedCache<BlogFeatureDto> cache) |
|||
public BlogFeatureChangedHandler( |
|||
IDistributedCache<BlogFeatureCacheItem, BlogFeatureCacheKey> cache) |
|||
{ |
|||
Cache = cache; |
|||
} |
|||
|
|||
public async Task HandleEventAsync(BlogFeatureChangedEto eventData) |
|||
public Task RemoveFromCacheAsync(Guid blogId, string featureName) |
|||
{ |
|||
await Cache.RemoveAsync($"{eventData.BlogId}_{eventData.FeatureName}"); |
|||
return Cache.RemoveAsync(new BlogFeatureCacheKey(blogId, featureName)); |
|||
} |
|||
|
|||
public Task HandleEventAsync(EntityCreatedEventData<BlogFeature> eventData) |
|||
{ |
|||
return RemoveFromCacheAsync(eventData.Entity.BlogId, eventData.Entity.FeatureName); |
|||
} |
|||
|
|||
public Task HandleEventAsync(EntityUpdatedEventData<BlogFeature> eventData) |
|||
{ |
|||
return RemoveFromCacheAsync(eventData.Entity.BlogId, eventData.Entity.FeatureName); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -1,17 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.EventBus; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
[EventName("CmsKit.Blogs.BlogFeature.Changed")] |
|||
public class BlogFeatureChangedEto |
|||
{ |
|||
public Guid BlogId { get; set; } |
|||
public string FeatureName { get; set; } |
|||
public bool IsEnabled { get; set; } |
|||
} |
|||
} |
|||
@ -1,17 +0,0 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.CmsKit.Blogs; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public interface IBlogFeatureManager : IDomainService |
|||
{ |
|||
Task<List<BlogFeature>> GetListAsync(Guid blogId); |
|||
|
|||
Task SetAsync(Guid blogId, string featureName, bool isEnabled); |
|||
} |
|||
} |
|||
@ -1,14 +0,0 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public interface IBlogPostManager |
|||
{ |
|||
Task<BlogPost> CreateAsync(BlogPost blogPost); |
|||
|
|||
Task UpdateAsync(BlogPost blogPost); |
|||
|
|||
Task SetSlugUrlAsync(BlogPost blogPost, [NotNull] string newSlug); |
|||
} |
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
using Shouldly; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Application.Dtos; |
|||
using Volo.Abp.Domain.Entities; |
|||
using Volo.CmsKit.Public.Blogs; |
|||
using Xunit; |
|||
|
|||
namespace Volo.CmsKit.Blogs |
|||
{ |
|||
public class BlogPostPublicAppService_Tests : CmsKitApplicationTestBase |
|||
{ |
|||
private readonly IBlogPostPublicAppService blogPostAppService; |
|||
|
|||
private readonly CmsKitTestData cmsKitTestData; |
|||
|
|||
public BlogPostPublicAppService_Tests() |
|||
{ |
|||
blogPostAppService = GetRequiredService<IBlogPostPublicAppService>(); |
|||
cmsKitTestData = GetRequiredService<CmsKitTestData>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetListAsync_ShouldWorkProperly_WithExistingBlog() |
|||
{ |
|||
var blogPosts = await blogPostAppService.GetListAsync(cmsKitTestData.BlogSlug, new PagedAndSortedResultRequestDto { MaxResultCount = 2 }); |
|||
|
|||
blogPosts.ShouldNotBeNull(); |
|||
blogPosts.TotalCount.ShouldBe(2); |
|||
blogPosts.Items.ShouldNotBeEmpty(); |
|||
blogPosts.Items.Count.ShouldBe(2); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetAsync_ShouldWorkProperly_WithExistingSlug() |
|||
{ |
|||
var blogPost = await blogPostAppService.GetAsync(cmsKitTestData.BlogSlug, cmsKitTestData.BlogPost_1_Slug); |
|||
|
|||
blogPost.Id.ShouldBe(cmsKitTestData.BlogPost_1_Id); |
|||
blogPost.Title.ShouldBe(cmsKitTestData.BlogPost_1_Title); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetAsync_ShouldThrowException_WithNonExistingBlogPostSlug() |
|||
{ |
|||
var nonExistingSlug = "any-other-url"; |
|||
var exception = await Should.ThrowAsync<EntityNotFoundException>(async () => |
|||
await blogPostAppService.GetAsync(cmsKitTestData.BlogSlug, nonExistingSlug)); |
|||
|
|||
exception.EntityType.ShouldBe(typeof(BlogPost)); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task GetAsync_ShouldThrowException_WithNonExistingBlogSlug() |
|||
{ |
|||
var nonExistingSlug = "any-other-url"; |
|||
var exception = await Should.ThrowAsync<EntityNotFoundException>(async () => |
|||
await blogPostAppService.GetAsync(nonExistingSlug, cmsKitTestData.Page_1_Slug)); |
|||
|
|||
exception.EntityType.ShouldBe(typeof(Blog)); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue