Skip to content

Commit 2007b9c

Browse files
Implement Paging.
1 parent 259703b commit 2007b9c

File tree

4 files changed

+119
-9
lines changed

4 files changed

+119
-9
lines changed
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace HyperCache.Web.Dtos;
2+
3+
public class CustomPropertyDto
4+
{
5+
public Guid Id { get; set; }
6+
public Guid ParentId { get; set; }
7+
public required string Name { get; set; }
8+
public required string Value { get; set; }
9+
public DateTime CreatedOn { get; set; }
10+
public DateTime ModifiedOn { get; set; }
11+
public required string CreatedBy { get; set; }
12+
public required string ModifiedBy { get; set; }
13+
public required string ParentTable { get; set; }
14+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace HyperCache.Web.Dtos;
2+
3+
public class PagedResponseDto<T>
4+
{
5+
public List<T> Items { get; set; } = [];
6+
public int CurrentPage { get; set; }
7+
public int TotalPages { get; set; }
8+
public bool HasPreviousPage => CurrentPage > 1;
9+
public bool HasNextPage => CurrentPage < TotalPages;
10+
}

HyperCache.Web/Pages/CustomProperties.razor

+67-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
@page "/custom-properties"
2-
@inject HttpClient Http
3-
@using HyperCache.Shared.Dtos
42

53
<h3 class="mt-4 text-primary">Custom Properties</h3>
64

7-
@if (properties == null)
5+
@if (pagedResponse == null)
86
{
97
<div class="text-center mt-4">
108
<div class="spinner-border text-primary" role="status"></div>
119
<p>Loading...</p>
1210
</div>
1311
}
14-
else if (!properties.Any())
12+
else if (!pagedResponse.Items.Any())
1513
{
1614
<div class="alert alert-info mt-4" role="alert">
1715
No properties found.
@@ -28,7 +26,7 @@ else
2826
</tr>
2927
</thead>
3028
<tbody>
31-
@foreach (var property in properties)
29+
@foreach (var property in pagedResponse.Items)
3230
{
3331
<tr>
3432
<td>@property.Name</td>
@@ -40,13 +38,74 @@ else
4038
}
4139
</tbody>
4240
</table>
41+
42+
<nav class="mt-4">
43+
<ul class="pagination">
44+
<li class="page-item @(pagedResponse.HasPreviousPage ? "" : "disabled")">
45+
<button class="page-link" @onclick="PreviousPage">Previous</button>
46+
</li>
47+
@foreach (var pageNumber in GetPageNumbers())
48+
{
49+
<li class="page-item @(currentPage == pageNumber ? "active" : "")">
50+
<button class="page-link" @onclick="() => ChangePage(pageNumber)">@pageNumber</button>
51+
</li>
52+
}
53+
<li class="page-item @(pagedResponse.HasNextPage ? "" : "disabled")">
54+
<button class="page-link" @onclick="NextPage">Next</button>
55+
</li>
56+
</ul>
57+
</nav>
4358
}
4459

4560
@code {
46-
private List<CustomPropertyDto>? properties;
61+
private PagedResponseDto<CustomPropertyDto>? pagedResponse;
62+
private int currentPage = 1;
63+
private int pageSize = 20;
4764

4865
protected override async Task OnInitializedAsync()
4966
{
50-
properties = await Http.GetFromJsonAsync<List<CustomPropertyDto>>("CustomProperties/paged?page=1&pageSize=20");
67+
await LoadProperties();
68+
}
69+
70+
private async Task LoadProperties()
71+
{
72+
pagedResponse = await Http.GetFromJsonAsync<PagedResponseDto<CustomPropertyDto>>(
73+
$"{ApiConstant.CustomPropertiesPaged}?page={currentPage}&pageSize={pageSize}");
74+
}
75+
76+
private async Task ChangePage(int page)
77+
{
78+
currentPage = page;
79+
await LoadProperties();
80+
}
81+
82+
private async Task PreviousPage()
83+
{
84+
if (pagedResponse?.HasPreviousPage == true)
85+
{
86+
currentPage--;
87+
await LoadProperties();
88+
}
89+
}
90+
91+
private async Task NextPage()
92+
{
93+
if (pagedResponse?.HasNextPage == true)
94+
{
95+
currentPage++;
96+
await LoadProperties();
97+
}
98+
}
99+
100+
private IEnumerable<int> GetPageNumbers()
101+
{
102+
const int maxVisiblePages = 5;
103+
int startPage = Math.Max(1, currentPage - maxVisiblePages / 2);
104+
int endPage = Math.Min(startPage + maxVisiblePages - 1, pagedResponse?.TotalPages ?? 1);
105+
106+
for (int i = startPage; i <= endPage; i++)
107+
{
108+
yield return i;
109+
}
51110
}
52-
}
111+
}

HyperCache/Controllers/CustomPropertiesController.cs

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using HyperCache.Api.Data;
2+
using HyperCache.Api.Models;
23
using Microsoft.AspNetCore.Mvc;
34
using Microsoft.EntityFrameworkCore;
45

@@ -12,11 +13,37 @@ public class CustomPropertiesController(AppDbContext context) : ControllerBase
1213
[HttpGet("paged")]
1314
public async Task<IActionResult> GetPagedProperties([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
1415
{
16+
if (page < 1 || pageSize < 1)
17+
{
18+
return BadRequest("Page and PageSize must be greater than 0.");
19+
}
20+
21+
var totalCount = await context.CustomProperties.CountAsync();
22+
var totalPages = (int)Math.Ceiling(totalCount / (double)pageSize);
23+
1524
var properties = await context.CustomProperties
1625
.Skip((page - 1) * pageSize)
1726
.Take(pageSize)
1827
.ToListAsync();
19-
return Ok(properties);
28+
29+
var response = new
30+
{
31+
Items = properties.Select(p => new CustomProperty
32+
{
33+
Id = p.Id,
34+
Name = p.Name,
35+
Value = p.Value,
36+
ParentTable = p.ParentTable,
37+
CreatedBy = p.CreatedBy,
38+
ModifiedBy = p.ModifiedBy
39+
}).ToList(),
40+
CurrentPage = page,
41+
TotalPages = totalPages,
42+
HasPreviousPage = page > 1,
43+
HasNextPage = page < totalPages
44+
};
45+
46+
return Ok(response);
2047
}
2148

2249
[HttpGet("{id}")]

0 commit comments

Comments
 (0)