refactor: replace PagedList class
This commit is contained in:
parent
d9751da4f4
commit
1882715f4d
@ -40,17 +40,7 @@ public class CountryManagementController : ControllerBase
|
|||||||
return BadRequest(result.message);
|
return BadRequest(result.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
var metadata = new
|
Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(result.pagingMetadata));
|
||||||
{
|
|
||||||
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));
|
|
||||||
|
|
||||||
return Ok(result.countries);
|
return Ok(result.countries);
|
||||||
}
|
}
|
||||||
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ using AutoMapper;
|
|||||||
using AutoMapper.QueryableExtensions;
|
using AutoMapper.QueryableExtensions;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Server.Data;
|
using Server.Data;
|
||||||
using Server.Helpers;
|
|
||||||
using Server.Models;
|
using Server.Models;
|
||||||
using SharedModels.DataTransferObjects;
|
using SharedModels.DataTransferObjects;
|
||||||
using SharedModels.QueryStringParameters;
|
using SharedModels.QueryStringParameters;
|
||||||
@ -30,13 +29,21 @@ public class CountryManagementService : ICountryManagementService
|
|||||||
|
|
||||||
return (true, String.Empty, _mapper.Map<CountryDto>(country));
|
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);
|
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)
|
public async Task<(bool isSucceed, string message, CountryDto country)> GetCountry(int id)
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
using Server.Helpers;
|
|
||||||
using Server.Models;
|
using Server.Models;
|
||||||
using SharedModels.DataTransferObjects;
|
using SharedModels.DataTransferObjects;
|
||||||
using SharedModels.QueryStringParameters;
|
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, 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)> GetCountry(int id);
|
||||||
Task<(bool isSucceed, string message, CountryDto country)> UpdateCountry(UpdateCountryDto updateCountryDto);
|
Task<(bool isSucceed, string message, CountryDto country)> UpdateCountry(UpdateCountryDto updateCountryDto);
|
||||||
Task<(bool isSucceed, string message)> DeleteCountry(int id);
|
Task<(bool isSucceed, string message)> DeleteCountry(int id);
|
||||||
|
20
SharedModels/QueryStringParameters/PagingMetadata.cs
Normal file
20
SharedModels/QueryStringParameters/PagingMetadata.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user