diff --git a/Core/Interfaces/ICustomerDom.cs b/Core/Interfaces/ICustomerDom.cs index 3f8520c..be7c9f9 100644 --- a/Core/Interfaces/ICustomerDom.cs +++ b/Core/Interfaces/ICustomerDom.cs @@ -1,4 +1,5 @@ using Domain.Entities; +using Domain.Generics; using System; using System.Collections.Generic; using System.Linq; @@ -13,7 +14,7 @@ namespace Core.Interfaces Task DeleteAsync(int id); Task> GetAllAsync(); Task GetByIdAsync(int id); - Task> SearchAsync(string? name, string? email, string? document); + 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 7572930..2b2bd0a 100644 --- a/Core/Services/CustomerService.cs +++ b/Core/Services/CustomerService.cs @@ -1,6 +1,7 @@ using System.Reflection; using Core.Interfaces; using Domain.Entities; +using Domain.Generics; using Models.Helpers; using Models.Interfaces; using Models.Models; @@ -45,11 +46,11 @@ namespace Core.Services throw new NotImplementedException(); } - public async Task> SearchAsync(string? name, string? email, string? document) + public async Task> SearchAsync(string? name, string? email, string? document, int page, int pageSize) { try { - return await _repository.SearchAsync(name, email, document); + return await _repository.SearchAsync(name, email, document, page, pageSize); } catch (Exception ex) { @@ -58,6 +59,9 @@ namespace Core.Services } } + + + public Task UpdateAsync(ECustomer entity) { throw new NotImplementedException(); diff --git a/Domain/Generics/PagedRequest.cs b/Domain/Generics/PagedRequest.cs new file mode 100644 index 0000000..45a818c --- /dev/null +++ b/Domain/Generics/PagedRequest.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Domain.Generics +{ + public class PagedRequest + { + public int Page { get; set; } = 1; // Página actual (empieza en 1) + public int PageSize { get; set; } = 10; // Tamaño de página por defecto + } +} diff --git a/Domain/Generics/PagedResult.cs b/Domain/Generics/PagedResult.cs new file mode 100644 index 0000000..c6d1c51 --- /dev/null +++ b/Domain/Generics/PagedResult.cs @@ -0,0 +1,12 @@ +namespace Domain.Generics +{ + public class PagedResult + { + public IEnumerable Items { get; set; } = Enumerable.Empty(); + public int TotalItems { get; set; } + public int Page { get; set; } + public int PageSize { get; set; } + + public int TotalPages => (int)Math.Ceiling((double)TotalItems / PageSize); + } +} diff --git a/Models/Helpers/PaginationExtensions.cs b/Models/Helpers/PaginationExtensions.cs new file mode 100644 index 0000000..9cd5123 --- /dev/null +++ b/Models/Helpers/PaginationExtensions.cs @@ -0,0 +1,33 @@ +using Domain.Generics; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Models.Helpers +{ + public static class PaginationExtensions + { + public static async Task> ToPagedResultAsync( + this IQueryable query, + int page, + int pageSize) + { + var totalItems = await query.CountAsync(); + var items = await query + .Skip((page - 1) * pageSize) + .Take(pageSize) + .ToListAsync(); + + return new PagedResult + { + Items = items, + TotalItems = totalItems, + Page = page, + PageSize = pageSize + }; + } + } +} diff --git a/Models/Interfaces/IPhSCustomerRepository.cs b/Models/Interfaces/IPhSCustomerRepository.cs index f0dba74..417276e 100644 --- a/Models/Interfaces/IPhSCustomerRepository.cs +++ b/Models/Interfaces/IPhSCustomerRepository.cs @@ -1,4 +1,5 @@ using Domain.Entities; +using Domain.Generics; using Models.Models; using System; using System.Collections.Generic; @@ -14,7 +15,12 @@ namespace Models.Interfaces Task DeleteAsync(int id); Task> GetAllAsync(); Task GetByIdAsync(int id); - Task> SearchAsync(string? name, string? email, string? document); + Task> SearchAsync( + string? name, + string? email, + string? document, + int page, + int pageSize); Task UpdateAsync(ECustomer entity); } } diff --git a/Models/Repositories/PhSCustomerRepository.cs b/Models/Repositories/PhSCustomerRepository.cs index 283e638..9f0062d 100644 --- a/Models/Repositories/PhSCustomerRepository.cs +++ b/Models/Repositories/PhSCustomerRepository.cs @@ -1,4 +1,5 @@ using Domain.Entities; +using Domain.Generics; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using Models.Helpers; @@ -36,48 +37,92 @@ namespace Models.Repositories return customer != null ? EntityMapper.MapEntity(customer) : null; } - public async Task> SearchAsync(string? name, string? email, string? document) + public async Task> SearchAsync( + string? name, string? email, string? document, + int page = 1, int pageSize = 50) { 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(); + var lowered = 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))); + c.Name.ToLower().Contains(lowered) || + c.BusinessName.ToLower().Contains(lowered)); } if (!string.IsNullOrWhiteSpace(document) && document != "?") { query = query.Where(c => - c.PhSCustomerDocuments.Any(a => - EF.Functions.Like(a.DocumentNumber, $"%{document}%"))); + c.PhSCustomerDocuments.Any(d => + EF.Functions.Like(d.DocumentNumber, $"%{document}%"))); } - var customers = await query.ToListAsync(); + if (!string.IsNullOrWhiteSpace(email)) + { + var lowered = email.ToLower(); + query = query.Where(c => + c.PhSCustomerAddresses.Any(a => + a.Email.ToLower().Contains(lowered))); + } - Console.WriteLine($"VALOR RECIBIDO DE 'name': {name}"); - Console.WriteLine($"VALOR RECIBIDO DE 'email': {email}"); - Console.WriteLine($"VALOR RECIBIDO DE 'document': {document}"); + var pagedEntities = await query.ToPagedResultAsync(page, pageSize); - - return customers.Select(EntityMapper.MapEntity); + return new PagedResult + { + Items = pagedEntities.Items.Select(EntityMapper.MapEntity), + TotalItems = pagedEntities.TotalItems, + Page = pagedEntities.Page, + PageSize = pagedEntities.PageSize + }; } + //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 28c6ee5..dd5b6bc 100644 --- a/phronCare.API/Controllers/Sales/CustomerController.cs +++ b/phronCare.API/Controllers/Sales/CustomerController.cs @@ -14,7 +14,7 @@ namespace phronCare.API.Controllers.Sales { _customerService = customerService ?? throw new ArgumentNullException(nameof(customerService)); } - [HttpGet("GetAll")] + [HttpGet("all")] public async Task GetAll() { try @@ -28,11 +28,16 @@ namespace phronCare.API.Controllers.Sales } } [HttpGet("search")] - public async Task Search([FromQuery] string? name, [FromQuery] string? email, [FromQuery] string? document) + public async Task Search( + [FromQuery] string? name, + [FromQuery] string? email, + [FromQuery] string? document, + [FromQuery] int page = 1, + [FromQuery] int pageSize = 50) { try { - var result = await _customerService.SearchAsync(name, email, document); + var result = await _customerService.SearchAsync(name, email, document, page, pageSize); return Ok(result); } catch (Exception ex) @@ -41,6 +46,20 @@ namespace phronCare.API.Controllers.Sales return StatusCode(500, $"{methodName} Message: {ex.Message}"); } } + //[HttpGet("search")] + //public async Task Search([FromQuery] string? name, [FromQuery] string? email, [FromQuery] string? document) + //{ + // try + // { + // var result = await _customerService.SearchAsync(name, email, document); + // return Ok(result); + // } + // catch (Exception ex) + // { + // var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod"; + // return StatusCode(500, $"{methodName} Message: {ex.Message}"); + // } + //} } }