ref: project structure
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
|
||||
namespace mws.backend.dotnet.infrastructure.Persistence;
|
||||
|
||||
public static class PaginationExtensions
|
||||
{
|
||||
public const int DefaultPageSize = 20;
|
||||
public const int MaxPageSize = 100;
|
||||
|
||||
public static async Task<PagedResult<T>> ToPagedResultAsync<T>(
|
||||
this IQueryable<T> query, int page, int pageSize, CancellationToken ct = default)
|
||||
{
|
||||
page = page < 1 ? 1 : page;
|
||||
pageSize = pageSize < 1 ? DefaultPageSize : Math.Min(pageSize, MaxPageSize);
|
||||
|
||||
var totalCount = await query.CountAsync(ct);
|
||||
var items = await query.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync(ct);
|
||||
|
||||
return new PagedResult<T> { Items = items, TotalCount = totalCount, Page = page, PageSize = pageSize };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user