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 diff --git a/src/MongoDM/MongoDM.csproj b/src/MongoDM/MongoDM.csproj index 7ae7a7e5..bbbd7382 100644 --- a/src/MongoDM/MongoDM.csproj +++ b/src/MongoDM/MongoDM.csproj @@ -5,18 +5,18 @@ true Digicando.MongoDM true - 0.18.0-dev + 0.18.0 Digicando Srl ODM framework for MongoDB - + - - + + 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()); 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 - +