phronCare/Models/Repositories/PhSCustomerRepository.cs

153 lines
5.8 KiB
C#
Raw Normal View History

2025-04-04 14:58:44 -03:00
using Domain.Entities;
2025-04-06 01:19:47 -03:00
using Domain.Generics;
2025-04-04 14:58:44 -03:00
using Microsoft.EntityFrameworkCore;
2025-04-05 12:54:16 -03:00
using Microsoft.Extensions.Logging;
2025-04-04 14:58:44 -03:00
using Models.Helpers;
using Models.Interfaces;
using Models.Models;
using System.Xml.Linq;
namespace Models.Repositories
{
2025-04-05 12:54:16 -03:00
public class PhSCustomerRepository(PhronCareOperationsHubContext context, ILogger<PhSCustomerRepository> logger) : IPhSCustomerRepository
2025-04-04 14:58:44 -03:00
{
#region Declaraciones y Constructor
private readonly PhronCareOperationsHubContext _context = context;
2025-04-05 12:54:16 -03:00
private readonly ILogger<PhSCustomerRepository> _logger = logger;
2025-04-04 14:58:44 -03:00
#endregion
public async Task<IEnumerable<ECustomer>> GetAllAsync()
{
var customers = await _context.PhSCustomers
.Include(c => c.Accounttypes)
2025-04-04 15:16:24 -03:00
.Include(c => c.PhSCustomerAddresses)
.Include(c => c.PhSCustomerDocuments)
2025-04-04 15:08:16 -03:00
.Take(100)
2025-04-04 14:58:44 -03:00
.ToListAsync();
return customers.Select(EntityMapper.MapEntity<PhSCustomer, ECustomer>);
}
public async Task<ECustomer?> GetByIdAsync(int id)
{
var customer = await _context.PhSCustomers
.Include(c => c.Accounttypes)
.Include(c => c.PhSCustomerAddresses)
.Include(c => c.PhSCustomerDocuments)
.Include(c => c.PhSQuoteHeaders)
.FirstOrDefaultAsync(c => c.Id == id);
return customer != null ? EntityMapper.MapEntity<PhSCustomer, ECustomer>(customer) : null;
}
2025-04-06 01:19:47 -03:00
public async Task<PagedResult<ECustomer>> SearchAsync(
string? name, string? email, string? document,
int page = 1, int pageSize = 50)
2025-04-04 14:58:44 -03:00
{
var query = _context.PhSCustomers
.Include(c => c.Accounttypes)
.Include(c => c.PhSCustomerDocuments)
.Include(c => c.PhSCustomerAddresses)
.AsQueryable();
2025-04-05 19:01:28 -03:00
if (!string.IsNullOrWhiteSpace(name))
{
2025-04-06 01:19:47 -03:00
var lowered = name.ToLower();
2025-04-05 19:01:28 -03:00
query = query.Where(c =>
2025-04-06 01:19:47 -03:00
c.Name.ToLower().Contains(lowered) ||
c.BusinessName.ToLower().Contains(lowered));
2025-04-05 19:01:28 -03:00
}
2025-04-04 14:58:44 -03:00
2025-04-06 01:19:47 -03:00
if (!string.IsNullOrWhiteSpace(document) && document != "?")
2025-04-05 19:01:28 -03:00
{
query = query.Where(c =>
2025-04-06 01:19:47 -03:00
c.PhSCustomerDocuments.Any(d =>
EF.Functions.Like(d.DocumentNumber, $"%{document}%")));
2025-04-05 19:01:28 -03:00
}
2025-04-04 14:58:44 -03:00
2025-04-06 01:19:47 -03:00
if (!string.IsNullOrWhiteSpace(email))
2025-04-05 19:24:02 -03:00
{
2025-04-06 01:19:47 -03:00
var lowered = email.ToLower();
2025-04-05 19:24:02 -03:00
query = query.Where(c =>
2025-04-06 01:19:47 -03:00
c.PhSCustomerAddresses.Any(a =>
a.Email.ToLower().Contains(lowered)));
2025-04-05 19:24:02 -03:00
}
2025-04-06 01:19:47 -03:00
var pagedEntities = await query.ToPagedResultAsync(page, pageSize);
2025-04-05 19:01:28 -03:00
2025-04-06 01:19:47 -03:00
return new PagedResult<ECustomer>
{
Items = pagedEntities.Items.Select(EntityMapper.MapEntity<PhSCustomer, ECustomer>),
TotalItems = pagedEntities.TotalItems,
Page = pagedEntities.Page,
PageSize = pagedEntities.PageSize
};
}
2025-04-05 19:01:28 -03:00
2025-04-06 01:19:47 -03:00
//public async Task<IEnumerable<ECustomer>> 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();
2025-04-05 19:01:28 -03:00
2025-04-06 01:19:47 -03:00
// 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<PhSCustomer, ECustomer>);
//}
2025-04-04 21:44:20 -03:00
2025-04-04 14:58:44 -03:00
public async Task<ECustomer> AddAsync(ECustomer entity)
{
var customer = EntityMapper.MapEntity<ECustomer, PhSCustomer>(entity);
await _context.PhSCustomers.AddAsync(customer);
await _context.SaveChangesAsync();
return EntityMapper.MapEntity<PhSCustomer, ECustomer>(customer);
}
public async Task<bool> UpdateAsync(ECustomer entity)
{
var customer = await _context.PhSCustomers.FindAsync(entity.Id);
if (customer == null) return false;
_context.Entry(customer).CurrentValues.SetValues(entity);
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> DeleteAsync(int id)
{
var customer = await _context.PhSCustomers.FindAsync(id);
if (customer == null) return false;
_context.PhSCustomers.Remove(customer);
await _context.SaveChangesAsync();
return true;
}
}
}