API
This commit is contained in:
59
PARR.API/Extensions/PaginationExtensions.cs
Normal file
59
PARR.API/Extensions/PaginationExtensions.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using PARR.API.Contracts.V1.Responses.Base;
|
||||
using PARR.DAL.DomainModels;
|
||||
|
||||
namespace PARR.API.Extensions
|
||||
{
|
||||
public static class PaginationExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Заполняет TotalItems, PageNumber, PageSize, TotalPage
|
||||
/// </summary>
|
||||
/// <typeparam name="TypeResponse"></typeparam>
|
||||
/// <typeparam name="Query"></typeparam>
|
||||
/// <param name="pagedResponse"></param>
|
||||
/// <param name="paginationFilter"></param>
|
||||
/// <param name="quaryAllItemsApplyFilters"></param>
|
||||
/// <returns></returns>
|
||||
public static PagedResponse<TypeResponse> GetPaginatedProps<TypeResponse, Query>(
|
||||
this PagedResponse<TypeResponse> pagedResponse,
|
||||
PaginationFilter paginationFilter,
|
||||
IQueryable<Query> quaryAllItemsApplyFilters
|
||||
)
|
||||
{
|
||||
// TODO: CountAsync - подумать и затолкать в Task
|
||||
int totalItems = quaryAllItemsApplyFilters.Count();//.CountAsync().GetAwaiter().GetResult();
|
||||
return CreatePaginatedResponse(totalItems, paginationFilter, pagedResponse);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Заполняет TotalItems, PageNumber, PageSize, TotalPage
|
||||
/// </summary>
|
||||
/// <typeparam name="TypeResponse"></typeparam>
|
||||
/// <param name="pagedResponse"></param>
|
||||
/// <param name="paginationFilter"></param>
|
||||
/// <param name="totalItems"></param>
|
||||
/// <returns></returns>
|
||||
public static PagedResponse<TypeResponse> GetPaginatedProps<TypeResponse>(
|
||||
this PagedResponse<TypeResponse> pagedResponse,
|
||||
PaginationFilter paginationFilter,
|
||||
int totalItems
|
||||
)
|
||||
{
|
||||
return CreatePaginatedResponse(totalItems, paginationFilter, pagedResponse);
|
||||
}
|
||||
|
||||
private static PagedResponse<T> CreatePaginatedResponse<T>(int totalItems, PaginationFilter paginationFilter, PagedResponse<T> pagedResponse)
|
||||
{
|
||||
pagedResponse.TotalItems = totalItems;
|
||||
pagedResponse.PageNumber = paginationFilter.PageNumber >= 1 ? paginationFilter.PageNumber : (int?)null;
|
||||
pagedResponse.PageSize = paginationFilter.PageSize >= 1 ? paginationFilter.PageSize : (int?)null;
|
||||
pagedResponse.TotalPage =
|
||||
(pagedResponse.TotalItems.HasValue && pagedResponse.PageSize.HasValue)
|
||||
? (int)Math.Ceiling(pagedResponse.TotalItems.Value / (double)pagedResponse.PageSize.Value)
|
||||
: (int?)null;
|
||||
|
||||
return pagedResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user