Skip to content

Commit

Permalink
Merging 0.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Sep 12, 2019
2 parents 84825a1 + 8adc34b commit 6f30683
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 29 deletions.
115 changes: 115 additions & 0 deletions src/MongoDM/Extensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Order and paginate a list of elements
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TKey">Ordering key type</typeparam>
/// <param name="values">Source values</param>
/// <param name="orderKeySelector">Ordering key selector</param>
/// <param name="page">Page to take</param>
/// <param name="take">Elements per page</param>
/// <returns>Selected elements page</returns>
public static IEnumerable<TSource> Paginate<TSource, TKey>(
this IEnumerable<TSource> values,
Func<TSource, TKey> 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);
}

/// <summary>
/// Order and paginate a list of elements
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TKey">Ordering key type</typeparam>
/// <param name="values">Source values</param>
/// <param name="orderKeySelector">Ordering key selector</param>
/// <param name="page">Page to take</param>
/// <param name="take">Elements per page</param>
/// <returns>Selected elements page</returns>
public static IMongoQueryable<TSource> Paginate<TSource, TKey>(
this IMongoQueryable<TSource> values,
Expression<Func<TSource, TKey>> 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);
}

/// <summary>
/// Descending order and paginate a list of elements
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TKey">Ordering key type</typeparam>
/// <param name="values">Source values</param>
/// <param name="orderKeySelector">Ordering key selector</param>
/// <param name="page">Page to take</param>
/// <param name="take">Elements per page</param>
/// <returns>Selected elements page</returns>
public static IEnumerable<TSource> PaginateDescending<TSource, TKey>(
this IEnumerable<TSource> values,
Func<TSource, TKey> 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);
}

/// <summary>
/// Descending order and paginate a list of elements
/// </summary>
/// <typeparam name="TSource">Source type</typeparam>
/// <typeparam name="TKey">Ordering key type</typeparam>
/// <param name="values">Source values</param>
/// <param name="orderKeySelector">Ordering key selector</param>
/// <param name="page">Page to take</param>
/// <param name="take">Elements per page</param>
/// <returns>Selected elements page</returns>
public static IMongoQueryable<TSource> PaginateDescending<TSource, TKey>(
this IMongoQueryable<TSource> values,
Expression<Func<TSource, TKey>> 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);
}
}
}
22 changes: 0 additions & 22 deletions src/MongoDM/Extensions/MongoQueryableExtensions.cs

This file was deleted.

8 changes: 4 additions & 4 deletions src/MongoDM/MongoDM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<RootNamespace>Digicando.MongoDM</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.18.0-dev</Version>
<Version>0.18.0</Version>
<Authors>Digicando Srl</Authors>
<Description>ODM framework for MongoDB</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DomainHelper" Version="0.18.0-dev" />
<PackageReference Include="Hangfire.Mongo" Version="0.6.2" />
<PackageReference Include="Hangfire.Mongo" Version="0.6.3" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.2.0" />
<PackageReference Include="MongoDB.Driver" Version="2.8.1" />
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.8.1" />
<PackageReference Include="MongoDB.Driver" Version="2.9.1" />
<PackageReference Include="MongoDB.Driver.GridFS" Version="2.9.1" />
<PackageReference Include="morelinq" Version="3.2.0" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/MongoDM/Repositories/CollectionRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ protected override async Task<TModel> FindOneOnDBAsync(TKey id, CancellationToke
var filter = Builders<TModel>.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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/MongoDM/Repositories/GridFSRepositoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected override async Task<TModel> FindOneOnDBAsync(string id, CancellationTo
var filter = Builders<GridFSFileInfo>.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<TModel>();
ReflectionHelper.SetValue(file, m => m.Id, mongoFile.Id.ToString());
Expand Down
2 changes: 1 addition & 1 deletion test/MongoDM.Tests/MongoDM.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.12.0" />
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="xunit" Version="2.4.1" />
</ItemGroup>

Expand Down

0 comments on commit 6f30683

Please sign in to comment.