@@ -17,15 +17,17 @@ public class ProductsController : ControllerBase
1717 private readonly CategoryService categoryService ;
1818 private readonly OrderService orderService ;
1919 private readonly SupplierService supplierService ;
20+ private readonly PagingService pagingService ;
2021 private readonly IMapper mapper ;
2122 private readonly ILogger < ProductsController > logger ;
2223
23- public ProductsController ( ProductService productService , CategoryService categoryService , OrderService orderService , SupplierService supplierService , IMapper mapper , ILogger < ProductsController > logger )
24+ public ProductsController ( ProductService productService , CategoryService categoryService , OrderService orderService , SupplierService supplierService , PagingService pagingService , IMapper mapper , ILogger < ProductsController > logger )
2425 {
2526 this . productService = productService ;
2627 this . categoryService = categoryService ;
2728 this . orderService = orderService ;
2829 this . supplierService = supplierService ;
30+ this . pagingService = pagingService ;
2931 this . mapper = mapper ;
3032 this . logger = logger ;
3133 }
@@ -51,57 +53,31 @@ public ActionResult<ProductDto[]> GetAll()
5153 /// <param name="skip">Previously called pageNumber. The number of the page to fetch. If this parameter is not provided, all products are fetched.</param>
5254 /// <param name="top">Previously called pageSize. The size of the page to fetch. If this parameter is not provided, all products are fetched.</param>
5355 /// <param name="orderBy">The fields to order by, in the format "field1 asc, field2 desc". If not provided, defaults to no specific order.</param>
54- /// <returns>A PagedProductsDto object containing the fetched products and the total record count.</returns>
56+ /// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
5557 [ HttpGet ( "GetAllPagedProducts" ) ]
56- public ActionResult < PagedProductsDto > GetAllProducts ( int ? skip , int ? top , string ? orderBy )
58+ public ActionResult < PagedResultDto < ProductDto > > GetAllProducts ( int ? skip , int ? top , string ? orderBy )
5759 {
5860 try
5961 {
6062 // Retrieve all products
6163 var products = this . productService . GetAll ( ) ;
62- var totalRecords = products . Length ;
6364
64- // Default skip and top if not provided
65- int skipRecordsAmount = skip ?? 0 ;
66- int currentSize = top ?? totalRecords ;
65+ // Get paged data
66+ var pagedResult = pagingService . GetPagedData ( products , skip , top , orderBy ) ;
6767
68- // Apply ordering if specified
69- if ( ! string . IsNullOrEmpty ( orderBy ) )
70- {
71- var orderByParts = orderBy . Split ( ' ' ) ;
72- var field = orderByParts [ 0 ] ;
73- var order = orderByParts . Length > 1 ? orderByParts [ 1 ] : "ASC" ;
74-
75- // Get the property info of the field to order by
76- var propertyInfo = products . First ( ) . GetType ( ) . GetProperty ( field , BindingFlags . IgnoreCase | BindingFlags . Public | BindingFlags . Instance ) ;
77-
78- // Use dynamic LINQ to sort based on field and order
79- products = order . ToUpper ( CultureInfo . InvariantCulture ) == "DESC"
80- ? products . OrderByDescending ( e => propertyInfo ? . GetValue ( e , null ) ) . ToArray ( )
81- : products . OrderBy ( e => propertyInfo ? . GetValue ( e , null ) ) . ToArray ( ) ;
82- }
83-
84- // Apply pagination
85- var pagedProducts = products
86- . Skip ( skipRecordsAmount )
87- . Take ( currentSize )
88- . ToArray ( ) ;
89-
90- // Calculate total pages
91- int totalPages = ( int ) Math . Ceiling ( totalRecords / ( double ) currentSize ) ;
68+ // Map the results to ProductDto
69+ var productDtos = this . mapper . Map < ProductDto [ ] > ( pagedResult . Items ) ;
9270
93- // Create and return the product collection
94- var productCollection = new PagedProductsDto
71+ var result = new PagedResultDto < ProductDto >
9572 {
96- // Check if both pageNumber and pageSize are null, if so, return all products
97- Products = this . mapper . Map < ProductDb [ ] , ProductDto [ ] > ( pagedProducts ) ,
98- TotalRecordsCount = totalRecords ,
99- PageSize = currentSize ,
100- PageNumber = ( skipRecordsAmount / currentSize ) + 1 ,
101- TotalPages = totalPages ,
73+ Items = productDtos ,
74+ TotalRecordsCount = pagedResult . TotalRecordsCount ,
75+ PageSize = pagedResult . PageSize ,
76+ PageNumber = pagedResult . PageNumber ,
77+ TotalPages = pagedResult . TotalPages ,
10278 } ;
10379
104- return Ok ( productCollection ) ;
80+ return Ok ( result ) ;
10581 }
10682 catch ( Exception error )
10783 {
0 commit comments