From 1ffccbcb9e832c6b5b5e4540c474064af302fe49 Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 5 Sep 2019 19:13:54 +0200 Subject: [PATCH 1/4] Updated dependencies --- src/MongoDM/MongoDM.csproj | 6 +++--- test/MongoDM.Tests/MongoDM.Tests.csproj | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/MongoDM/MongoDM.csproj b/src/MongoDM/MongoDM.csproj index 7ae7a7e5..940ab715 100644 --- a/src/MongoDM/MongoDM.csproj +++ b/src/MongoDM/MongoDM.csproj @@ -12,11 +12,11 @@ - + - - + + diff --git a/test/MongoDM.Tests/MongoDM.Tests.csproj b/test/MongoDM.Tests/MongoDM.Tests.csproj index f3efdbc4..c58ea005 100644 --- a/test/MongoDM.Tests/MongoDM.Tests.csproj +++ b/test/MongoDM.Tests/MongoDM.Tests.csproj @@ -17,7 +17,7 @@ all runtime; build; native; contentfiles; analyzers - + From 8d50cb8dfa641d41d1654188d5b7635dbb3e2d0a Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 12 Sep 2019 17:05:22 +0200 Subject: [PATCH 2/4] Added KeyNotFoundException message --- src/MongoDM/Repositories/CollectionRepositoryBase.cs | 2 +- src/MongoDM/Repositories/GridFSRepositoryBase.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MongoDM/Repositories/CollectionRepositoryBase.cs b/src/MongoDM/Repositories/CollectionRepositoryBase.cs index a5e41ed2..c0257751 100644 --- a/src/MongoDM/Repositories/CollectionRepositoryBase.cs +++ b/src/MongoDM/Repositories/CollectionRepositoryBase.cs @@ -158,7 +158,7 @@ protected override async Task FindOneOnDBAsync(TKey id, CancellationToke var filter = Builders.Filter.Eq(m => m.Id, id); var element = await Collection.Find(filter).SingleOrDefaultAsync(cancellationToken); if (element as TModel == default(TModel)) - throw new KeyNotFoundException(); + throw new KeyNotFoundException($"Can't find key {id}"); return element as TModel; } diff --git a/src/MongoDM/Repositories/GridFSRepositoryBase.cs b/src/MongoDM/Repositories/GridFSRepositoryBase.cs index 6c5a07e7..c460eedc 100644 --- a/src/MongoDM/Repositories/GridFSRepositoryBase.cs +++ b/src/MongoDM/Repositories/GridFSRepositoryBase.cs @@ -80,7 +80,7 @@ protected override async Task FindOneOnDBAsync(string id, CancellationTo var filter = Builders.Filter.Eq("_id", ObjectId.Parse(id)); var mongoFile = await GridFSBucket.Find(filter).SingleOrDefaultAsync(cancellationToken); if (mongoFile == null) - throw new KeyNotFoundException(); + throw new KeyNotFoundException($"Can't find key {id}"); var file = ProxyGenerator.CreateInstance(); ReflectionHelper.SetValue(file, m => m.Id, mongoFile.Id.ToString()); From 1cbfa6ea2e356dde5d355eed37edb8cafa63b3be Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 12 Sep 2019 17:28:11 +0200 Subject: [PATCH 3/4] Updated EnumerableExtensions --- .../Extensions/EnumerableExtensions.cs | 115 ++++++++++++++++++ .../Extensions/MongoQueryableExtensions.cs | 22 ---- 2 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 src/MongoDM/Extensions/EnumerableExtensions.cs delete mode 100644 src/MongoDM/Extensions/MongoQueryableExtensions.cs diff --git a/src/MongoDM/Extensions/EnumerableExtensions.cs b/src/MongoDM/Extensions/EnumerableExtensions.cs new file mode 100644 index 00000000..838e2c44 --- /dev/null +++ b/src/MongoDM/Extensions/EnumerableExtensions.cs @@ -0,0 +1,115 @@ +using MongoDB.Driver.Linq; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; + +namespace Digicando.MongoDM.Extensions +{ + public static class EnumerableExtensions + { + /// + /// Order and paginate a list of elements + /// + /// Source type + /// Ordering key type + /// Source values + /// Ordering key selector + /// Page to take + /// Elements per page + /// Selected elements page + public static IEnumerable Paginate( + this IEnumerable values, + Func orderKeySelector, + int page, + int take) + { + if (page < 0) + throw new ArgumentOutOfRangeException(nameof(page)); + if (take < 1) + throw new ArgumentOutOfRangeException(nameof(take)); + + return values.OrderBy(orderKeySelector) + .Skip(page * take) + .Take(take); + } + + /// + /// Order and paginate a list of elements + /// + /// Source type + /// Ordering key type + /// Source values + /// Ordering key selector + /// Page to take + /// Elements per page + /// Selected elements page + public static IMongoQueryable Paginate( + this IMongoQueryable values, + Expression> orderKeySelector, + int page, + int take) + { + if (page < 0) + throw new ArgumentOutOfRangeException(nameof(page)); + if (take < 1) + throw new ArgumentOutOfRangeException(nameof(take)); + + return values.OrderBy(orderKeySelector) + .Skip(page * take) + .Take(take); + } + + /// + /// Descending order and paginate a list of elements + /// + /// Source type + /// Ordering key type + /// Source values + /// Ordering key selector + /// Page to take + /// Elements per page + /// Selected elements page + public static IEnumerable PaginateDescending( + this IEnumerable values, + Func orderKeySelector, + int page, + int take) + { + if (page < 0) + throw new ArgumentOutOfRangeException(nameof(page)); + if (take < 1) + throw new ArgumentOutOfRangeException(nameof(take)); + + return values.OrderByDescending(orderKeySelector) + .Skip(page * take) + .Take(take); + } + + /// + /// Descending order and paginate a list of elements + /// + /// Source type + /// Ordering key type + /// Source values + /// Ordering key selector + /// Page to take + /// Elements per page + /// Selected elements page + public static IMongoQueryable PaginateDescending( + this IMongoQueryable values, + Expression> orderKeySelector, + int page, + int take) + { + if (page < 0) + throw new ArgumentOutOfRangeException(nameof(page)); + if (take < 1) + throw new ArgumentOutOfRangeException(nameof(take)); + + return values.OrderByDescending(orderKeySelector) + .Skip(page * take) + .Take(take); + } + } +} \ No newline at end of file diff --git a/src/MongoDM/Extensions/MongoQueryableExtensions.cs b/src/MongoDM/Extensions/MongoQueryableExtensions.cs deleted file mode 100644 index f84a11e9..00000000 --- a/src/MongoDM/Extensions/MongoQueryableExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -using MongoDB.Driver.Linq; -using System; -using System.Linq; - -namespace Digicando.MongoDM.Extensions -{ - public static class MongoQueryableExtensions - { - public static IMongoQueryable Paginate(this IMongoQueryable values, int page, int take) - { - if (page < 0) - throw new ArgumentOutOfRangeException(nameof(page)); - if (take < 0) - throw new ArgumentOutOfRangeException(nameof(take)); - - if (take > 0) - values = values.Skip(page * take).Take(take); - - return values; - } - } -} \ No newline at end of file From 8adc34b31b8c83612f840a082648c672fb01808d Mon Sep 17 00:00:00 2001 From: Mirko Da Corte Date: Thu, 12 Sep 2019 17:29:59 +0200 Subject: [PATCH 4/4] Updated version --- src/MongoDM/MongoDM.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MongoDM/MongoDM.csproj b/src/MongoDM/MongoDM.csproj index 940ab715..bbbd7382 100644 --- a/src/MongoDM/MongoDM.csproj +++ b/src/MongoDM/MongoDM.csproj @@ -5,7 +5,7 @@ true Digicando.MongoDM true - 0.18.0-dev + 0.18.0 Digicando Srl ODM framework for MongoDB