Skip to content

Commit 13d89ec

Browse files
committed
FInal polishing
1 parent e1f7d40 commit 13d89ec

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

NorthwindCRUD/Controllers/ProductsController.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,9 @@ public ActionResult<PagedResultDto<ProductDto>> GetAllProducts(int? skip, int? t
6363
var products = this.productService.GetAll();
6464

6565
// Get paged data
66-
var pagedResult = pagingService.GetPagedData(products, skip, top, orderBy);
66+
var pagedResult = pagingService.GetPagedData<ProductDb, ProductDto>(products, skip, top, orderBy);
6767

68-
// Map the results to ProductDto
69-
var productDtos = this.mapper.Map<ProductDto[]>(pagedResult.Items);
70-
71-
var result = new PagedResultDto<ProductDto>
72-
{
73-
Items = productDtos,
74-
TotalRecordsCount = pagedResult.TotalRecordsCount,
75-
PageSize = pagedResult.PageSize,
76-
PageNumber = pagedResult.PageNumber,
77-
TotalPages = pagedResult.TotalPages,
78-
};
79-
80-
return Ok(result);
68+
return Ok(pagedResult);
8169
}
8270
catch (Exception error)
8371
{

NorthwindCRUD/Services/PagingService.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
using System.Globalization;
22
using System.Reflection;
3+
using AutoMapper;
4+
using Microsoft.AspNetCore.Mvc.RazorPages;
35
using NorthwindCRUD.Models.Dtos;
46

57
namespace 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

Comments
 (0)