From 245113a4f26302b0b3a80caeac6655e5656ddb01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCsters?= Date: Wed, 19 Jul 2023 07:41:14 +0200 Subject: [PATCH 1/2] Removed unnecessary ToString() calls --- .../RepositoryTestBase.DeleteAsync.cs | 20 +++++++++++++++++++ .../DatabaseRepository`1.Delete.cs | 2 +- .../Repositories/DatabaseRepository`1.Get.cs | 2 +- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/core/Wemogy.Infrastructure.Database.Core.UnitTests/Repositories/RepositoryTestBase.DeleteAsync.cs b/src/core/Wemogy.Infrastructure.Database.Core.UnitTests/Repositories/RepositoryTestBase.DeleteAsync.cs index 98bf477..ee9a78a 100644 --- a/src/core/Wemogy.Infrastructure.Database.Core.UnitTests/Repositories/RepositoryTestBase.DeleteAsync.cs +++ b/src/core/Wemogy.Infrastructure.Database.Core.UnitTests/Repositories/RepositoryTestBase.DeleteAsync.cs @@ -32,6 +32,26 @@ await MicrosoftUserRepository.DeleteAsync( userExistsAfterDeletion.Should().BeFalse(); } + [Fact] + public async Task DeleteAsyncWithIdOnlyShouldWork() + { + // Arrange + var user = User.Faker.Generate(); + await MicrosoftUserRepository.CreateAsync(user); + + // Act + var userExistsBeforeDeletion = await MicrosoftUserRepository.ExistsAsync( + user.Id); + await MicrosoftUserRepository.DeleteAsync( + user.Id); + var userExistsAfterDeletion = await MicrosoftUserRepository.ExistsAsync( + user.Id); + + // Assert + userExistsBeforeDeletion.Should().BeTrue(); + userExistsAfterDeletion.Should().BeFalse(); + } + [Fact] public async Task DeleteAsyncShouldThrowForNonExistingEntities() { diff --git a/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Delete.cs b/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Delete.cs index 1a2a4d8..4f3aafc 100644 --- a/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Delete.cs +++ b/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Delete.cs @@ -10,7 +10,7 @@ public partial class DatabaseRepository { public Task DeleteAsync(string id) { - return _database.DeleteAsync(x => id == x.Id.ToString()); + return _database.DeleteAsync(x => id == x.Id); } public Task DeleteAsync(string id, string partitionKey) diff --git a/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Get.cs b/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Get.cs index bdb1f29..7c2f080 100644 --- a/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Get.cs +++ b/src/core/Wemogy.Infrastructure.Database.Core/Repositories/DatabaseRepository`1.Get.cs @@ -52,7 +52,7 @@ public async Task GetAsync(string id, CancellationToken cancellationTok try { return await GetAsync( - x => x.Id.ToString() == id, + x => x.Id == id, cancellationToken); } catch (NotFoundErrorException) From 50a5e1c5e0fcfdacd7b83d8880f2ea7a5719b0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20K=C3=BCsters?= Date: Wed, 19 Jul 2023 07:41:27 +0200 Subject: [PATCH 2/2] Fixed MongoDb logger --- .../Factories/MongoDatabaseClientFactory.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/mongo/Wemogy.Infrastructure.Database.Mongo/Factories/MongoDatabaseClientFactory.cs b/src/mongo/Wemogy.Infrastructure.Database.Mongo/Factories/MongoDatabaseClientFactory.cs index ea3dedc..df7327a 100644 --- a/src/mongo/Wemogy.Infrastructure.Database.Mongo/Factories/MongoDatabaseClientFactory.cs +++ b/src/mongo/Wemogy.Infrastructure.Database.Mongo/Factories/MongoDatabaseClientFactory.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.Logging; +using MongoDB.Bson; using MongoDB.Bson.Serialization.Conventions; using MongoDB.Driver; +using MongoDB.Driver.Core.Events; using Wemogy.Infrastructure.Database.Core.Abstractions; using Wemogy.Infrastructure.Database.Core.Models; using Wemogy.Infrastructure.Database.Mongo.Client; @@ -24,7 +26,7 @@ public MongoDatabaseClientFactory( var pack = new ConventionPack { new CamelCaseElementNameConvention() }; ConventionRegistry.Register(nameof(CamelCaseElementNameConvention), pack, x => true); - _mongoClient = new MongoClient(connectionString); + var settings = MongoClientSettings.FromConnectionString(connectionString); if (enableLogging) { @@ -33,7 +35,20 @@ public MongoDatabaseClientFactory( builder.AddConsole(); }); _logger = loggerFactory.CreateLogger(nameof(MongoDatabaseClientFactory)); + + settings.ClusterConfigurator = cb => + { + cb.Subscribe(e => + { + _logger.LogInformation( + "{CommandName} - {Json}", + e.CommandName, + e.Command.ToJson()); + }); + }; } + + _mongoClient = new MongoClient(settings); } public IDatabaseClient CreateClient(DatabaseRepositoryOptions databaseRepositoryOptions)