Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Backend.Tests/Controllers/MergeControllerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace Backend.Tests.Controllers
{
public class MergeControllerTests : IDisposable
{
private IMongoDbContext _mongoDbContext = null!;
private IMergeBlacklistRepository _mergeBlacklistRepo = null!;
private IMergeGraylistRepository _mergeGraylistRepo = null!;
private IWordRepository _wordRepo = null!;
Expand Down Expand Up @@ -38,11 +39,13 @@ protected virtual void Dispose(bool disposing)
[SetUp]
public void Setup()
{
_mongoDbContext = new MongoDbContextMock();
_mergeBlacklistRepo = new MergeBlacklistRepositoryMock();
_mergeGraylistRepo = new MergeGraylistRepositoryMock();
_wordRepo = new WordRepositoryMock();
_wordService = new WordService(_wordRepo);
_mergeService = new MergeService(_mergeBlacklistRepo, _mergeGraylistRepo, _wordRepo, _wordService);
_mergeService = new MergeService(
_mongoDbContext, _mergeBlacklistRepo, _mergeGraylistRepo, _wordRepo, _wordService);
_permissionService = new PermissionServiceMock();
_mergeController = new MergeController(_mergeService, _permissionService);
}
Expand Down
32 changes: 32 additions & 0 deletions Backend.Tests/Mocks/MongoDbContextMock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using MongoDB.Driver;

namespace Backend.Tests.Mocks;

public class MongoDbContextMock : IMongoDbContext
{
public IMongoDatabase Db => throw new NotSupportedException();
public Task<IMongoTransaction> BeginTransaction()
{
return Task.FromResult<IMongoTransaction>(new MongoTransactionMock());
}

private sealed class MongoTransactionMock : IMongoTransaction
{
public Task CommitTransactionAsync()
{
return Task.CompletedTask;
}

public Task AbortTransactionAsync()
{
return Task.CompletedTask;
}

public void Dispose()
{
}
}
}
5 changes: 4 additions & 1 deletion Backend.Tests/Services/MergeServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Backend.Tests.Services
{
public class MergeServiceTests
{
private IMongoDbContext _mongoDbContext = null!;
private IMergeBlacklistRepository _mergeBlacklistRepo = null!;
private IMergeGraylistRepository _mergeGraylistRepo = null!;
private IWordRepository _wordRepo = null!;
Expand All @@ -22,11 +23,13 @@ public class MergeServiceTests
[SetUp]
public void Setup()
{
_mongoDbContext = new MongoDbContextMock();
_mergeBlacklistRepo = new MergeBlacklistRepositoryMock();
_mergeGraylistRepo = new MergeGraylistRepositoryMock();
_wordRepo = new WordRepositoryMock();
_wordService = new WordService(_wordRepo);
_mergeService = new MergeService(_mergeBlacklistRepo, _mergeGraylistRepo, _wordRepo, _wordService);
_mergeService = new MergeService(
_mongoDbContext, _mergeBlacklistRepo, _mergeGraylistRepo, _wordRepo, _wordService);
}

[Test]
Expand Down
10 changes: 4 additions & 6 deletions Backend/Contexts/BannerContext.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class BannerContext : IBannerContext
{
private readonly IMongoDatabase _db;
private readonly IMongoDbContext _mongoDbContext;

public BannerContext(IOptions<Startup.Settings> options)
public BannerContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<Banner> Banners => _db.GetCollection<Banner>("BannerCollection");
public IMongoCollection<Banner> Banners => _mongoDbContext.Db.GetCollection<Banner>("BannerCollection");
}
}
11 changes: 4 additions & 7 deletions Backend/Contexts/MergeBlacklistContext.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class MergeBlacklistContext : IMergeBlacklistContext
{
private readonly IMongoDatabase _db;

public MergeBlacklistContext(IOptions<Startup.Settings> options)
private readonly IMongoDbContext _mongoDbContext;
public MergeBlacklistContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<MergeWordSet> MergeBlacklist => _db.GetCollection<MergeWordSet>(
public IMongoCollection<MergeWordSet> MergeBlacklist => _mongoDbContext.Db.GetCollection<MergeWordSet>(
"MergeBlacklistCollection");
}
}
11 changes: 4 additions & 7 deletions Backend/Contexts/MergeGraylistContext.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class MergeGraylistContext : IMergeGraylistContext
{
private readonly IMongoDatabase _db;

public MergeGraylistContext(IOptions<Startup.Settings> options)
private readonly IMongoDbContext _mongoDbContext;
public MergeGraylistContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<MergeWordSet> MergeGraylist => _db.GetCollection<MergeWordSet>(
public IMongoCollection<MergeWordSet> MergeGraylist => _mongoDbContext.Db.GetCollection<MergeWordSet>(
"MergeGraylistCollection");
}
}
49 changes: 49 additions & 0 deletions Backend/Contexts/MongoDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Threading.Tasks;
using BackendFramework.Interfaces;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts;

public class MongoDbContext : IMongoDbContext
{
public IMongoDatabase Db { get; }

public MongoDbContext(IOptions<Startup.Settings> options)
{
var client = new MongoClient(options.Value.ConnectionString);
Db = client.GetDatabase(options.Value.CombineDatabase);
}

public async Task<IMongoTransaction> BeginTransaction()
{
var session = await Db.Client.StartSessionAsync();
session.StartTransaction();
return new MongoTransactionWrapper(session);
}

private class MongoTransactionWrapper : IMongoTransaction
{
private readonly IClientSessionHandle _session;

public MongoTransactionWrapper(IClientSessionHandle session)
{
_session = session;
}

public Task CommitTransactionAsync()
{
return _session.CommitTransactionAsync();
}

public Task AbortTransactionAsync()
{
return _session.AbortTransactionAsync();
}

public void Dispose()
{
_session.Dispose();
}
}
}
9 changes: 4 additions & 5 deletions Backend/Contexts/PasswordResetContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ namespace BackendFramework.Contexts
[ExcludeFromCodeCoverage]
public class PasswordResetContext : IPasswordResetContext
{
private readonly IMongoDatabase _db;
private readonly IMongoDbContext _mongoDbContext;
public int ExpireTime { get; }

public PasswordResetContext(IOptions<Startup.Settings> options)
public PasswordResetContext(IOptions<Startup.Settings> options, IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
ExpireTime = options.Value.PassResetExpireTime;
}

private IMongoCollection<PasswordReset> PasswordResets => _db.GetCollection<PasswordReset>(
private IMongoCollection<PasswordReset> PasswordResets => _mongoDbContext.Db.GetCollection<PasswordReset>(
"PasswordResetCollection");

public Task ClearAll(string email)
Expand Down
11 changes: 4 additions & 7 deletions Backend/Contexts/ProjectContext.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class ProjectContext : IProjectContext
{
private readonly IMongoDatabase _db;

public ProjectContext(IOptions<Startup.Settings> options)
private readonly IMongoDbContext _mongoDbContext;
public ProjectContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<Project> Projects => _db.GetCollection<Project>("ProjectsCollection");
public IMongoCollection<Project> Projects => _mongoDbContext.Db.GetCollection<Project>("ProjectsCollection");
}
}
13 changes: 5 additions & 8 deletions Backend/Contexts/SemanticDomainContext.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class SemanticDomainContext : ISemanticDomainContext
{
private readonly IMongoDatabase _db;

public SemanticDomainContext(IOptions<Startup.Settings> options)
private readonly IMongoDbContext _mongoDbContext;
public SemanticDomainContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<SemanticDomainTreeNode> SemanticDomains => _db.GetCollection<SemanticDomainTreeNode>("SemanticDomainTree");
public IMongoCollection<SemanticDomainFull> FullSemanticDomains => _db.GetCollection<SemanticDomainFull>("SemanticDomains");
public IMongoCollection<SemanticDomainTreeNode> SemanticDomains => _mongoDbContext.Db.GetCollection<SemanticDomainTreeNode>("SemanticDomainTree");
public IMongoCollection<SemanticDomainFull> FullSemanticDomains => _mongoDbContext.Db.GetCollection<SemanticDomainFull>("SemanticDomains");
}
}
10 changes: 4 additions & 6 deletions Backend/Contexts/SpeakerContext.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class SpeakerContext : ISpeakerContext
{
private readonly IMongoDatabase _db;
private readonly IMongoDbContext _mongoDbContext;

public SpeakerContext(IOptions<Startup.Settings> options)
public SpeakerContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<Speaker> Speakers => _db.GetCollection<Speaker>("SpeakersCollection");
public IMongoCollection<Speaker> Speakers => _mongoDbContext.Db.GetCollection<Speaker>("SpeakersCollection");
}
}
11 changes: 4 additions & 7 deletions Backend/Contexts/UserContext.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class UserContext : IUserContext
{
private readonly IMongoDatabase _db;

public UserContext(IOptions<Startup.Settings> options)
private readonly IMongoDbContext _mongoDbContext;
public UserContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<User> Users => _db.GetCollection<User>("UsersCollection");
public IMongoCollection<User> Users => _mongoDbContext.Db.GetCollection<User>("UsersCollection");
}
}
11 changes: 4 additions & 7 deletions Backend/Contexts/UserEditContext.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class UserEditContext : IUserEditContext
{
private readonly IMongoDatabase _db;

public UserEditContext(IOptions<Startup.Settings> options)
private readonly IMongoDbContext _mongoDbContext;
public UserEditContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<UserEdit> UserEdits => _db.GetCollection<UserEdit>("UserEditsCollection");
public IMongoCollection<UserEdit> UserEdits => _mongoDbContext.Db.GetCollection<UserEdit>("UserEditsCollection");
}
}
11 changes: 4 additions & 7 deletions Backend/Contexts/UserRoleContext.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
using System.Diagnostics.CodeAnalysis;
using BackendFramework.Interfaces;
using BackendFramework.Models;
using Microsoft.Extensions.Options;
using MongoDB.Driver;

namespace BackendFramework.Contexts
{
[ExcludeFromCodeCoverage]
public class UserRoleContext : IUserRoleContext
{
private readonly IMongoDatabase _db;

public UserRoleContext(IOptions<Startup.Settings> options)
private readonly IMongoDbContext _mongoDbContext;
public UserRoleContext(IMongoDbContext mongoDbContext)
{
var client = new MongoClient(options.Value.ConnectionString);
_db = client.GetDatabase(options.Value.CombineDatabase);
_mongoDbContext = mongoDbContext;
}

public IMongoCollection<UserRole> UserRoles => _db.GetCollection<UserRole>("UserRolesCollection");
public IMongoCollection<UserRole> UserRoles => _mongoDbContext.Db.GetCollection<UserRole>("UserRolesCollection");
}
}
Loading