Skip to content

Commit b6843a1

Browse files
committed
Adding server-side paging for categories
1 parent 2b31741 commit b6843a1

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

NorthwindCRUD/Controllers/CategoriesController.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ public class CategoriesController : ControllerBase
1313
{
1414
private readonly CategoryService categoryService;
1515
private readonly ProductService productService;
16+
private readonly PagingService pagingService;
1617
private readonly IMapper mapper;
1718
private readonly ILogger<CategoriesController> logger;
1819

19-
public CategoriesController(CategoryService categoryService, ProductService productService, IMapper mapper, ILogger<CategoriesController> logger)
20+
public CategoriesController(CategoryService categoryService, ProductService productService, PagingService pagingService, IMapper mapper, ILogger<CategoriesController> logger)
2021
{
2122
this.categoryService = categoryService;
2223
this.productService = productService;
24+
this.pagingService = pagingService;
2325
this.mapper = mapper;
2426
this.logger = logger;
2527
}
@@ -39,6 +41,33 @@ public ActionResult<CategoryDto[]> GetAll()
3941
}
4042
}
4143

44+
/// <summary>
45+
/// Fetches all categories or a page of categories based on the provided parameters.
46+
/// </summary>
47+
/// <param name="skip">The number of records to skip before starting to fetch the categories. If this parameter is not provided, fetching starts from the beginning.</param>
48+
/// <param name="top">The maximum number of categories to fetch. If this parameter is not provided, all categories are fetched.</param>
49+
/// <param name="orderBy">A comma-separated list of fields to order the categories by, along with the sort direction (e.g., "field1 asc, field2 desc").</param>
50+
/// <returns>A PagedResultDto object containing the fetched T and the total record count.</returns>
51+
[HttpGet("GetPagedCategories")]
52+
public ActionResult<PagedResultDto<ProductDto>> GetPagedCategories(int? skip, int? top, string? orderBy)
53+
{
54+
try
55+
{
56+
// Retrieve all categories
57+
var categories = this.categoryService.GetAll();
58+
59+
// Get paged data
60+
var pagedResult = pagingService.GetPagedData<CategoryDb, CategoryDto>(categories, skip, top, orderBy);
61+
62+
return Ok(pagedResult);
63+
}
64+
catch (Exception error)
65+
{
66+
logger.LogError(error.Message);
67+
return StatusCode(500);
68+
}
69+
}
70+
4271
[HttpGet("{id}")]
4372
public ActionResult<CategoryDto> GetById(int id)
4473
{

0 commit comments

Comments
 (0)