From 0844c51566ed7510216df3fb6051dbb5eb2bb319 Mon Sep 17 00:00:00 2001 From: Alexandre SPIESER Date: Sat, 6 Jun 2020 11:28:46 +0100 Subject: [PATCH] update many implementation, with tests --- .../CoreIntegrationTests.csproj | 5 +- ...ase.cs => MongoDbDocumentTestBase.Main.cs} | 156 +------ .../MongoDbDocumentTestBase.Update.cs | 381 ++++++++++++++++++ ...cs => MongoDbTKeyDocumentTestBase.Main.cs} | 156 +------ .../MongoDbTKeyDocumentTestBase.Update.cs | 377 +++++++++++++++++ .../IBaseMongoRepository_Update.cs | 104 +++++ .../BaseMongoRepository.Update.cs | 240 +++++++++++ .../DataAccess/Update/MongoDbUpdater.cs | 124 ++++++ .../BaseMongoRepository.TKey.Update.cs | 199 +++++++++ MongoDbGenericRepository/Models/IDocument.cs | 3 +- 10 files changed, 1432 insertions(+), 313 deletions(-) rename CoreIntegrationTests/Infrastructure/{MongoDbDocumentTestBase.cs => MongoDbDocumentTestBase.Main.cs} (85%) create mode 100644 CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Update.cs rename CoreIntegrationTests/Infrastructure/{MongoDbTKeyDocumentTestBase.cs => MongoDbTKeyDocumentTestBase.Main.cs} (86%) create mode 100644 CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Update.cs diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index 115d4aa..1fcb1a2 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -8,7 +8,6 @@ - all @@ -18,6 +17,10 @@ + + + + ..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll diff --git a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs similarity index 85% rename from CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs rename to CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs index 2e2a108..98fc846 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Main.cs @@ -12,7 +12,7 @@ using Xunit; namespace CoreIntegrationTests.Infrastructure { - public abstract class MongoDbDocumentTestBase : + public abstract partial class MongoDbDocumentTestBase : IClassFixture> where T : TestDoc, new() { @@ -347,160 +347,6 @@ namespace CoreIntegrationTests.Infrastructure #endregion Read - #region Update - - [Fact] - public void UpdateOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - document.SomeContent = content; - // Act - var result = SUT.UpdateOne(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - document.SomeContent = content; - // Act - var result = await SUT.UpdateOneAsync(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public void UpdateOneField() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = SUT.UpdateOne(document, x => x.SomeContent, content); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneFieldAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = await SUT.UpdateOneAsync(document, x => x.SomeContent, content); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public void UpdateOneFieldWithFilter() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = SUT.UpdateOne(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneFieldWithFilterAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = await SUT.UpdateOneAsync(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneAsyncWithUpdateDefinition() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var childrenToAdd = new List - { - new Child("testType1", "testValue1"), - new Child("testType2", "testValue2") - }; - - var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); - - // Act - var result = await SUT.UpdateOneAsync(document, updateDef); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument); - Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); - Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); - Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); - Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); - } - - [Fact] - public void UpdateOneWithUpdateDefinition() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var childrenToAdd = new List - { - new Child("testType1", "testValue1"), - new Child("testType2", "testValue2") - }; - - var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); - - // Act - var result = SUT.UpdateOne(document, updateDef); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument); - Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); - Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); - Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); - Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); - } - - #endregion Update - #region Delete [Fact] diff --git a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Update.cs b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Update.cs new file mode 100644 index 0000000..465ce1b --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.Update.cs @@ -0,0 +1,381 @@ +using MongoDB.Driver; +using MongoDbGenericRepository; +using MongoDbGenericRepository.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Linq.Expressions; +using System.Runtime.CompilerServices; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace CoreIntegrationTests.Infrastructure +{ + public abstract partial class MongoDbDocumentTestBase : + IClassFixture> + where T : TestDoc, new() + { + #region Update One + + [Fact] + public void UpdateOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = SUT.UpdateOne(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = await SUT.UpdateOneAsync(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneField() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneFieldWithFilter() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldWithFilterAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsyncWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = await SUT.UpdateOneAsync(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + } + + [Fact] + public void UpdateOneWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = SUT.UpdateOne(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + } + + #endregion Update One + + #region Update Many + + [Fact] + public async Task UpdateManyWithLinqFilterAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public async Task UpdateManyWithFilterDefinitionAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(filterDefinition, x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public async Task UpdateManyWithLinqFilterAndUpdateDefinitionAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(x => docIds.Contains(x.Id), updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + [Fact] + public async Task UpdateManyWithFilterAndUpdateDefinitionsAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(filterDefinition, updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + + [Fact] + public void UpdateManyWithLinqFilter() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public void UpdateManyWithFilterDefinition() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(filterDefinition, x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public void UpdateManyWithLinqFilterAndUpdateDefinition() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(x => docIds.Contains(x.Id), updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + [Fact] + public void UpdateManyWithFilterAndUpdateDefinitions() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(filterDefinition, updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + + #endregion Update Many + } +} diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs similarity index 86% rename from CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs rename to CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs index 5414e48..ddd8ff9 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Main.cs @@ -12,7 +12,7 @@ using Xunit; namespace CoreIntegrationTests.Infrastructure { - public abstract class MongoDbTKeyDocumentTestBase : + public abstract partial class MongoDbTKeyDocumentTestBase : IClassFixture> where T : TestDoc, new() where TKey : IEquatable @@ -348,160 +348,6 @@ namespace CoreIntegrationTests.Infrastructure #endregion Read - #region Update - - [Fact] - public void UpdateOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - document.SomeContent = content; - // Act - var result = SUT.UpdateOne(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - document.SomeContent = content; - // Act - var result = await SUT.UpdateOneAsync(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public void UpdateOneField() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = SUT.UpdateOne(document, x => x.SomeContent, content); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneFieldAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = await SUT.UpdateOneAsync(document, x => x.SomeContent, content); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public void UpdateOneFieldWithFilter() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = SUT.UpdateOne(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneFieldWithFilterAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var content = GetContent(); - // Act - var result = await SUT.UpdateOneAsync(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); - // Assert - Assert.True(result, GetTestName()); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument, GetTestName()); - Assert.True(content == updatedDocument.SomeContent, GetTestName()); - } - - [Fact] - public async Task UpdateOneAsyncWithUpdateDefinition() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var childrenToAdd = new List - { - new Child("testType1", "testValue1"), - new Child("testType2", "testValue2") - }; - - var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); - - // Act - var result = await SUT.UpdateOneAsync(document, updateDef); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument); - Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); - Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); - Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); - Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); - } - - [Fact] - public void UpdateOneWithUpdateDefinition() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - var childrenToAdd = new List - { - new Child("testType1", "testValue1"), - new Child("testType2", "testValue2") - }; - - var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); - - // Act - var result = SUT.UpdateOne(document, updateDef); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.True(null != updatedDocument); - Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); - Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); - Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); - Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); - } - - #endregion Update - #region Delete [Fact] diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Update.cs b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Update.cs new file mode 100644 index 0000000..568066a --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.Update.cs @@ -0,0 +1,377 @@ +using MongoDB.Driver; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Xunit; + +namespace CoreIntegrationTests.Infrastructure +{ + public abstract partial class MongoDbTKeyDocumentTestBase : + IClassFixture> + where T : TestDoc, new() + where TKey : IEquatable + + { + #region Update One + + [Fact] + public void UpdateOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = SUT.UpdateOne(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = await SUT.UpdateOneAsync(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneField() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneFieldWithFilter() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldWithFilterAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsyncWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = await SUT.UpdateOneAsync(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + } + + [Fact] + public void UpdateOneWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = SUT.UpdateOne(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + } + + #endregion Update One + + #region Update Many + + [Fact] + public async Task UpdateManyWithLinqFilterAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public async Task UpdateManyWithFilterDefinitionAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(filterDefinition, x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public async Task UpdateManyWithLinqFilterAndUpdateDefinitionAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(x => docIds.Contains(x.Id), updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + [Fact] + public async Task UpdateManyWithFilterAndUpdateDefinitionsAsync() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = await SUT.UpdateManyAsync(filterDefinition, updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + + [Fact] + public void UpdateManyWithLinqFilter() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(x => docIds.Contains(x.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public void UpdateManyWithFilterDefinition() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(filterDefinition, x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocument = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocument.Count == 2); + Assert.True(updatedDocument.All(u => u.SomeContent == content), GetTestName()); + } + + [Fact] + public void UpdateManyWithLinqFilterAndUpdateDefinition() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var docIds = documents.Select(u => u.Id).ToArray(); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(x => docIds.Contains(x.Id), updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + [Fact] + public void UpdateManyWithFilterAndUpdateDefinitions() + { + // Arrange + var documents = CreateTestDocuments(2); + SUT.AddMany(documents); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + var docIds = documents.Select(u => u.Id).ToArray(); + var filterDefinition = Builders.Filter.Where(x => docIds.Contains(x.Id)); + var content = GetContent(); + // Act + var result = SUT.UpdateMany(filterDefinition, updateDef, PartitionKey); + // Assert + Assert.True(result == 2, GetTestName()); + var updatedDocuments = SUT.GetAll(x => docIds.Contains(x.Id), PartitionKey); + + Assert.True(updatedDocuments.Count == 2); + updatedDocuments.ForEach(updatedDocument => + { + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + }); + } + + + #endregion Update Many + } +} diff --git a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs index 878fdb7..d30bc5c 100644 --- a/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs +++ b/MongoDbGenericRepository/Abstractions/IBaseMongoRepository_Update.cs @@ -131,5 +131,109 @@ namespace MongoDbGenericRepository bool UpdateOne(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable; } } diff --git a/MongoDbGenericRepository/BaseMongoRepository.Update.cs b/MongoDbGenericRepository/BaseMongoRepository.Update.cs index 61696a7..82306b1 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Update.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Update.cs @@ -168,6 +168,119 @@ namespace MongoDbGenericRepository return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + } + #endregion Update #region Update TKey @@ -326,6 +439,133 @@ namespace MongoDbGenericRepository return await MongoDbUpdater.UpdateOneAsync(Builders.Filter.Where(filter), field, value, partitionKey); } + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + } #endregion Update } diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index c14d83c..7f7c230 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -183,5 +183,129 @@ namespace MongoDbGenericRepository.DataAccess.Update { return UpdateOne(Builders.Filter.Where(filter), field, value, partitionKey); } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(Builders.Filter.Where(filter), field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = await collection.UpdateManyAsync(filter, Builders.Update.Set(field, value)); + return updateRes.ModifiedCount; + } + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition update, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return await UpdateManyAsync(Builders.Filter.Where(filter), update, partitionKey); + } + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The update definition. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = await collection.UpdateManyAsync(filter, updateDefinition, new UpdateOptions { IsUpsert = true }); + return updateRes.ModifiedCount; + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + return UpdateMany(Builders.Filter.Where(filter), field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = collection.UpdateMany(filter, Builders.Update.Set(field, value)); + return updateRes.ModifiedCount; + } + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The type of the field. + /// The document filter. + /// The update definition. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition UpdateDefinition, string partitionKey = null) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = collection.UpdateMany(filter, UpdateDefinition, new UpdateOptions { IsUpsert = true }); + return updateRes.ModifiedCount; + } } } diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs index 98c4e08..b56d9c4 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Update.cs @@ -110,6 +110,94 @@ namespace MongoDbGenericRepository /// The partition key for the document. Task UpdateOneAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument; + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument; } public abstract partial class BaseMongoRepository : IBaseMongoRepository_Update @@ -266,5 +354,116 @@ namespace MongoDbGenericRepository { return await MongoDbUpdater.UpdateOneAsync(filter, field, value, partitionKey); } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + public virtual async Task UpdateManyAsync(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual async Task UpdateManyAsync(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + { + return await MongoDbUpdater.UpdateManyAsync(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The partition key for the document. + public virtual long UpdateMany(Expression> filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, updates the property field with the given value. + /// + /// The type representing a Document. + /// The type of the field. + /// The document filter. + /// The field selector. + /// The new value of the property field. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, Expression> field, TField value, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, field, value, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual long UpdateMany(FilterDefinition filter, UpdateDefinition updateDefinition, string partitionKey = null) + where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + } + + /// + /// For the entities selected by the filter, applies the update you have defined in MongoDb. + /// + /// The type representing a Document. + /// The document filter. + /// The update definition to apply. + /// The value of the partition key. + public virtual long UpdateMany(Expression> filter, UpdateDefinition updateDefinition, string partitionKey = null) where TDocument : IDocument + { + return MongoDbUpdater.UpdateMany(filter, updateDefinition, partitionKey); + } } } diff --git a/MongoDbGenericRepository/Models/IDocument.cs b/MongoDbGenericRepository/Models/IDocument.cs index d034223..da6ec36 100644 --- a/MongoDbGenericRepository/Models/IDocument.cs +++ b/MongoDbGenericRepository/Models/IDocument.cs @@ -1,5 +1,4 @@ -using MongoDB.Bson.Serialization.Attributes; -using System; +using System; namespace MongoDbGenericRepository.Models {