11using System . Globalization ;
22using System . Reflection ;
3+ using AutoMapper ;
4+ using Microsoft . AspNetCore . Mvc . RazorPages ;
35using NorthwindCRUD . Models . Dtos ;
46
57namespace NorthwindCRUD . Services
68{
79 public interface IPagingService
810 {
9- PagedResultDto < T > GetPagedData < T > ( IEnumerable < T > data , int ? skip , int ? top , string ? orderBy ) ;
11+ PagedResultDto < TDto > GetPagedData < TEntity , TDto > ( IEnumerable < TEntity > data , int ? skip , int ? top , string ? orderBy ) ;
1012 }
1113
1214 public class PagingService : IPagingService
1315 {
14- public PagedResultDto < T > GetPagedData < T > ( IEnumerable < T > data , int ? skip , int ? top , string ? orderBy )
16+ private readonly IMapper mapper ;
17+
18+ public PagingService ( IMapper mapper )
19+ {
20+ this . mapper = mapper ;
21+ }
22+
23+ public PagedResultDto < TDto > GetPagedData < TEntity , TDto > ( IEnumerable < TEntity > data , int ? skip , int ? top , string ? orderBy )
1524 {
1625 var dataArray = data . ToArray ( ) ;
1726 var totalRecords = dataArray . Length ;
@@ -27,7 +36,7 @@ public PagedResultDto<T> GetPagedData<T>(IEnumerable<T> data, int? skip, int? to
2736 var field = orderByParts [ 0 ] ;
2837 var order = orderByParts . Length > 1 ? orderByParts [ 1 ] : "ASC" ;
2938
30- var propertyInfo = typeof ( T ) . GetProperty ( field , BindingFlags . IgnoreCase | BindingFlags . Public | BindingFlags . Instance ) ;
39+ var propertyInfo = typeof ( TEntity ) . GetProperty ( field , BindingFlags . IgnoreCase | BindingFlags . Public | BindingFlags . Instance ) ;
3140
3241 if ( propertyInfo != null )
3342 {
@@ -46,9 +55,12 @@ public PagedResultDto<T> GetPagedData<T>(IEnumerable<T> data, int? skip, int? to
4655 // Calculate total pages
4756 int totalPages = ( int ) Math . Ceiling ( totalRecords / ( double ) currentSize ) ;
4857
49- return new PagedResultDto < T >
58+ // Map the results to ProductDto
59+ var pagedDataDtos = mapper . Map < TDto [ ] > ( pagedData ) ;
60+
61+ return new PagedResultDto < TDto >
5062 {
51- Items = pagedData ,
63+ Items = pagedDataDtos ,
5264 TotalRecordsCount = totalRecords ,
5365 PageSize = currentSize ,
5466 PageNumber = ( skipRecordsAmount / currentSize ) + 1 ,
0 commit comments