From f228768ee804f9e7531b287ede1ffa48506c7375 Mon Sep 17 00:00:00 2001 From: enisn Date: Wed, 3 Feb 2021 16:01:42 +0300 Subject: [PATCH] CmsKit - Add Existence check for blog --- .../Volo/CmsKit/Blogs/BlogPostManager.cs | 4 +++- .../Volo/CmsKit/Blogs/IBlogRepository.cs | 1 + .../Volo/CmsKit/Blogs/EfCoreBlogRepository.cs | 9 ++++++++- .../MongoDB/Blogs/MongoBlogRepository.cs | 10 +++++++++- .../Blogs/BlogRepository_Test.cs | 18 ++++++++++++++++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs index 61198e0fad..228d8eccbf 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/BlogPostManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Services; namespace Volo.CmsKit.Blogs @@ -48,7 +49,8 @@ namespace Volo.CmsKit.Blogs private async Task CheckBlogExistenceAsync(Guid blogId) { - await blogRepository.GetAsync(blogId); + if (!await blogRepository.ExistsAsync(blogId)) + throw new EntityNotFoundException(typeof(Blog), blogId); } } } diff --git a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs index 152c17d615..bbff825189 100644 --- a/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Blogs/IBlogRepository.cs @@ -7,5 +7,6 @@ namespace Volo.CmsKit.Blogs public interface IBlogRepository : IBasicRepository { public Task GetByUrlSlugAsync(string urlSlug); + Task ExistsAsync(Guid blogId); } } diff --git a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs index 76211780f6..677857d2b6 100644 --- a/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Blogs/EfCoreBlogRepository.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; +using System.Security.Cryptography.X509Certificates; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.EntityFrameworkCore; using Volo.Abp.EntityFrameworkCore; @@ -12,6 +14,11 @@ namespace Volo.CmsKit.Blogs { } + public async Task ExistsAsync(Guid blogId) + { + return await (await GetQueryableAsync()).AnyAsync(x => x.Id == blogId); + } + public Task GetByUrlSlugAsync(string urlSlug) { return GetAsync(x => x.UrlSlug == urlSlug); diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs index 998edf6e67..b1d9d3167b 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Blogs/MongoBlogRepository.cs @@ -1,4 +1,5 @@ -using System; +using MongoDB.Driver.Core.Operations; +using System; using System.Threading.Tasks; using Volo.Abp.Domain.Repositories.MongoDB; using Volo.Abp.MongoDB; @@ -12,6 +13,13 @@ namespace Volo.CmsKit.MongoDB.Blogs { } + public async Task ExistsAsync(Guid blogId) + { + return await AsyncExecuter.AnyAsync( + await GetQueryableAsync(), + x => x.Id == blogId); + } + public Task GetByUrlSlugAsync(string urlSlug) { return GetAsync(x => x.UrlSlug == urlSlug); diff --git a/modules/cms-kit/test/Volo.CmsKit.TestBase/Blogs/BlogRepository_Test.cs b/modules/cms-kit/test/Volo.CmsKit.TestBase/Blogs/BlogRepository_Test.cs index 1f425ff7f7..5119dfd75b 100644 --- a/modules/cms-kit/test/Volo.CmsKit.TestBase/Blogs/BlogRepository_Test.cs +++ b/modules/cms-kit/test/Volo.CmsKit.TestBase/Blogs/BlogRepository_Test.cs @@ -43,5 +43,23 @@ namespace Volo.CmsKit.Blogs exception.ShouldNotBeNull(); exception.EntityType.ShouldBe(typeof(Blog)); } + + [Fact] + public async Task ExistsAsync_ShouldReturnTrue_WithExistingId() + { + var result = await blogRepository.ExistsAsync(testData.Blog_Id); + + result.ShouldBeTrue(); + } + + [Fact] + public async Task ExistsAsync_ShouldReturnFalse_WithExistingId() + { + var nonExistingId = Guid.NewGuid(); + + var result = await blogRepository.ExistsAsync(nonExistingId); + + result.ShouldBeFalse(); + } } }