Add Search Customer in API
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m55s
All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m55s
This commit is contained in:
parent
17127d3921
commit
4537352054
@ -45,9 +45,17 @@ namespace Core.Services
|
||||
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)
|
||||
|
||||
@ -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<PhSCustomer, ECustomer>);
|
||||
}
|
||||
|
||||
public async Task<ECustomer> AddAsync(ECustomer entity)
|
||||
{
|
||||
var customer = EntityMapper.MapEntity<ECustomer, PhSCustomer>(entity);
|
||||
|
||||
@ -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<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}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user