Added tests for Updating methods
This commit is contained in:
@@ -8,6 +8,9 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
public class MongoDbContext : IMongoDbContext
|
||||
{
|
||||
private readonly IMongoClient _client;
|
||||
private readonly IMongoDatabase _database;
|
||||
|
||||
static MongoDbContext()
|
||||
{
|
||||
// Avoid legacy UUID representation: use Binary 0x04 subtype.
|
||||
@@ -16,14 +19,10 @@ namespace MongoDbGenericRepository
|
||||
|
||||
public MongoDbContext(string connectionString, string databaseName)
|
||||
{
|
||||
|
||||
_client = new MongoClient(connectionString);
|
||||
_database = _client.GetDatabase(databaseName);
|
||||
}
|
||||
|
||||
private readonly IMongoClient _client;
|
||||
private readonly IMongoDatabase _database;
|
||||
|
||||
/// <summary>
|
||||
/// The private GetCollection method
|
||||
/// </summary>
|
||||
@@ -62,6 +61,11 @@ namespace MongoDbGenericRepository
|
||||
_database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Very naively pluralizes a TDocument type name.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <returns></returns>
|
||||
private string Pluralize<TDocument>()
|
||||
{
|
||||
return typeof(TDocument).Name.ToLower() + "s";
|
||||
|
||||
@@ -55,6 +55,14 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
@@ -127,7 +135,27 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
#endregion Get
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -232,6 +260,18 @@ namespace MongoDbGenericRepository
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
@@ -472,43 +512,27 @@ namespace MongoDbGenericRepository
|
||||
#region Update
|
||||
|
||||
/// <summary>
|
||||
/// Updates a document
|
||||
/// Asynchronously Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> UpdateOneAsync<TDocument>(TDocument entity) where TDocument : IDocument
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
|
||||
{
|
||||
var updateRes = await GetCollection<TDocument>().ReplaceOneAsync(x => x.Id == entity.Id, entity);
|
||||
return updateRes.ModifiedCount < 1;
|
||||
var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(x => x.Id == modifiedDocument.Id, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UpdateOne with filter
|
||||
/// Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="update"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<long> UpdateOne<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update) where TDocument : IDocument
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
|
||||
{
|
||||
var updateRes = await GetCollection<TDocument>().UpdateOneAsync(filter, update);
|
||||
return updateRes.ModifiedCount;
|
||||
var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(x => x.Id == modifiedDocument.Id, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UpdateMany with filter
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="update"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<long> UpdateMany<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update) where TDocument : IDocument
|
||||
{
|
||||
var collection = GetCollection<TDocument>();
|
||||
var updateRes = await collection.UpdateManyAsync(filter, update);
|
||||
return updateRes.ModifiedCount;
|
||||
}
|
||||
#endregion Update
|
||||
|
||||
#region Find And Update
|
||||
|
||||
Reference in New Issue
Block a user