From 2bf96b1588eb21ed61f2aa769e27ccdd478aa818 Mon Sep 17 00:00:00 2001 From: David Lebee Date: Tue, 12 Feb 2019 22:53:22 -0500 Subject: [PATCH] added get async --- .../IObjectStorageCollection.cs | 8 +++- .../CrudTests.cs | 19 ++++++++++ .../MongoObjectStorageCollection.cs | 37 ++++++++++++++++--- 3 files changed, 57 insertions(+), 7 deletions(-) diff --git a/PoweredSoft.ObjectStorage.Core/IObjectStorageCollection.cs b/PoweredSoft.ObjectStorage.Core/IObjectStorageCollection.cs index ba1a307..0100a97 100644 --- a/PoweredSoft.ObjectStorage.Core/IObjectStorageCollection.cs +++ b/PoweredSoft.ObjectStorage.Core/IObjectStorageCollection.cs @@ -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> GetAllAsync(CancellationToken cancellationToken = default(CancellationToken)); + Task> GetAllAsync(Expression> predicate, CancellationToken cancellationToken = default(CancellationToken)); + Task GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken)); Task AddAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); Task DeleteAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); Task UpdateAsync(TEntity entity, CancellationToken cancellationToken = default(CancellationToken)); diff --git a/PoweredSoft.ObjectStorage.MongoDB.Tests/CrudTests.cs b/PoweredSoft.ObjectStorage.MongoDB.Tests/CrudTests.cs index 3d5d1af..8d47089 100644 --- a/PoweredSoft.ObjectStorage.MongoDB.Tests/CrudTests.cs +++ b/PoweredSoft.ObjectStorage.MongoDB.Tests/CrudTests.cs @@ -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(); + 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); + } } } diff --git a/PoweredSoft.ObjectStorage.MongoDB/MongoObjectStorageCollection.cs b/PoweredSoft.ObjectStorage.MongoDB/MongoObjectStorageCollection.cs index 5828e93..784f36d 100644 --- a/PoweredSoft.ObjectStorage.MongoDB/MongoObjectStorageCollection.cs +++ b/PoweredSoft.ObjectStorage.MongoDB/MongoObjectStorageCollection.cs @@ -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> CreateEntityExpression(TEntity entity) { - var objectKey = typeof(TEntity) - .GetProperties(BindingFlags.Public | BindingFlags.Instance) - .FirstOrDefault(t => t.GetCustomAttribute() != 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(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() != 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> GetAllAsync(CancellationToken cancellationToken = default(CancellationToken)) + { + return Collection.AsQueryable().ToListAsync(cancellationToken); + } + + public Task> GetAllAsync(Expression> predicate, CancellationToken cancellationToken = default(CancellationToken)) + { + return Collection.Find(predicate).ToListAsync(cancellationToken); + } + + public Task GetAsync(object key, CancellationToken cancellationToken = default(CancellationToken)) + { + var keyProp = GetKeyProperty(); + var expression = QueryableHelpers.CreateConditionExpression(keyProp.Name, + DynamicLinq.ConditionOperators.Equal, key, DynamicLinq.QueryConvertStrategy.LeaveAsIs); + var result = Collection.Find(expression).FirstOrDefaultAsync(); + return result; + } } }