Add Search Customer in API
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m55s

This commit is contained in:
Leandro Hernan Rojas 2025-04-04 21:44:20 -03:00
parent 17127d3921
commit 4537352054
3 changed files with 48 additions and 8 deletions

View File

@ -45,9 +45,17 @@ namespace Core.Services
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<IEnumerable<ECustomer>> SearchAsync(string? name, string? email, string? document) public async Task<IEnumerable<ECustomer>> 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<bool> UpdateAsync(ECustomer entity) public Task<bool> UpdateAsync(ECustomer entity)

View File

@ -41,18 +41,34 @@ namespace Models.Repositories
.Include(c => c.PhSCustomerAddresses) .Include(c => c.PhSCustomerAddresses)
.AsQueryable(); .AsQueryable();
if (!string.IsNullOrEmpty(name)) if (!string.IsNullOrWhiteSpace(name))
query = query.Where(c => c.Name.Contains(name) || c.BusinessName.Contains(name)); {
var loweredName = name.ToLower();
query = query.Where(c =>
c.Name.ToLower().Contains(loweredName) ||
c.BusinessName.ToLower().Contains(loweredName));
}
if (!string.IsNullOrEmpty(document)) if (!string.IsNullOrWhiteSpace(document))
query = query.Where(c => c.PhSCustomerDocuments.Any(d => d.DocumentNumber == document)); {
var loweredDoc = document.ToLower();
query = query.Where(c =>
c.PhSCustomerDocuments.Any(d =>
d.DocumentNumber.ToLower().Contains(loweredDoc)));
}
if (!string.IsNullOrEmpty(email)) if (!string.IsNullOrWhiteSpace(email))
query = query.Where(c => c.PhSCustomerAddresses.Any(a => a.Email == email)); {
var loweredEmail = email.ToLower();
query = query.Where(c =>
c.PhSCustomerAddresses.Any(a =>
a.Email.ToLower().Contains(loweredEmail)));
}
var customers = await query.ToListAsync(); var customers = await query.ToListAsync();
return customers.Select(EntityMapper.MapEntity<PhSCustomer, ECustomer>); 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

@ -1,6 +1,7 @@
using Core.Interfaces; using Core.Interfaces;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Reflection;
namespace phronCare.API.Controllers.Sales namespace phronCare.API.Controllers.Sales
{ {
@ -26,5 +27,20 @@ namespace phronCare.API.Controllers.Sales
return BadRequest(ex.Message); return BadRequest(ex.Message);
} }
} }
[HttpGet("search")]
public async Task<IActionResult> 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}");
}
}
} }
} }