added some missing cancellation tokens and fixed incorrect tests

This commit is contained in:
Sean Garrett
2023-06-16 22:40:46 +01:00
parent f09cfc53c2
commit 23780697fb
25 changed files with 1077 additions and 175 deletions
@@ -19,8 +19,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="document">The document you want to delete.</param>
/// <param name="cancellationToken">An optional cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteOne<TDocument, TKey>(TDocument document)
long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -31,8 +32,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -1,12 +1,12 @@
using MongoDB.Driver;
using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models;
namespace MongoDbGenericRepository.DataAccess.Delete
{
@@ -14,7 +14,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
public class MongoDbEraser : DataAccessBase, IMongoDbEraser
{
/// <summary>
/// The MongoDbEraser constructor.
/// The MongoDbEraser constructor.
/// </summary>
/// <param name="mongoDbContext">the MongoDb Context</param>
public MongoDbEraser(IMongoDbContext mongoDbContext) : base(mongoDbContext)
@@ -24,12 +24,20 @@ namespace MongoDbGenericRepository.DataAccess.Delete
#region Delete TKey
/// <inheritdoc />
public virtual long DeleteOne<TDocument, TKey>(TDocument document)
public virtual long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", document.Id);
return HandlePartitioned<TDocument, TKey>(document).DeleteOne(filter).DeletedCount;
return HandlePartitioned<TDocument, TKey>(document).DeleteOne(filter, cancellationToken).DeletedCount;
}
/// <inheritdoc />
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOne(filter, cancellationToken).DeletedCount;
}
/// <inheritdoc />
@@ -42,15 +50,10 @@ namespace MongoDbGenericRepository.DataAccess.Delete
}
/// <inheritdoc />
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteOne(filter).DeletedCount;
}
/// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
string partitionKey,
CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -58,7 +61,10 @@ namespace MongoDbGenericRepository.DataAccess.Delete
}
/// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
string partitionKey,
CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -71,7 +77,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
where TKey : IEquatable<TKey>
{
var documentList = documents.ToList();
if (!documentList.Any())
{
return 0;
@@ -81,22 +87,24 @@ namespace MongoDbGenericRepository.DataAccess.Delete
if (documentList.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{
var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += (await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault())
.DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken: cancellationToken))
.DeleteManyAsync(x => groupIdsToDelete.Contains(x.Id), cancellationToken))
.DeletedCount;
}
return deleteCount;
}
var idsToDelete = documentList.Select(e => e.Id).ToArray();
return (await HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken: cancellationToken)).DeletedCount;
return (await HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteManyAsync(x => idsToDelete.Contains(x.Id), cancellationToken))
.DeletedCount;
}
/// <summary>
/// Deletes a list of documents.
/// Deletes a list of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
@@ -107,7 +115,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
where TKey : IEquatable<TKey>
{
var documentList = documents.ToList();
if (!documentList.Any())
{
return 0;
@@ -117,11 +125,12 @@ namespace MongoDbGenericRepository.DataAccess.Delete
if (documentList.Any(e => e is IPartitionedDocument))
{
long deleteCount = 0;
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{
var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += (HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id))).DeletedCount;
deleteCount += HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id)).DeletedCount;
}
return deleteCount;
}
@@ -130,7 +139,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
}
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
@@ -145,6 +154,5 @@ namespace MongoDbGenericRepository.DataAccess.Delete
}
#endregion
}
}
}