File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed
Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ using Initium . Request ;
2+
3+ namespace Initium . Extensions ;
4+
5+ public static class PaginationExtensions
6+ {
7+ public static IEnumerable < TEntity > ApplyPagination < TEntity > ( this IQueryable < TEntity > source , PaginationParameters paginationParameters ) => source
8+ . Skip ( ( paginationParameters . Page - 1 ) * paginationParameters . PageSize )
9+ . Take ( paginationParameters . PageSize ) ;
10+
11+ public static IEnumerable < TModel > ApplyPagination < TModel > ( this IEnumerable < TModel > source , PaginationParameters paginationParameters ) => source
12+ . Skip ( ( paginationParameters . Page - 1 ) * paginationParameters . PageSize )
13+ . Take ( paginationParameters . PageSize ) ;
14+ }
Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Mvc ;
2+ using Tapper ;
3+
4+ namespace Initium . Request ;
5+
6+ [ TranspilationSource ]
7+ [ ModelBinder ( BinderType = typeof ( PaginationParametersBinder ) ) ]
8+ public class PaginationParameters
9+ {
10+ public int Page { get ; set ; } = 1 ;
11+ public int PageSize { get ; set ; } = 10 ;
12+ }
Original file line number Diff line number Diff line change 1+ using Microsoft . AspNetCore . Mvc . ModelBinding ;
2+
3+ namespace Initium . Request ;
4+
5+ public class PaginationParametersBinder : IModelBinder
6+ {
7+ public Task BindModelAsync ( ModelBindingContext bindingContext )
8+ {
9+ ArgumentNullException . ThrowIfNull ( bindingContext ) ;
10+
11+ var query = bindingContext . HttpContext . Request . Query ;
12+
13+ var pagination = new PaginationParameters
14+ {
15+ Page = int . TryParse ( query [ "Page" ] , out var page ) ? page : 1 ,
16+ PageSize = int . TryParse ( query [ "PageSize" ] , out var pageSize ) ? pageSize : 10
17+ } ;
18+
19+ bindingContext . Result = ModelBindingResult . Success ( pagination ) ;
20+ return Task . CompletedTask ;
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments