@@ -52,26 +52,35 @@ public ActionResult<ProductDto[]> GetAll()
5252 [ HttpGet ( "GetAllPagedProducts" ) ]
5353 public ActionResult < ProductDtoCollection > GetAllProducts ( int ? skip , int ? top )
5454 {
55- int skipRecordsAmount = skip ?? 0 ;
56- int currentSize = top ?? 0 ;
57-
5855 try
5956 {
57+ // Retrieve all products
6058 var products = this . productService . GetAll ( ) ;
6159 var totalRecords = products . Length ;
6260
61+ // Default skip and top if not provided
62+ int skipRecordsAmount = skip ?? 0 ;
63+ int currentSize = top ?? totalRecords ;
64+
65+ // Apply pagination
6366 var pagedProducts = products
6467 . Skip ( skipRecordsAmount )
6568 . Take ( currentSize )
6669 . ToArray ( ) ;
6770
68- // Create a new ProductDtoCollection object
69- // TODO, return also Page size, Page and totalPages = (int)Math.Ceiling(totalRecords / (double)currentSize);
71+ // Calculate total pages
72+ int totalPages = ( int ) Math . Ceiling ( totalRecords / ( double ) currentSize ) ;
73+
74+ // Create and return the product collection
7075 var productCollection = new ProductDtoCollection
7176 {
7277 // Check if both pageNumber and pageSize are null, if so, return all products
73- Products = this . mapper . Map < ProductDb [ ] , ProductDto [ ] > ( ( skipRecordsAmount == 0 && currentSize == 0 ) ? products : pagedProducts ) ,
78+ Products = this . mapper . Map < ProductDb [ ] , ProductDto [ ] > ( pagedProducts ) ,
7479 TotalRecordsCount = totalRecords ,
80+ PageSize = currentSize ,
81+ PageNumber = ( skipRecordsAmount / currentSize ) + 1 ,
82+ TotalPages = totalPages ,
83+
7584 } ;
7685
7786 return Ok ( productCollection ) ;
0 commit comments