using CuentasCobrar.CORE.Entities; using CuentasCobrar.CORE.Interfaces; using CuentasCobrar.CORE.Pagination; using CuentasCobrar.CORE.QueryFilters; using CuentasCobrar.INFRASTRUCTURE.Data; using Microsoft.EntityFrameworkCore; namespace CuentasCobrar.INFRASTRUCTURE.Repositories { /// /// Clase encargada de realizar todos las operaciones de lectura, modificación y adición de datos /// en la tabla Cliente de la base de datos. /// /// Implementa la interfaz IClienteRepo public class ClienteRepo : IClienteRepo { /// /// Contexto de la base de datos en SQL Server. /// private readonly CuentasCobrarContext _cuentasCobrarContext; public ClienteRepo(CuentasCobrarContext cuentasCobrarContext) { _cuentasCobrarContext = cuentasCobrarContext; } public async Task> Get() { var clientes = await _cuentasCobrarContext.Clientes.OrderBy(x => x.Nombre).ToListAsync(); return clientes; } public async Task> Get(ClienteQueryFilter filtros) { var clientes = await _cuentasCobrarContext.Clientes.OrderBy(x => x.Nombre).ToListAsync(); var paises = await _cuentasCobrarContext.Paises.ToListAsync(); var tiposIdentificacion = await _cuentasCobrarContext.TipoIdentificaciones.ToListAsync(); if (filtros.Busqueda != null) clientes = filtros.FiltrarPorBusqueda(clientes, paises, tiposIdentificacion); var clientesPaginados = new ListaDePaginacion(clientes, filtros.NumeroDePagina, filtros.RegistrosPorPagina); return clientesPaginados; } public async Task Get(string noIdentificacion) { var cliente = await _cuentasCobrarContext.Clientes.FirstOrDefaultAsync(x => x.NoIdentificacion.ToUpper() == noIdentificacion.ToUpper()); return cliente; } public async Task Post(Cliente cliente) { bool agregado = false; cliente.NoIdentificacion = cliente.NoIdentificacion.ToUpper(); await _cuentasCobrarContext.Clientes.AddAsync(cliente); int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync(); if (filasAfectadas > 0) { agregado = true; } return agregado; } public async Task Put(string noIdentificacion, Cliente cliente) { try { var clienteActual = await Get(noIdentificacion.ToUpper()); clienteActual.NoIdentificacion = cliente.NoIdentificacion.ToUpper(); clienteActual.IdTipoIdentificacion = cliente.IdTipoIdentificacion; clienteActual.Nombre = cliente.Nombre; clienteActual.Apellido1 = cliente.Apellido1; clienteActual.Apellido2 = cliente.Apellido2; clienteActual.IdPaisNacionalidad = cliente.IdPaisNacionalidad; clienteActual.Correo = cliente.Correo; clienteActual.Direccion = cliente.Direccion; clienteActual.Habilitado = cliente.Habilitado; await _cuentasCobrarContext.SaveChangesAsync(); } catch (Exception) { return false; } return true; } public async Task Delete(string noIdentificacion) { bool eliminado = false; var cliente = await Get(noIdentificacion.ToUpper()); _cuentasCobrarContext.Remove(cliente); int filasAfectadas = await _cuentasCobrarContext.SaveChangesAsync(); if (filasAfectadas > 0) { eliminado = true; } return eliminado; } } }