refactor: replace PagedList class

This commit is contained in:
cuqmbr 2022-10-12 09:42:36 +03:00
parent d9751da4f4
commit 1882715f4d
5 changed files with 35 additions and 46 deletions

View File

@ -40,17 +40,7 @@ public class CountryManagementController : ControllerBase
return BadRequest(result.message);
}
var metadata = new
{
result.countries.TotalCount,
result.countries.PageSize,
result.countries.CurrentPage,
result.countries.TotalPages,
result.countries.HasNext,
result.countries.HasPrevious
};
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata));
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(result.pagingMetadata));
return Ok(result.countries);
}

View File

@ -1,28 +0,0 @@
namespace Server.Helpers;
public class PagedList<T> : List<T>
{
public int CurrentPage { get; private set; }
public int TotalPages { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public bool HasPrevious => CurrentPage > 1;
public bool HasNext => CurrentPage < TotalPages;
public PagedList(List<T> items, int count, int pageNumber, int pageSize)
{
TotalCount = count;
PageSize = pageSize;
CurrentPage = pageNumber;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
AddRange(items);
}
public static PagedList<T> ToPagedList(IQueryable<T> source, int pageNumber, int pageSize)
{
var count = source.Count();
var items = source.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
return new PagedList<T>(items, count, pageNumber, pageSize);
}
}

View File

@ -2,7 +2,6 @@ using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
using Server.Data;
using Server.Helpers;
using Server.Models;
using SharedModels.DataTransferObjects;
using SharedModels.QueryStringParameters;
@ -30,13 +29,21 @@ public class CountryManagementService : ICountryManagementService
return (true, String.Empty, _mapper.Map<CountryDto>(country));
}
public async Task<(bool isSucceed, string message, PagedList<Country> countries)> GetCountries(CountryParameters parameters)
public async
Task<(bool isSucceed, string message, IEnumerable<CountryDto> countries,
PagingMetadata<Country> pagingMetadata)> GetCountries(CountryParameters parameters)
{
var dbCountries = PagedList<Country>.ToPagedList(_dbContext.Countries,
var dbCountries = await _dbContext.Countries
.Skip((parameters.PageNumber - 1) * parameters.PageSize)
.Take(parameters.PageSize)
.ToListAsync();
var pagingMetadata = new PagingMetadata<Country>(_dbContext.Countries,
parameters.PageNumber, parameters.PageSize);
return (true, "", dbCountries);
return (true, "", dbCountries.ConvertAll(c => _mapper.Map<CountryDto>(c)),
pagingMetadata);
}
public async Task<(bool isSucceed, string message, CountryDto country)> GetCountry(int id)

View File

@ -1,4 +1,3 @@
using Server.Helpers;
using Server.Models;
using SharedModels.DataTransferObjects;
using SharedModels.QueryStringParameters;
@ -9,7 +8,8 @@ public interface ICountryManagementService
{
Task<(bool isSucceed, string message, CountryDto country)> AddCountry(CreateCountryDto createCountryDto);
Task<(bool isSucceed, string message, PagedList<Country> countries)> GetCountries(CountryParameters parameters);
Task<(bool isSucceed, string message, IEnumerable<CountryDto> countries,
PagingMetadata<Country> pagingMetadata)> GetCountries(CountryParameters parameters);
Task<(bool isSucceed, string message, CountryDto country)> GetCountry(int id);
Task<(bool isSucceed, string message, CountryDto country)> UpdateCountry(UpdateCountryDto updateCountryDto);
Task<(bool isSucceed, string message)> DeleteCountry(int id);

View File

@ -0,0 +1,20 @@
namespace SharedModels.QueryStringParameters;
public class PagingMetadata<T>
{
public int CurrentPage { get; private set; }
public int TotalPages { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public bool HasPrevious => CurrentPage > 1;
public bool HasNext => CurrentPage < TotalPages;
public PagingMetadata(IQueryable<T> source, int pageNumber, int pageSize)
{
TotalCount = source.Count();
PageSize = pageSize;
CurrentPage = pageNumber;
TotalPages = (int)Math.Ceiling(TotalCount / (double)pageSize);
}
}