some batch actions, and added some methods while being there :)
This commit is contained in:
parent
e7383e75f6
commit
516d18314b
@ -20,5 +20,38 @@ namespace PoweredSoft.ObjectStorage.Core
|
||||
Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
|
||||
IQueryable<TEntity> AsQueryable();
|
||||
List<PropertyInfo> GetObjectKeys();
|
||||
Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
|
||||
|
||||
Task UpdateManyAsync<TField>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task UpdateManyAsync<TField, TField2>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task UpdateManyAsync<TField, TField2, TField3>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
Expression<Func<TEntity, TField3>> fieldExpression3, TField3 value3,
|
||||
CancellationToken cancellationToken = default);
|
||||
|
||||
Task UpdateOneAsync<TField>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
CancellationToken cancellationToken = default);
|
||||
Task UpdateOneAsync<TField, TField2>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
CancellationToken cancellationToken = default);
|
||||
Task UpdateOneAsync<TField, TField2, TField3>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
Expression<Func<TEntity, TField3>> fieldExpression3, TField3 value3,
|
||||
CancellationToken cancellationToken = default);
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,35 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests
|
||||
{
|
||||
public class CrudTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task UpdateMany()
|
||||
{
|
||||
var osc = MongoDatabaseFactory.GetObjectStorageContext();
|
||||
var collection = osc.GetCollection<Contact>();
|
||||
var a = await collection.AddAsync(new Contact
|
||||
{
|
||||
LastName = "A",
|
||||
FirstName = "A"
|
||||
});
|
||||
|
||||
var b = await collection.AddAsync(new Contact
|
||||
{
|
||||
LastName = "B",
|
||||
FirstName = "B"
|
||||
});
|
||||
|
||||
await collection.UpdateManyAsync(t => t.LastName == "A" || t.LastName == "B", t => t.LastName, "C");
|
||||
var howManyCs = await collection.GetAllAsync(t => t.LastName == "C");
|
||||
var howManyCs2 = await collection.LongCountAsync(t => t.LastName == "C");
|
||||
Assert.Equal(2, howManyCs.Count);
|
||||
Assert.Equal(2, howManyCs2);
|
||||
|
||||
|
||||
// clean up.
|
||||
await collection.DeleteAsync(a);
|
||||
await collection.DeleteAsync(b);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CreateUpdateThenDelete()
|
||||
{
|
||||
@ -26,6 +55,12 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests
|
||||
var updatedContact = await collection.UpdateAsync(contact);
|
||||
Assert.Equal("Norris", updatedContact.LastName);
|
||||
|
||||
// update name back to not norris
|
||||
await collection.UpdateOneAsync(t => t.Id == contact.Id, t => t.LastName, "Not Norris");
|
||||
|
||||
var updatedContact2 = await collection.FirstAsync(t => t.Id == contact.Id);
|
||||
Assert.Equal("Not Norris", updatedContact2.LastName);
|
||||
|
||||
// delete the test.
|
||||
await collection.DeleteAsync(updatedContact);
|
||||
|
||||
@ -49,6 +84,13 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests
|
||||
var fetchedUsingGetAsync = await collection.GetAsync(contact.Id);
|
||||
Assert.NotNull(fetchedUsingGetAsync);
|
||||
|
||||
Assert.True(await collection.AnyAsync(t => t.Id == contact.Id), "Any async does not work");
|
||||
Assert.NotNull(await collection.FirstOrDefaultAsync(t => t.Id == contact.Id));
|
||||
|
||||
// does not crash.
|
||||
await collection.FirstAsync(t => t.Id == contact.Id);
|
||||
|
||||
|
||||
// now delete it.
|
||||
await collection.DeleteAsync(fetchedUsingGetAsync);
|
||||
}
|
||||
|
@ -6,7 +6,8 @@ using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Driver.Linq;
|
||||
using PoweredSoft.DynamicLinq.Helpers;
|
||||
using PoweredSoft.ObjectStorage.Core;
|
||||
|
||||
@ -32,6 +33,11 @@ namespace PoweredSoft.ObjectStorage.MongoDB
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Task<bool> AnyAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return MongoQueryable.AnyAsync(Collection.AsQueryable(), predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public IQueryable<TEntity> AsQueryable()
|
||||
{
|
||||
return Collection.AsQueryable();
|
||||
@ -95,6 +101,131 @@ namespace PoweredSoft.ObjectStorage.MongoDB
|
||||
{
|
||||
GetBsonIdProperty()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return this.Collection.AsQueryable().FirstOrDefaultAsync(predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public Task<TEntity> FirstAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return this.Collection.AsQueryable().FirstAsync(predicate, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateManyAsync<TField>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TField>> fieldExpression, TField value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var updateDefinition = Builders<TEntity>.Update.Set<TField>(fieldExpression, value);
|
||||
await Collection.UpdateManyAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateManyAsync<TField, TField2>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var updateDefinition = Builders<TEntity>.Update
|
||||
.Set(fieldExpression, value)
|
||||
.Set(fieldExpression2, value2)
|
||||
;
|
||||
|
||||
await Collection.UpdateManyAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateManyAsync<TField, TField2, TField3>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
Expression<Func<TEntity, TField3>> fieldExpression3, TField3 value3,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var updateDefinition = Builders<TEntity>.Update
|
||||
.Set(fieldExpression, value)
|
||||
.Set(fieldExpression2, value2)
|
||||
.Set(fieldExpression3, value3)
|
||||
;
|
||||
|
||||
await Collection.UpdateManyAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateOneAsync<TField>(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TField>> fieldExpression, TField value, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var updateDefinition = Builders<TEntity>.Update.Set<TField>(fieldExpression, value);
|
||||
await Collection.UpdateOneAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateOneAsync<TField, TField2>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var updateDefinition = Builders<TEntity>.Update
|
||||
.Set(fieldExpression, value)
|
||||
.Set(fieldExpression2, value2);
|
||||
|
||||
await Collection.UpdateOneAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateOneAsync<TField, TField2, TField3>(Expression<Func<TEntity, bool>> predicate,
|
||||
Expression<Func<TEntity, TField>> fieldExpression, TField value,
|
||||
Expression<Func<TEntity, TField2>> fieldExpression2, TField2 value2,
|
||||
Expression<Func<TEntity, TField3>> fieldExpression3, TField3 value3,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var updateDefinition = Builders<TEntity>.Update
|
||||
.Set(fieldExpression, value)
|
||||
.Set(fieldExpression2, value2)
|
||||
.Set(fieldExpression3, value3)
|
||||
;
|
||||
|
||||
await Collection.UpdateOneAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateOneAsync(Expression<Func<TEntity, bool>> predicate, UpdateDefinition<TEntity> updateDefinition, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await Collection.UpdateOneAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task UpdateManyAsync(Expression<Func<TEntity, bool>> predicate, UpdateDefinition<TEntity> updateDefinition, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await Collection.UpdateManyAsync(predicate, updateDefinition, new UpdateOptions()
|
||||
{
|
||||
IsUpsert = false
|
||||
}, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<int> CountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var longResult = await Collection.CountDocumentsAsync(predicate, null, cancellationToken);
|
||||
if (longResult > int.MaxValue)
|
||||
throw new Exception("Exceeds integer maximum value");
|
||||
return (int)longResult;
|
||||
}
|
||||
|
||||
public Task<long> LongCountAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Collection.CountDocumentsAsync(predicate, null, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user