11using System ;
2+ using System . Collections . Generic ;
23using System . Linq . Expressions ;
4+ using System . Threading ;
35using System . Threading . Tasks ;
46using MongoDB . Driver ;
57using NetCoreKit . Domain ;
@@ -11,38 +13,58 @@ public static class MongoQueryRepositoryExtensions
1113 public static async Task < TEntity > FindOneAsync < TEntity , TId > (
1214 this IMongoQueryRepository < TEntity > repo ,
1315 Expression < Func < TEntity , TId > > filter ,
14- TId id )
16+ TId id ,
17+ CancellationToken cancellationToken = default ( CancellationToken ) )
1518 where TEntity : class , IAggregateRoot
1619 {
1720 var collection = repo . DbContext . Collection < TEntity > ( ) ;
1821 var filterDef = Builders < TEntity > . Filter . Eq ( filter , id ) ;
19- return await collection . Find ( filterDef ) . FirstOrDefaultAsync ( ) ;
22+ return await collection . Find ( filterDef ) . FirstOrDefaultAsync ( cancellationToken ) ;
23+ }
24+
25+ public static async Task < IReadOnlyList < TEntity > > FindListByFieldAsync < TEntity > (
26+ this IMongoQueryRepository < TEntity > repo ,
27+ string fieldName ,
28+ string fieldValue ,
29+ CancellationToken cancellationToken = default ( CancellationToken ) )
30+ where TEntity : class , IAggregateRoot
31+ {
32+ // only use it if you don't have other solutions :p
33+ var filter = Builders < TEntity > . Filter . Eq ( fieldName , fieldValue ) ;
34+ return await repo
35+ . DbContext
36+ . Collection < TEntity > ( )
37+ . Find ( filter )
38+ . ToListAsync ( cancellationToken ) ;
2039 }
2140
2241 public static async Task < PaginatedItem < TResponse > > QueryAsync < TEntity , TResponse > (
2342 this IMongoQueryRepository < TEntity > repo ,
2443 Criterion criterion ,
25- Expression < Func < TEntity , TResponse > > selector )
44+ Expression < Func < TEntity , TResponse > > selector ,
45+ CancellationToken cancellationToken = default ( CancellationToken ) )
2646 where TEntity : class , IAggregateRoot
2747 {
28- return await GetDataAsync ( repo , criterion , selector ) ;
48+ return await GetDataAsync ( repo , criterion , selector , null , cancellationToken ) ;
2949 }
3050
3151 public static async Task < PaginatedItem < TResponse > > FindAllAsync < TEntity , TResponse > (
3252 this IMongoQueryRepository < TEntity > repo ,
3353 Criterion criterion ,
3454 Expression < Func < TEntity , TResponse > > selector ,
35- Expression < Func < TEntity , bool > > filter )
55+ Expression < Func < TEntity , bool > > filter ,
56+ CancellationToken cancellationToken = default ( CancellationToken ) )
3657 where TEntity : class , IAggregateRoot
3758 {
38- return await GetDataAsync ( repo , criterion , selector , filter ) ;
59+ return await GetDataAsync ( repo , criterion , selector , filter , cancellationToken ) ;
3960 }
4061
4162 private static async Task < PaginatedItem < TResponse > > GetDataAsync < TEntity , TResponse > (
4263 IMongoQueryRepository < TEntity > repo ,
4364 Criterion criterion ,
4465 Expression < Func < TEntity , TResponse > > selector ,
45- Expression < Func < TEntity , bool > > filter = null )
66+ Expression < Func < TEntity , bool > > filter = null ,
67+ CancellationToken cancellationToken = default ( CancellationToken ) )
4668 where TEntity : class , IAggregateRoot
4769 {
4870 var collection = repo . DbContext . Collection < TEntity > ( ) ;
@@ -69,9 +91,9 @@ private static async Task<PaginatedItem<TResponse>> GetDataAsync<TEntity, TRespo
6991 . SortEntity ( sortDef )
7092 . Skip ( criterion . CurrentPage * criterion . PageSize )
7193 . Limit ( criterion . PageSize )
72- . ToListAsync ( ) ;
94+ . ToListAsync ( cancellationToken ) ;
7395
74- var totalRecord = await collection . CountDocumentsAsync ( FilterDefinition < TEntity > . Empty ) ;
96+ var totalRecord = await collection . CountDocumentsAsync ( FilterDefinition < TEntity > . Empty , null , cancellationToken ) ;
7597 var totalPages = ( int ) Math . Ceiling ( ( double ) totalRecord / criterion . PageSize ) ;
7698
7799 if ( criterion . CurrentPage > totalPages )
0 commit comments