From 18472daf14fff04591c6f6411da163751a30544b Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 14 Nov 2018 15:57:42 +0300 Subject: [PATCH] [Blog module] MongoDB integration #581 --- modules/blogging/Volo.Blogging.sln | 20 ++++++- .../Volo.BloggingTestApp.MongoDb.csproj | 15 +++++ .../MongoDb/BloggingTestAppMongoDbModule.cs | 21 +++++++ .../BloggingTestAppModule.cs | 16 +++++- .../Volo.BloggingTestApp.csproj | 1 + .../Volo/Blogging/Posts/PostAppService.cs | 2 +- .../Blogging/Comments/ICommentRepository.cs | 2 +- .../Volo/Blogging/Posts/IPostRepository.cs | 2 +- .../Comments/EfCoreCommentRepository.cs | 2 +- .../Blogging/Posts/EfCorePostRepository.cs | 4 +- ...ository.cs => EfCoreBlogUserRepository.cs} | 0 .../Volo.Blogging.MongoDB.csproj | 16 ++++++ .../Blogging/Blogs/MongoBlogRepository.cs | 38 +++++++++++++ .../Comments/MongoCommentRepository.cs | 49 ++++++++++++++++ .../MongoDB/AbpBloggingBsonClassMap.cs | 27 +++++++++ .../MongoDB/BloggingMongoDbContext.cs | 41 ++++++++++++++ .../BloggingMongoDbContextExtensions.cs | 56 +++++++++++++++++++ .../Blogging/MongoDB/BloggingMongoDbModule.cs | 33 +++++++++++ ...ngMongoModelBuilderConfigurationOptions.cs | 16 ++++++ .../MongoDB/IBloggingMongoDbContext.cs | 27 +++++++++ .../Blogging/Posts/MongoPostRepository.cs | 36 ++++++++++++ .../Blogging/Posts/MongoPostTagRepository.cs | 30 ++++++++++ .../Blogging/Tagging/MongoTagRepository.cs | 50 +++++++++++++++++ .../Blogging/Users/MongoBlogUserRepository.cs | 30 ++++++++++ 24 files changed, 523 insertions(+), 11 deletions(-) create mode 100644 modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo.BloggingTestApp.MongoDb.csproj create mode 100644 modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo/BloggingTestApp/MongoDb/BloggingTestAppMongoDbModule.cs rename modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/{EfCoreQaUserRepository.cs => EfCoreBlogUserRepository.cs} (100%) create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo.Blogging.MongoDB.csproj create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Blogs/MongoBlogRepository.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Comments/MongoCommentRepository.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/AbpBloggingBsonClassMap.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoModelBuilderConfigurationOptions.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostTagRepository.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Tagging/MongoTagRepository.cs create mode 100644 modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Users/MongoBlogUserRepository.cs diff --git a/modules/blogging/Volo.Blogging.sln b/modules/blogging/Volo.Blogging.sln index f9a49d767f..05982d4524 100644 --- a/modules/blogging/Volo.Blogging.sln +++ b/modules/blogging/Volo.Blogging.sln @@ -29,11 +29,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{42BF26EF-B8C EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{25B3A516-5C0D-42E3-9294-E8A9346CEE4B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Blogging.TestBase", "test\Volo.Blogging.TestBase\Volo.Blogging.TestBase.csproj", "{BBE8D8BE-1B24-49FE-86EF-3848D4BB6829}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Blogging.TestBase", "test\Volo.Blogging.TestBase\Volo.Blogging.TestBase.csproj", "{BBE8D8BE-1B24-49FE-86EF-3848D4BB6829}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Blogging.EntityFrameworkCore.Tests", "test\Volo.Blogging.EntityFrameworkCore.Tests\Volo.Blogging.EntityFrameworkCore.Tests.csproj", "{0B9AAD44-1FCF-4AF1-838F-A09446E98E37}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Blogging.EntityFrameworkCore.Tests", "test\Volo.Blogging.EntityFrameworkCore.Tests\Volo.Blogging.EntityFrameworkCore.Tests.csproj", "{0B9AAD44-1FCF-4AF1-838F-A09446E98E37}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Blogging.Application.Tests", "test\Volo.Blogging.Application.Tests\Volo.Blogging.Application.Tests.csproj", "{C949B953-80B3-4B36-B535-1AD74A34FEAC}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Volo.Blogging.Application.Tests", "test\Volo.Blogging.Application.Tests\Volo.Blogging.Application.Tests.csproj", "{C949B953-80B3-4B36-B535-1AD74A34FEAC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.Blogging.MongoDB", "src\Volo.Blogging.MongoDB\Volo.Blogging.MongoDB.csproj", "{98C2D36A-F874-405D-8565-9CE59438E879}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Volo.BloggingTestApp.MongoDb", "app\Volo.BloggingTestApp.MongoDb\Volo.BloggingTestApp.MongoDb.csproj", "{4C818374-2DE8-422F-8585-975E8366DB26}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -93,6 +97,14 @@ Global {C949B953-80B3-4B36-B535-1AD74A34FEAC}.Debug|Any CPU.Build.0 = Debug|Any CPU {C949B953-80B3-4B36-B535-1AD74A34FEAC}.Release|Any CPU.ActiveCfg = Release|Any CPU {C949B953-80B3-4B36-B535-1AD74A34FEAC}.Release|Any CPU.Build.0 = Release|Any CPU + {98C2D36A-F874-405D-8565-9CE59438E879}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {98C2D36A-F874-405D-8565-9CE59438E879}.Debug|Any CPU.Build.0 = Debug|Any CPU + {98C2D36A-F874-405D-8565-9CE59438E879}.Release|Any CPU.ActiveCfg = Release|Any CPU + {98C2D36A-F874-405D-8565-9CE59438E879}.Release|Any CPU.Build.0 = Release|Any CPU + {4C818374-2DE8-422F-8585-975E8366DB26}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4C818374-2DE8-422F-8585-975E8366DB26}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4C818374-2DE8-422F-8585-975E8366DB26}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4C818374-2DE8-422F-8585-975E8366DB26}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -111,6 +123,8 @@ Global {BBE8D8BE-1B24-49FE-86EF-3848D4BB6829} = {25B3A516-5C0D-42E3-9294-E8A9346CEE4B} {0B9AAD44-1FCF-4AF1-838F-A09446E98E37} = {25B3A516-5C0D-42E3-9294-E8A9346CEE4B} {C949B953-80B3-4B36-B535-1AD74A34FEAC} = {25B3A516-5C0D-42E3-9294-E8A9346CEE4B} + {98C2D36A-F874-405D-8565-9CE59438E879} = {42BF26EF-B8C7-42DC-9FFB-3653109B7776} + {4C818374-2DE8-422F-8585-975E8366DB26} = {EB4FB44A-FE39-4245-9DAD-D6437BCE3870} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {F2BAE819-78D4-407A-9201-22473B2850B0} diff --git a/modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo.BloggingTestApp.MongoDb.csproj b/modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo.BloggingTestApp.MongoDb.csproj new file mode 100644 index 0000000000..6893488771 --- /dev/null +++ b/modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo.BloggingTestApp.MongoDb.csproj @@ -0,0 +1,15 @@ + + + + netcoreapp2.1 + + + + + + + + + + + diff --git a/modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo/BloggingTestApp/MongoDb/BloggingTestAppMongoDbModule.cs b/modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo/BloggingTestApp/MongoDb/BloggingTestAppMongoDbModule.cs new file mode 100644 index 0000000000..a03c4eb14b --- /dev/null +++ b/modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo/BloggingTestApp/MongoDb/BloggingTestAppMongoDbModule.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp.Identity.MongoDB; +using Volo.Abp.Modularity; +using Volo.Abp.PermissionManagement.MongoDB; +using Volo.Abp.SettingManagement.MongoDB; +using Volo.Blogging.MongoDB; + +namespace Volo.BloggingTestApp.MongoDb +{ + [DependsOn( + typeof(AbpIdentityMongoDbModule), + typeof(BloggingMongoDbModule), + typeof(AbpSettingManagementMongoDbModule), + typeof(AbpPermissionManagementMongoDbModule) + )] + public class BloggingTestAppMongoDbModule : AbpModule + { + } +} diff --git a/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs b/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs index eb5d43dfb4..9b0f17cf11 100644 --- a/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs +++ b/modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +//#define MONGODB + +using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; @@ -28,13 +30,18 @@ using Volo.Abp.UI; using Volo.Abp.VirtualFileSystem; using Volo.Blogging; using Volo.BloggingTestApp.EntityFrameworkCore; +using Volo.BloggingTestApp.MongoDb; namespace Volo.BloggingTestApp { [DependsOn( typeof(BloggingWebModule), typeof(BloggingApplicationModule), +#if MONGODB + typeof(BloggingTestAppMongoDbModule), +#else typeof(BloggingTestAppEntityFrameworkCoreModule), +#endif typeof(AbpAccountWebModule), typeof(AbpIdentityWebModule), typeof(AbpIdentityApplicationModule), @@ -50,15 +57,20 @@ namespace Volo.BloggingTestApp context.Services.Configure(options => { +#if MONGODB + const string connStringName = "MongoDb"; +#else const string connStringName = "SqlServer"; +#endif options.ConnectionStrings.Default = configuration.GetConnectionString(connStringName); }); +#if !MONGODB context.Services.Configure(options => { options.UseSqlServer(); }); - +#endif if (hostingEnvironment.IsDevelopment()) { context.Services.Configure(options => diff --git a/modules/blogging/app/Volo.BloggingTestApp/Volo.BloggingTestApp.csproj b/modules/blogging/app/Volo.BloggingTestApp/Volo.BloggingTestApp.csproj index 67676321a9..9d8489f632 100644 --- a/modules/blogging/app/Volo.BloggingTestApp/Volo.BloggingTestApp.csproj +++ b/modules/blogging/app/Volo.BloggingTestApp/Volo.BloggingTestApp.csproj @@ -24,6 +24,7 @@ + diff --git a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs index 4a8f683d7e..09671ca30b 100644 --- a/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs +++ b/modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs @@ -38,7 +38,7 @@ namespace Volo.Blogging.Posts public async Task> GetListByBlogIdAndTagName(Guid id, string tagName) { - var posts = _postRepository.GetPostsByBlogId(id); + var posts = await _postRepository.GetPostsByBlogId(id); var tag = tagName.IsNullOrWhiteSpace() ? null : await _tagRepository.FindByNameAsync(id, tagName); var userDictionary = new Dictionary(); var postDtos = new List(ObjectMapper.Map, List>(posts)); diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs index 7122e1da0a..88ff91d516 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs @@ -15,6 +15,6 @@ namespace Volo.Blogging.Comments Task> GetRepliesOfComment(Guid id); - void DeleteOfPost(Guid id); + Task DeleteOfPost(Guid id); } } diff --git a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs index 2344dea06d..d38cd5c0d7 100644 --- a/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs +++ b/modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs @@ -7,7 +7,7 @@ namespace Volo.Blogging.Posts { public interface IPostRepository : IBasicRepository { - List GetPostsByBlogId(Guid id); + Task> GetPostsByBlogId(Guid id); Task GetPostByUrl(Guid blogId, string url); } diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs index 35795ee85f..751d106071 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs @@ -36,7 +36,7 @@ namespace Volo.Blogging.Comments .Where(a => a.RepliedCommentId == id).ToListAsync(); } - public void DeleteOfPost(Guid id) + public async Task DeleteOfPost(Guid id) { var recordsToDelete = DbSet.Where(pt => pt.PostId == id); DbSet.RemoveRange(recordsToDelete); diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs index a48682855c..58a3c83413 100644 --- a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs +++ b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs @@ -18,9 +18,9 @@ namespace Volo.Blogging.Posts } - public List GetPostsByBlogId(Guid id) + public async Task> GetPostsByBlogId(Guid id) { - return DbSet.Where(p => p.BlogId == id).OrderByDescending(p=>p.CreationTime).ToList(); + return await DbSet.Where(p => p.BlogId == id).OrderByDescending(p=>p.CreationTime).ToListAsync(); } public async Task GetPostByUrl(Guid blogId, string url) diff --git a/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/EfCoreQaUserRepository.cs b/modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/EfCoreBlogUserRepository.cs similarity index 100% rename from modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/EfCoreQaUserRepository.cs rename to modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/EfCoreBlogUserRepository.cs diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo.Blogging.MongoDB.csproj b/modules/blogging/src/Volo.Blogging.MongoDB/Volo.Blogging.MongoDB.csproj new file mode 100644 index 0000000000..d8327afc01 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo.Blogging.MongoDB.csproj @@ -0,0 +1,16 @@ + + + + netcoreapp2.1 + Volo.Blogging.MongoDB + Volo.Blogging.MongoDB + + + + + + + + + + diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Blogs/MongoBlogRepository.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Blogs/MongoBlogRepository.cs new file mode 100644 index 0000000000..9319eb64af --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Blogs/MongoBlogRepository.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Blogging.MongoDB; +using System.Linq; +using System.Linq.Dynamic.Core; + +namespace Volo.Blogging.Blogs +{ + public class MongoBlogRepository : MongoDbRepository, IBlogRepository + { + public MongoBlogRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public async Task FindByShortNameAsync(string shortName) + { + return await GetMongoQueryable().FirstOrDefaultAsync(p => p.ShortName == shortName); + } + + public async Task> GetListAsync(string sorting, int maxResultCount, int skipCount) + { + var auditLogs = GetMongoQueryable().OrderBy(sorting ?? "creationTime desc").As>() + .PageBy(skipCount, maxResultCount) + .ToList(); + + return auditLogs; + } + + public async Task GetTotalCount() + { + return await GetMongoQueryable().CountAsync(); + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Comments/MongoCommentRepository.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Comments/MongoCommentRepository.cs new file mode 100644 index 0000000000..f09ddf7026 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Comments/MongoCommentRepository.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Blogging.MongoDB; + +namespace Volo.Blogging.Comments +{ + public class MongoCommentRepository : MongoDbRepository, ICommentRepository + { + public MongoCommentRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public async Task> GetListOfPostAsync(Guid postId) + { + return await GetMongoQueryable() + .Where(a => a.PostId == postId) + .OrderBy(a => a.CreationTime) + .ToListAsync(); + } + + public async Task GetCommentCountOfPostAsync(Guid postId) + { + return await GetMongoQueryable() + .CountAsync(a => a.PostId == postId); + } + + public async Task> GetRepliesOfComment(Guid id) + { + return await GetMongoQueryable() + .Where(a => a.RepliedCommentId == id).ToListAsync(); + } + + public async Task DeleteOfPost(Guid id) + { + var recordsToDelete = GetMongoQueryable().Where(pt => pt.PostId == id); + + foreach (var record in recordsToDelete) + { + await DeleteAsync(record); + } + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/AbpBloggingBsonClassMap.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/AbpBloggingBsonClassMap.cs new file mode 100644 index 0000000000..4ff5d387d3 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/AbpBloggingBsonClassMap.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Text; +using MongoDB.Bson.Serialization; +using Volo.Abp.MongoDB; +using Volo.Abp.Threading; +using Volo.Blogging.Users; + +namespace Volo.Blogging.MongoDB +{ + public static class AbpBloggingBsonClassMap + { + private static readonly OneTimeRunner OneTimeRunner = new OneTimeRunner(); + + public static void Configure() + { + OneTimeRunner.Run(() => + { + BsonClassMap.RegisterClassMap(map => + { + map.AutoMap(); + map.ConfigureExtraProperties(); + }); + }); + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs new file mode 100644 index 0000000000..cf57e77b57 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.Text; +using MongoDB.Driver; +using Volo.Abp.Data; +using Volo.Abp.MongoDB; +using Volo.Blogging.Blogs; +using Volo.Blogging.Comments; +using Volo.Blogging.Posts; +using Volo.Blogging.Users; + +namespace Volo.Blogging.MongoDB +{ + [ConnectionStringName("Blogging")] + public class BloggingMongoDbContext : AbpMongoDbContext, IBloggingMongoDbContext + { + public static string CollectionPrefix { get; set; } = BloggingConsts.DefaultDbTablePrefix; + + public IMongoCollection Users => Collection(); + + public IMongoCollection Blogs => Collection(); + + public IMongoCollection Posts => Collection(); + + public IMongoCollection Tags => Collection(); + + public IMongoCollection PostTags => Collection(); + + public IMongoCollection Comments => Collection(); + + protected override void CreateModel(IMongoModelBuilder modelBuilder) + { + base.CreateModel(modelBuilder); + + modelBuilder.ConfigureBlogging(options => + { + options.CollectionPrefix = CollectionPrefix; + }); + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs new file mode 100644 index 0000000000..8afbdf12f8 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Volo.Abp; +using Volo.Abp.MongoDB; +using Volo.Blogging.Blogs; +using Volo.Blogging.Comments; +using Volo.Blogging.Posts; +using Volo.Blogging.Users; + +namespace Volo.Blogging.MongoDB +{ + public static class BloggingMongoDbContextExtensions + { + public static void ConfigureBlogging( + this IMongoModelBuilder builder, + Action optionsAction = null) + { + Check.NotNull(builder, nameof(builder)); + + var options = new BloggingMongoModelBuilderConfigurationOptions(); + + optionsAction?.Invoke(options); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "Users"; + }); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "Blogs"; + }); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "Posts"; + }); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "Tags"; + }); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "PostTags"; + }); + + builder.Entity(b => + { + b.CollectionName = options.CollectionPrefix + "Comments"; + }); + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs new file mode 100644 index 0000000000..e68d0d9b71 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; +using Volo.Abp.Users.MongoDB; +using Volo.Blogging.Blogs; +using Volo.Blogging.Comments; +using Volo.Blogging.Posts; +using Volo.Blogging.Tagging; +using Volo.Blogging.Users; + +namespace Volo.Blogging.MongoDB +{ + [DependsOn( + typeof(BloggingDomainModule), + typeof(AbpUsersMongoDbModule) + )] + public class BloggingMongoDbModule : AbpModule + { + public override void ConfigureServices(ServiceConfigurationContext context) + { + AbpBloggingBsonClassMap.Configure(); + + context.Services.AddMongoDbContext(options => + { + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); + options.AddRepository(); + }); + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoModelBuilderConfigurationOptions.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoModelBuilderConfigurationOptions.cs new file mode 100644 index 0000000000..c913106c3f --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoModelBuilderConfigurationOptions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; +using JetBrains.Annotations; +using Volo.Abp.MongoDB; + +namespace Volo.Blogging.MongoDB +{ + public class BloggingMongoModelBuilderConfigurationOptions : MongoModelBuilderConfigurationOptions + { + public BloggingMongoModelBuilderConfigurationOptions([NotNull] string tablePrefix = BloggingConsts.DefaultDbTablePrefix) + : base(tablePrefix) + { + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs new file mode 100644 index 0000000000..4e5c782c98 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs @@ -0,0 +1,27 @@ +using MongoDB.Driver; +using Volo.Abp.Data; +using Volo.Abp.MongoDB; +using Volo.Blogging.Blogs; +using Volo.Blogging.Comments; +using Volo.Blogging.Posts; +using Volo.Blogging.Users; + +namespace Volo.Blogging.MongoDB +{ + [ConnectionStringName("Blogging")] + public interface IBloggingMongoDbContext : IAbpMongoDbContext + { + IMongoCollection Users { get; } + + IMongoCollection Blogs { get; } + + IMongoCollection Posts { get; } + + IMongoCollection Tags { get; } + + IMongoCollection PostTags { get; } + + IMongoCollection Comments { get; } + + } +} \ No newline at end of file diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs new file mode 100644 index 0000000000..52a614df90 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Entities; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Blogging.MongoDB; + +namespace Volo.Blogging.Posts +{ + public class MongoPostRepository : MongoDbRepository, IPostRepository + { + public MongoPostRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public async Task> GetPostsByBlogId(Guid id) + { + return await GetMongoQueryable().Where(p => p.BlogId == id).OrderByDescending(p => p.CreationTime).ToListAsync(); + } + + public async Task GetPostByUrl(Guid blogId, string url) + { + var post = await GetMongoQueryable().FirstOrDefaultAsync(p => p.BlogId == blogId && p.Url == url); + + if (post == null) + { + throw new EntityNotFoundException(typeof(Post), nameof(post)); + } + + return post; + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostTagRepository.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostTagRepository.cs new file mode 100644 index 0000000000..edd3ad4bae --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostTagRepository.cs @@ -0,0 +1,30 @@ +using System; +using System.Threading.Tasks; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Blogging.MongoDB; + +namespace Volo.Blogging.Posts +{ + public class MongoPostTagRepository : MongoDbRepository, IPostTagRepository + { + public MongoPostTagRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public void DeleteOfPost(Guid id) + { + var recordsToDelete = GetMongoQueryable().Where(pt => pt.PostId == id); + foreach (var record in recordsToDelete) + { + Delete(record); + } + } + + public async Task FindByTagIdAndPostIdAsync(Guid postId, Guid tagId) + { + return await GetMongoQueryable().FirstOrDefaultAsync(pt => pt.PostId == postId && pt.TagId == tagId); + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Tagging/MongoTagRepository.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Tagging/MongoTagRepository.cs new file mode 100644 index 0000000000..1dedad184b --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Tagging/MongoTagRepository.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using MongoDB.Driver; +using MongoDB.Driver.Linq; +using Volo.Abp.Domain.Repositories.MongoDB; +using Volo.Abp.MongoDB; +using Volo.Blogging.MongoDB; + +namespace Volo.Blogging.Tagging +{ + public class MongoTagRepository : MongoDbRepository, ITagRepository + { + public MongoTagRepository(IMongoDbContextProvider dbContextProvider) + : base(dbContextProvider) + { + } + + public async Task> GetListAsync(Guid blogId) + { + return await GetMongoQueryable().Where(t=>t.BlogId == blogId).ToListAsync(); + } + + public async Task GetByNameAsync(Guid blogId, string name) + { + return await GetMongoQueryable().Where(t => t.BlogId == blogId && t.Name == name).FirstAsync(); + } + + public async Task FindByNameAsync(Guid blogId, string name) + { + return await GetMongoQueryable().Where(t => t.BlogId == blogId && t.Name == name).FirstOrDefaultAsync(); + } + + public async Task> GetListAsync(IEnumerable ids) + { + return await GetMongoQueryable().Where(t => ids.Contains(t.Id)).ToListAsync(); + } + + public void DecreaseUsageCountOfTags(List ids) + { + var tags = GetMongoQueryable().Where(t => ids.Contains(t.Id)); + + foreach (var tag in tags) + { + tag.DecreaseUsageCount(); + } + } + } +} diff --git a/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Users/MongoBlogUserRepository.cs b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Users/MongoBlogUserRepository.cs new file mode 100644 index 0000000000..a0b7655946 --- /dev/null +++ b/modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Users/MongoBlogUserRepository.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Volo.Abp.MongoDB; +using Volo.Abp.Users.MongoDB; +using Volo.Blogging.MongoDB; +using MongoDB.Driver; +using MongoDB.Driver.Linq; + +namespace Volo.Blogging.Users +{ + public class MongoBlogUserRepository : MongoUserRepositoryBase, IBlogUserRepository + { + public MongoBlogUserRepository(IMongoDbContextProvider dbContextProvider) : base(dbContextProvider) + { + } + + public async Task> GetUsersAsync(int maxCount, string filter, CancellationToken cancellationToken) + { + var query = GetMongoQueryable(); + + if (!string.IsNullOrWhiteSpace(filter)) + { + query = query.Where(x => x.UserName.Contains(filter)); + } + + return await query.Take(maxCount).ToListAsync(cancellationToken); + } + } +}