All checks were successful
CI/CD Pipeline / Build and Deploy with Docker Compose (push) Successful in 2m58s
80 lines
3.1 KiB
C#
80 lines
3.1 KiB
C#
using Domain.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Models.Helpers;
|
|
using Models.Interfaces;
|
|
using Models.Models;
|
|
using System.Xml.Linq;
|
|
|
|
namespace Models.Repositories
|
|
{
|
|
public class PhSCustomerRepository(PhronCareOperationsHubContext context) : IPhSCustomerRepository
|
|
{
|
|
#region Declaraciones y Constructor
|
|
private readonly PhronCareOperationsHubContext _context = context;
|
|
#endregion
|
|
public async Task<IEnumerable<ECustomer>> GetAllAsync()
|
|
{
|
|
var customers = await _context.PhSCustomers
|
|
.Include(c => c.Accounttypes)
|
|
.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;
|
|
}
|
|
public async Task<IEnumerable<ECustomer>> SearchAsync(string? name, string? document, string? email)
|
|
{
|
|
var query = _context.PhSCustomers
|
|
.Include(c => c.Accounttypes)
|
|
.Include(c => c.PhSCustomerDocuments)
|
|
.Include(c => c.PhSCustomerAddresses)
|
|
.AsQueryable();
|
|
|
|
if (!string.IsNullOrEmpty(name))
|
|
query = query.Where(c => c.Name.Contains(name) || c.BusinessName.Contains(name));
|
|
|
|
if (!string.IsNullOrEmpty(document))
|
|
query = query.Where(c => c.PhSCustomerDocuments.Any(d => d.DocumentNumber == document));
|
|
|
|
if (!string.IsNullOrEmpty(email))
|
|
query = query.Where(c => c.PhSCustomerAddresses.Any(a => a.Email == email));
|
|
|
|
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);
|
|
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;
|
|
}
|
|
}
|
|
}
|