added get async

This commit is contained in:
David Lebee 2019-02-12 22:53:22 -05:00
parent f3cc1f2fe3
commit 2bf96b1588
3 changed files with 57 additions and 7 deletions

View File

@ -1,4 +1,7 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
@ -8,6 +11,9 @@ namespace PoweredSoft.ObjectStorage.Core
{
string CollectionName { get; }
Task<List<TEntity>> GetAllAsync(CancellationToken cancellationToken = default(CancellationToken));
Task<List<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken));
Task<TEntity> GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken));
Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));
Task<TEntity> UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken));

View File

@ -33,5 +33,24 @@ namespace PoweredSoft.ObjectStorage.MongoDB.Tests
var shouldBeNull = collection.AsQueryable().FirstOrDefault(t => t.Id == updatedContact.Id);
Assert.Null(shouldBeNull);
}
[Fact]
public async Task TestGetAsync()
{
var osc = MongoDatabaseFactory.GetObjectStorageClient();
var collection = osc.GetCollection<Contact>();
var contact = await collection.AddAsync(new Contact
{
FirstName = "David",
LastName = "Lebee"
});
// make sure you can retreive it easily.
var fetchedUsingGetAsync = await collection.GetAsync(contact.Id);
Assert.NotNull(fetchedUsingGetAsync);
// now delete it.
await collection.DeleteAsync(fetchedUsingGetAsync);
}
}
}

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
@ -38,12 +39,7 @@ namespace PoweredSoft.ObjectStorage.MongoDB
protected virtual Expression<Func<TEntity, bool>> CreateEntityExpression(TEntity entity)
{
var objectKey = typeof(TEntity)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(t => t.GetCustomAttribute<BsonIdAttribute>() != null);
if (objectKey == null)
throw new Exception("Must have a BsonIdAttribute on one of the properties.");
var objectKey = GetKeyProperty();
var constant = objectKey.GetValue(entity);
var expression = QueryableHelpers.CreateConditionExpression<TEntity>(objectKey.Name,
DynamicLinq.ConditionOperators.Equal, constant, DynamicLinq.QueryConvertStrategy.LeaveAsIs);
@ -51,6 +47,16 @@ namespace PoweredSoft.ObjectStorage.MongoDB
return expression;
}
protected virtual PropertyInfo GetKeyProperty()
{
var objectKey = typeof(TEntity)
.GetProperties(BindingFlags.Public | BindingFlags.Instance)
.FirstOrDefault(t => t.GetCustomAttribute<BsonIdAttribute>() != null);
if (objectKey == null)
throw new Exception("Must have a BsonIdAttribute on one of the properties.");
return objectKey;
}
public async Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken))
{
var expression = CreateEntityExpression(entity);
@ -63,5 +69,24 @@ namespace PoweredSoft.ObjectStorage.MongoDB
await Collection.ReplaceOneAsync(expression, entity);
return entity;
}
public Task<List<TEntity>> GetAllAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return Collection.AsQueryable().ToListAsync(cancellationToken);
}
public Task<List<TEntity>> GetAllAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default(CancellationToken))
{
return Collection.Find(predicate).ToListAsync(cancellationToken);
}
public Task<TEntity> GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken))
{
var keyProp = GetKeyProperty();
var expression = QueryableHelpers.CreateConditionExpression<TEntity>(keyProp.Name,
DynamicLinq.ConditionOperators.Equal, key, DynamicLinq.QueryConvertStrategy.LeaveAsIs);
var result = Collection.Find(expression).FirstOrDefaultAsync();
return result;
}
}
}