Add Pagination on GetAll Customer in API
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 3m7s

This commit is contained in:
Leandro Hernan Rojas 2025-04-06 01:35:31 -03:00
parent 3962d889fd
commit 650650b9a6
5 changed files with 22 additions and 54 deletions

View File

@ -12,7 +12,7 @@ namespace Core.Interfaces
{ {
Task<ECustomer> AddAsync(ECustomer entity); Task<ECustomer> AddAsync(ECustomer entity);
Task<bool> DeleteAsync(int id); Task<bool> DeleteAsync(int id);
Task<IEnumerable<ECustomer>> GetAllAsync(); Task<PagedResult<ECustomer>> GetAllAsync(int page = 1, int pageSize = 50);
Task<ECustomer?> GetByIdAsync(int id); Task<ECustomer?> GetByIdAsync(int id);
Task<PagedResult<ECustomer>> SearchAsync(string? name, string? email, string? document, int page = 1, int pageSize = 50); Task<PagedResult<ECustomer>> SearchAsync(string? name, string? email, string? document, int page = 1, int pageSize = 50);
Task<bool> UpdateAsync(ECustomer entity); Task<bool> UpdateAsync(ECustomer entity);

View File

@ -28,11 +28,11 @@ namespace Core.Services
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<IEnumerable<ECustomer>> GetAllAsync() public async Task<PagedResult<ECustomer>> GetAllAsync(int page = 1, int pageSize = 50)
{ {
try try
{ {
return await _repository.GetAllAsync(); return await _repository.GetAllAsync(page, pageSize);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -13,7 +13,7 @@ namespace Models.Interfaces
{ {
Task<ECustomer> AddAsync(ECustomer entity); Task<ECustomer> AddAsync(ECustomer entity);
Task<bool> DeleteAsync(int id); Task<bool> DeleteAsync(int id);
Task<IEnumerable<ECustomer>> GetAllAsync(); Task<PagedResult<ECustomer>> GetAllAsync(int page = 1, int pageSize = 50);
Task<ECustomer?> GetByIdAsync(int id); Task<ECustomer?> GetByIdAsync(int id);
Task<PagedResult<ECustomer>> SearchAsync( Task<PagedResult<ECustomer>> SearchAsync(
string? name, string? name,

View File

@ -16,16 +16,25 @@ namespace Models.Repositories
private readonly ILogger<PhSCustomerRepository> _logger = logger; private readonly ILogger<PhSCustomerRepository> _logger = logger;
#endregion #endregion
public async Task<IEnumerable<ECustomer>> GetAllAsync() public async Task<PagedResult<ECustomer>> GetAllAsync(int page = 1, int pageSize = 50)
{ {
var customers = await _context.PhSCustomers var query = _context.PhSCustomers
.Include(c => c.Accounttypes) .Include(c => c.Accounttypes)
.Include(c => c.PhSCustomerAddresses) .Include(c => c.PhSCustomerAddresses)
.Include(c => c.PhSCustomerDocuments) .Include(c => c.PhSCustomerDocuments)
.Take(100) .AsQueryable();
.ToListAsync();
return customers.Select(EntityMapper.MapEntity<PhSCustomer, ECustomer>); var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
return new PagedResult<ECustomer>
{
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSCustomer, ECustomer>),
TotalItems = pagedEntities.TotalItems,
Page = pagedEntities.Page,
PageSize = pagedEntities.PageSize
};
} }
public async Task<ECustomer?> GetByIdAsync(int id) public async Task<ECustomer?> GetByIdAsync(int id)
{ {
var customer = await _context.PhSCustomers var customer = await _context.PhSCustomers
@ -81,48 +90,6 @@ namespace Models.Repositories
}; };
} }
//public async Task<IEnumerable<ECustomer>> SearchAsync(string? name, string? email, string? document)
//{
// var query = _context.PhSCustomers
// .Include(c => c.Accounttypes)
// .Include(c => c.PhSCustomerDocuments)
// .Include(c => c.PhSCustomerAddresses)
// .Take(100)
// .AsQueryable();
// if (!string.IsNullOrWhiteSpace(name))
// {
// var loweredName = name.ToLower();
// query = query.Where(c =>
// c.Name.ToLower().Contains(loweredName) ||
// c.BusinessName.ToLower().Contains(loweredName));
// }
// if (!string.IsNullOrWhiteSpace(email))
// {
// var loweredEmail = email.ToLower();
// query = query.Where(c =>
// c.PhSCustomerAddresses.Any(a =>
// a.Email.ToLower().Contains(loweredEmail)));
// }
// if (!string.IsNullOrWhiteSpace(document) && document != "?")
// {
// query = query.Where(c =>
// c.PhSCustomerDocuments.Any(a =>
// EF.Functions.Like(a.DocumentNumber, $"%{document}%")));
// }
// var customers = await query.ToListAsync();
// Console.WriteLine($"VALOR RECIBIDO DE 'name': {name}");
// Console.WriteLine($"VALOR RECIBIDO DE 'email': {email}");
// Console.WriteLine($"VALOR RECIBIDO DE 'document': {document}");
// return customers.Select(EntityMapper.MapEntity<PhSCustomer, ECustomer>);
//}
public async Task<ECustomer> AddAsync(ECustomer entity) public async Task<ECustomer> AddAsync(ECustomer entity)
{ {
var customer = EntityMapper.MapEntity<ECustomer, PhSCustomer>(entity); var customer = EntityMapper.MapEntity<ECustomer, PhSCustomer>(entity);

View File

@ -15,16 +15,17 @@ namespace phronCare.API.Controllers.Sales
_customerService = customerService ?? throw new ArgumentNullException(nameof(customerService)); _customerService = customerService ?? throw new ArgumentNullException(nameof(customerService));
} }
[HttpGet("all")] [HttpGet("all")]
public async Task<IActionResult> GetAll() public async Task<IActionResult> GetAll([FromQuery] int page = 1, [FromQuery] int pageSize = 50)
{ {
try try
{ {
var result = await _customerService.GetAllAsync(); var result = await _customerService.GetAllAsync(page, pageSize);
return Ok(result); return Ok(result);
} }
catch (Exception ex) catch (Exception ex)
{ {
return BadRequest(ex.Message); var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod";
return StatusCode(500, $"{methodName} Message: {ex.Message}");
} }
} }
[HttpGet("search")] [HttpGet("search")]