diff --git a/Core/Interfaces/ICustomerDom.cs b/Core/Interfaces/ICustomerDom.cs index be7c9f9..009de10 100644 --- a/Core/Interfaces/ICustomerDom.cs +++ b/Core/Interfaces/ICustomerDom.cs @@ -12,7 +12,7 @@ namespace Core.Interfaces { Task AddAsync(ECustomer entity); Task DeleteAsync(int id); - Task> GetAllAsync(); + Task> GetAllAsync(int page = 1, int pageSize = 50); Task GetByIdAsync(int id); Task> SearchAsync(string? name, string? email, string? document, int page = 1, int pageSize = 50); Task UpdateAsync(ECustomer entity); diff --git a/Core/Services/CustomerService.cs b/Core/Services/CustomerService.cs index 2b2bd0a..bea465e 100644 --- a/Core/Services/CustomerService.cs +++ b/Core/Services/CustomerService.cs @@ -28,11 +28,11 @@ namespace Core.Services throw new NotImplementedException(); } - public async Task> GetAllAsync() + public async Task> GetAllAsync(int page = 1, int pageSize = 50) { try { - return await _repository.GetAllAsync(); + return await _repository.GetAllAsync(page, pageSize); } catch (Exception ex) { diff --git a/Models/Interfaces/IPhSCustomerRepository.cs b/Models/Interfaces/IPhSCustomerRepository.cs index 417276e..f5d390c 100644 --- a/Models/Interfaces/IPhSCustomerRepository.cs +++ b/Models/Interfaces/IPhSCustomerRepository.cs @@ -13,7 +13,7 @@ namespace Models.Interfaces { Task AddAsync(ECustomer entity); Task DeleteAsync(int id); - Task> GetAllAsync(); + Task> GetAllAsync(int page = 1, int pageSize = 50); Task GetByIdAsync(int id); Task> SearchAsync( string? name, diff --git a/Models/Repositories/PhSCustomerRepository.cs b/Models/Repositories/PhSCustomerRepository.cs index 9f0062d..45602b7 100644 --- a/Models/Repositories/PhSCustomerRepository.cs +++ b/Models/Repositories/PhSCustomerRepository.cs @@ -16,16 +16,25 @@ namespace Models.Repositories private readonly ILogger _logger = logger; #endregion - public async Task> GetAllAsync() + public async Task> GetAllAsync(int page = 1, int pageSize = 50) { - var customers = await _context.PhSCustomers + var query = _context.PhSCustomers .Include(c => c.Accounttypes) .Include(c => c.PhSCustomerAddresses) .Include(c => c.PhSCustomerDocuments) - .Take(100) - .ToListAsync(); - return customers.Select(EntityMapper.MapEntity); + .AsQueryable(); + + var pagedEntities = await query.ToPagedResultAsync(page, pageSize); + + return new PagedResult + { + Items = pagedEntities.Items.Select(EntityMapper.MapEntity), + TotalItems = pagedEntities.TotalItems, + Page = pagedEntities.Page, + PageSize = pagedEntities.PageSize + }; } + public async Task GetByIdAsync(int id) { var customer = await _context.PhSCustomers @@ -81,48 +90,6 @@ namespace Models.Repositories }; } - //public async Task> 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); - //} - public async Task AddAsync(ECustomer entity) { var customer = EntityMapper.MapEntity(entity); diff --git a/phronCare.API/Controllers/Sales/CustomerController.cs b/phronCare.API/Controllers/Sales/CustomerController.cs index dd5b6bc..1691776 100644 --- a/phronCare.API/Controllers/Sales/CustomerController.cs +++ b/phronCare.API/Controllers/Sales/CustomerController.cs @@ -15,16 +15,17 @@ namespace phronCare.API.Controllers.Sales _customerService = customerService ?? throw new ArgumentNullException(nameof(customerService)); } [HttpGet("all")] - public async Task GetAll() + public async Task GetAll([FromQuery] int page = 1, [FromQuery] int pageSize = 50) { try { - var result = await _customerService.GetAllAsync(); + var result = await _customerService.GetAllAsync(page, pageSize); return Ok(result); } catch (Exception ex) { - return BadRequest(ex.Message); + var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod"; + return StatusCode(500, $"{methodName} Message: {ex.Message}"); } } [HttpGet("search")]