diff --git a/Core/Services/CustomerService.cs b/Core/Services/CustomerService.cs index c1f18ad..7572930 100644 --- a/Core/Services/CustomerService.cs +++ b/Core/Services/CustomerService.cs @@ -45,9 +45,17 @@ namespace Core.Services throw new NotImplementedException(); } - public Task> SearchAsync(string? name, string? email, string? document) + public async Task> SearchAsync(string? name, string? email, string? document) { - throw new NotImplementedException(); + try + { + return await _repository.SearchAsync(name, email, document); + } + catch (Exception ex) + { + var methodName = MethodBase.GetCurrentMethod()?.Name ?? "UnknownMethod"; + throw new Exception($"{methodName} Message: {ex.Message}", ex); + } } public Task UpdateAsync(ECustomer entity) diff --git a/Models/Repositories/PhSCustomerRepository.cs b/Models/Repositories/PhSCustomerRepository.cs index f901ec7..c064443 100644 --- a/Models/Repositories/PhSCustomerRepository.cs +++ b/Models/Repositories/PhSCustomerRepository.cs @@ -41,18 +41,34 @@ namespace Models.Repositories .Include(c => c.PhSCustomerAddresses) .AsQueryable(); - if (!string.IsNullOrEmpty(name)) - query = query.Where(c => c.Name.Contains(name) || c.BusinessName.Contains(name)); + if (!string.IsNullOrWhiteSpace(name)) + { + var loweredName = name.ToLower(); + query = query.Where(c => + c.Name.ToLower().Contains(loweredName) || + c.BusinessName.ToLower().Contains(loweredName)); + } - if (!string.IsNullOrEmpty(document)) - query = query.Where(c => c.PhSCustomerDocuments.Any(d => d.DocumentNumber == document)); + if (!string.IsNullOrWhiteSpace(document)) + { + var loweredDoc = document.ToLower(); + query = query.Where(c => + c.PhSCustomerDocuments.Any(d => + d.DocumentNumber.ToLower().Contains(loweredDoc))); + } - if (!string.IsNullOrEmpty(email)) - query = query.Where(c => c.PhSCustomerAddresses.Any(a => a.Email == email)); + if (!string.IsNullOrWhiteSpace(email)) + { + var loweredEmail = email.ToLower(); + query = query.Where(c => + c.PhSCustomerAddresses.Any(a => + a.Email.ToLower().Contains(loweredEmail))); + } var customers = await query.ToListAsync(); 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 425c877..28c6ee5 100644 --- a/phronCare.API/Controllers/Sales/CustomerController.cs +++ b/phronCare.API/Controllers/Sales/CustomerController.cs @@ -1,6 +1,7 @@ using Core.Interfaces; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using System.Reflection; namespace phronCare.API.Controllers.Sales { @@ -26,5 +27,20 @@ namespace phronCare.API.Controllers.Sales return BadRequest(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}"); + } + } + } }