Browse Source

[Blog module] MongoDB integration #581

pull/594/head
Yunus Emre Kalkan 8 years ago
parent
commit
18472daf14
  1. 20
      modules/blogging/Volo.Blogging.sln
  2. 15
      modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo.BloggingTestApp.MongoDb.csproj
  3. 21
      modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo/BloggingTestApp/MongoDb/BloggingTestAppMongoDbModule.cs
  4. 16
      modules/blogging/app/Volo.BloggingTestApp/BloggingTestAppModule.cs
  5. 1
      modules/blogging/app/Volo.BloggingTestApp/Volo.BloggingTestApp.csproj
  6. 2
      modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs
  7. 2
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs
  8. 2
      modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs
  9. 2
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Comments/EfCoreCommentRepository.cs
  10. 4
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs
  11. 0
      modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/EfCoreBlogUserRepository.cs
  12. 16
      modules/blogging/src/Volo.Blogging.MongoDB/Volo.Blogging.MongoDB.csproj
  13. 38
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Blogs/MongoBlogRepository.cs
  14. 49
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Comments/MongoCommentRepository.cs
  15. 27
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/AbpBloggingBsonClassMap.cs
  16. 41
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContext.cs
  17. 56
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbContextExtensions.cs
  18. 33
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoDbModule.cs
  19. 16
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/BloggingMongoModelBuilderConfigurationOptions.cs
  20. 27
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/MongoDB/IBloggingMongoDbContext.cs
  21. 36
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostRepository.cs
  22. 30
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Posts/MongoPostTagRepository.cs
  23. 50
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Tagging/MongoTagRepository.cs
  24. 30
      modules/blogging/src/Volo.Blogging.MongoDB/Volo/Blogging/Users/MongoBlogUserRepository.cs

20
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}

15
modules/blogging/app/Volo.BloggingTestApp.MongoDb/Volo.BloggingTestApp.MongoDb.csproj

@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Volo.Blogging.MongoDB\Volo.Blogging.MongoDB.csproj" />
<ProjectReference Include="..\..\..\..\modules\permission-management\src\Volo.Abp.PermissionManagement.MongoDB\Volo.Abp.PermissionManagement.MongoDB.csproj" />
<ProjectReference Include="..\..\..\..\modules\setting-management\src\Volo.Abp.SettingManagement.MongoDB\Volo.Abp.SettingManagement.MongoDB.csproj" />
<ProjectReference Include="..\..\..\..\modules\identity\src\Volo.Abp.Identity.MongoDB\Volo.Abp.Identity.MongoDB.csproj" />
</ItemGroup>
</Project>

21
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
{
}
}

16
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<DbConnectionOptions>(options =>
{
#if MONGODB
const string connStringName = "MongoDb";
#else
const string connStringName = "SqlServer";
#endif
options.ConnectionStrings.Default = configuration.GetConnectionString(connStringName);
});
#if !MONGODB
context.Services.Configure<AbpDbContextOptions>(options =>
{
options.UseSqlServer();
});
#endif
if (hostingEnvironment.IsDevelopment())
{
context.Services.Configure<VirtualFileSystemOptions>(options =>

1
modules/blogging/app/Volo.BloggingTestApp/Volo.BloggingTestApp.csproj

@ -24,6 +24,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.BloggingTestApp.EntityFrameworkCore\Volo.BloggingTestApp.EntityFrameworkCore.csproj" />
<ProjectReference Include="..\Volo.BloggingTestApp.MongoDb\Volo.BloggingTestApp.MongoDb.csproj" />
<ProjectReference Include="..\..\src\Volo.Blogging.Application\Volo.Blogging.Application.csproj" />
<ProjectReference Include="..\..\src\Volo.Blogging.Web\Volo.Blogging.Web.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" />

2
modules/blogging/src/Volo.Blogging.Application/Volo/Blogging/Posts/PostAppService.cs

@ -38,7 +38,7 @@ namespace Volo.Blogging.Posts
public async Task<ListResultDto<PostWithDetailsDto>> 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<Guid, BlogUserDto>();
var postDtos = new List<PostWithDetailsDto>(ObjectMapper.Map<List<Post>, List<PostWithDetailsDto>>(posts));

2
modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Comments/ICommentRepository.cs

@ -15,6 +15,6 @@ namespace Volo.Blogging.Comments
Task<List<Comment>> GetRepliesOfComment(Guid id);
void DeleteOfPost(Guid id);
Task DeleteOfPost(Guid id);
}
}

2
modules/blogging/src/Volo.Blogging.Domain/Volo/Blogging/Posts/IPostRepository.cs

@ -7,7 +7,7 @@ namespace Volo.Blogging.Posts
{
public interface IPostRepository : IBasicRepository<Post, Guid>
{
List<Post> GetPostsByBlogId(Guid id);
Task<List<Post>> GetPostsByBlogId(Guid id);
Task<Post> GetPostByUrl(Guid blogId, string url);
}

2
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);

4
modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Posts/EfCorePostRepository.cs

@ -18,9 +18,9 @@ namespace Volo.Blogging.Posts
}
public List<Post> GetPostsByBlogId(Guid id)
public async Task<List<Post>> 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<Post> GetPostByUrl(Guid blogId, string url)

0
modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/EfCoreQaUserRepository.cs → modules/blogging/src/Volo.Blogging.EntityFrameworkCore/Volo/Blogging/Users/EfCoreBlogUserRepository.cs

16
modules/blogging/src/Volo.Blogging.MongoDB/Volo.Blogging.MongoDB.csproj

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<AssemblyName>Volo.Blogging.MongoDB</AssemblyName>
<PackageId>Volo.Blogging.MongoDB</PackageId>
<RootNamespace />
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Volo.Blogging.Domain\Volo.Blogging.Domain.csproj" />
<ProjectReference Include="..\..\..\..\framework\src\Volo.Abp.MongoDB\Volo.Abp.MongoDB.csproj" />
<ProjectReference Include="..\..\..\users\src\Volo.Abp.Users.MongoDB\Volo.Abp.Users.MongoDB.csproj" />
</ItemGroup>
</Project>

38
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<IBloggingMongoDbContext, Blog, Guid>, IBlogRepository
{
public MongoBlogRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbContextProvider) : base(dbContextProvider)
{
}
public async Task<Blog> FindByShortNameAsync(string shortName)
{
return await GetMongoQueryable().FirstOrDefaultAsync(p => p.ShortName == shortName);
}
public async Task<List<Blog>> GetListAsync(string sorting, int maxResultCount, int skipCount)
{
var auditLogs = GetMongoQueryable().OrderBy(sorting ?? "creationTime desc").As<IMongoQueryable<Blog>>()
.PageBy(skipCount, maxResultCount)
.ToList();
return auditLogs;
}
public async Task<int> GetTotalCount()
{
return await GetMongoQueryable().CountAsync();
}
}
}

49
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<IBloggingMongoDbContext, Comment, Guid>, ICommentRepository
{
public MongoCommentRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbContextProvider) : base(dbContextProvider)
{
}
public async Task<List<Comment>> GetListOfPostAsync(Guid postId)
{
return await GetMongoQueryable()
.Where(a => a.PostId == postId)
.OrderBy(a => a.CreationTime)
.ToListAsync();
}
public async Task<int> GetCommentCountOfPostAsync(Guid postId)
{
return await GetMongoQueryable()
.CountAsync(a => a.PostId == postId);
}
public async Task<List<Comment>> 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);
}
}
}
}

27
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<BlogUser>(map =>
{
map.AutoMap();
map.ConfigureExtraProperties();
});
});
}
}
}

41
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<BlogUser> Users => Collection<BlogUser>();
public IMongoCollection<Blog> Blogs => Collection<Blog>();
public IMongoCollection<Post> Posts => Collection<Post>();
public IMongoCollection<Tagging.Tag> Tags => Collection<Tagging.Tag>();
public IMongoCollection<PostTag> PostTags => Collection<PostTag>();
public IMongoCollection<Comment> Comments => Collection<Comment>();
protected override void CreateModel(IMongoModelBuilder modelBuilder)
{
base.CreateModel(modelBuilder);
modelBuilder.ConfigureBlogging(options =>
{
options.CollectionPrefix = CollectionPrefix;
});
}
}
}

56
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<MongoModelBuilderConfigurationOptions> optionsAction = null)
{
Check.NotNull(builder, nameof(builder));
var options = new BloggingMongoModelBuilderConfigurationOptions();
optionsAction?.Invoke(options);
builder.Entity<BlogUser>(b =>
{
b.CollectionName = options.CollectionPrefix + "Users";
});
builder.Entity<Blog>(b =>
{
b.CollectionName = options.CollectionPrefix + "Blogs";
});
builder.Entity<Post>(b =>
{
b.CollectionName = options.CollectionPrefix + "Posts";
});
builder.Entity<Tagging.Tag>(b =>
{
b.CollectionName = options.CollectionPrefix + "Tags";
});
builder.Entity<PostTag>(b =>
{
b.CollectionName = options.CollectionPrefix + "PostTags";
});
builder.Entity<Comment>(b =>
{
b.CollectionName = options.CollectionPrefix + "Comments";
});
}
}
}

33
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<BloggingMongoDbContext>(options =>
{
options.AddRepository<Blog, MongoBlogRepository>();
options.AddRepository<BlogUser, MongoBlogUserRepository>();
options.AddRepository<Post, MongoPostRepository>();
options.AddRepository<Tag, MongoTagRepository>();
options.AddRepository<PostTag, MongoPostTagRepository>();
options.AddRepository<Comment, MongoCommentRepository>();
});
}
}
}

16
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)
{
}
}
}

27
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<BlogUser> Users { get; }
IMongoCollection<Blog> Blogs { get; }
IMongoCollection<Post> Posts { get; }
IMongoCollection<Tagging.Tag> Tags { get; }
IMongoCollection<PostTag> PostTags { get; }
IMongoCollection<Comment> Comments { get; }
}
}

36
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<IBloggingMongoDbContext, Post, Guid>, IPostRepository
{
public MongoPostRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbContextProvider) : base(dbContextProvider)
{
}
public async Task<List<Post>> GetPostsByBlogId(Guid id)
{
return await GetMongoQueryable().Where(p => p.BlogId == id).OrderByDescending(p => p.CreationTime).ToListAsync();
}
public async Task<Post> 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;
}
}
}

30
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<IBloggingMongoDbContext, PostTag>, IPostTagRepository
{
public MongoPostTagRepository(IMongoDbContextProvider<IBloggingMongoDbContext> 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<PostTag> FindByTagIdAndPostIdAsync(Guid postId, Guid tagId)
{
return await GetMongoQueryable().FirstOrDefaultAsync(pt => pt.PostId == postId && pt.TagId == tagId);
}
}
}

50
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<IBloggingMongoDbContext, Tag, Guid>, ITagRepository
{
public MongoTagRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
public async Task<List<Tag>> GetListAsync(Guid blogId)
{
return await GetMongoQueryable().Where(t=>t.BlogId == blogId).ToListAsync();
}
public async Task<Tag> GetByNameAsync(Guid blogId, string name)
{
return await GetMongoQueryable().Where(t => t.BlogId == blogId && t.Name == name).FirstAsync();
}
public async Task<Tag> FindByNameAsync(Guid blogId, string name)
{
return await GetMongoQueryable().Where(t => t.BlogId == blogId && t.Name == name).FirstOrDefaultAsync();
}
public async Task<List<Tag>> GetListAsync(IEnumerable<Guid> ids)
{
return await GetMongoQueryable().Where(t => ids.Contains(t.Id)).ToListAsync();
}
public void DecreaseUsageCountOfTags(List<Guid> ids)
{
var tags = GetMongoQueryable().Where(t => ids.Contains(t.Id));
foreach (var tag in tags)
{
tag.DecreaseUsageCount();
}
}
}
}

30
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<IBloggingMongoDbContext, BlogUser>, IBlogUserRepository
{
public MongoBlogUserRepository(IMongoDbContextProvider<IBloggingMongoDbContext> dbContextProvider) : base(dbContextProvider)
{
}
public async Task<List<BlogUser>> 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);
}
}
}
Loading…
Cancel
Save