Skip to content

Commit 38cc386

Browse files
committed
Initial working version
1 parent b127550 commit 38cc386

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,46 @@ public ActionResult<ProductDto[]> GetAll()
4343
}
4444
}
4545

46+
/// <summary>
47+
/// Fetches all products or a page of products based on the provided parameters.
48+
/// </summary>
49+
/// <param name="skip">Previously called pageNumber. The number of the page to fetch. If this parameter is not provided, all products are fetched.</param>
50+
/// <param name="top">Previously called pageSize. The size of the page to fetch. If this parameter is not provided, all products are fetched.</param>
51+
/// <returns>A ProductDtoCollection object containing the fetched products and the total record count.</returns>
52+
[HttpGet("GetAllPagedProducts")]
53+
public ActionResult<ProductDtoCollection> GetAllProducts(int? skip, int? top)
54+
{
55+
int skipRecordsAmount = skip ?? 0;
56+
int currentSize = top ?? 0;
57+
58+
try
59+
{
60+
var products = this.productService.GetAll();
61+
var totalRecords = products.Length;
62+
63+
var pagedProducts = products
64+
.Skip(skipRecordsAmount)
65+
.Take(currentSize)
66+
.ToArray();
67+
68+
// Create a new ProductDtoCollection object
69+
// TODO, return also Page size, Page and totalPages = (int)Math.Ceiling(totalRecords / (double)currentSize);
70+
var productCollection = new ProductDtoCollection
71+
{
72+
// 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),
74+
TotalRecordsCount = totalRecords,
75+
};
76+
77+
return Ok(productCollection);
78+
}
79+
catch (Exception error)
80+
{
81+
logger.LogError(error.Message);
82+
return StatusCode(500);
83+
}
84+
}
85+
4686
[HttpGet("{id}")]
4787
public ActionResult<ProductDto> GetById(int id)
4888
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using NorthwindCRUD.Models.DbModels;
2+
3+
namespace NorthwindCRUD.Models.Dtos
4+
{
5+
public class ProductDtoCollection
6+
{
7+
//public List<ProductDto> Products { get; set; }
8+
public ICollection<ProductDto> Products { get; set; } = new List<ProductDto>();
9+
10+
public int TotalRecordsCount { get; set; }
11+
}
12+
}

NorthwindCRUD/Services/ProductService.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ public ProductDb[] GetAllByCategoryId(int id)
2929
return this.dataContext.Products.Where(p => p.CategoryId == id).ToArray();
3030
}
3131

32+
// New method for paging with category filter
33+
public ProductDb[] GetPagedProductsByCategoryId(int categoryId, int pageNumber, int pageSize)
34+
{
35+
return this.dataContext.Products
36+
.Where(p => p.CategoryId == categoryId)
37+
.Skip((pageNumber - 1) * pageSize)
38+
.Take(pageSize)
39+
.ToArray();
40+
}
41+
42+
// New method for paging with supplier filter
43+
public ProductDb[] GetPagedProductsBySupplierId(int supplierId, int pageNumber, int pageSize)
44+
{
45+
return this.dataContext.Products
46+
.Where(p => p.SupplierId == supplierId)
47+
.Skip((pageNumber - 1) * pageSize)
48+
.Take(pageSize)
49+
.ToArray();
50+
}
51+
3252
public ProductDb[] GetAllBySupplierId(int id)
3353
{
3454
return this.dataContext.Products.Where(p => p.SupplierId == id).ToArray();

0 commit comments

Comments
 (0)